How to make a drop down list in yii2?

前端 未结 12 1996
天命终不由人
天命终不由人 2020-12-04 13:07

How to make a dropdown in yii2 using an activeform and a model? Since all the methods has changed in yii2,how it is done

相关标签:
12条回答
  • 2020-12-04 13:34

    In ActiveForm just use:

    <?=
        $form->field($model, 'state_id')
             ->dropDownList(['prompt' => '---- Select State ----'])
             ->label('State')
    ?>
    
    0 讨论(0)
  • 2020-12-04 13:39

    Html::activeDropDownList($model, 'id', ArrayHelper::map(AttendanceLabel::find()->all(), 'id', 'label_name'), ['prompt'=>'Attendance Status'] );

    0 讨论(0)
  • 2020-12-04 13:39

    Following can also be done. If you want to append prepend icon. This will be helpful.

    <?php $form = ActiveForm::begin();    
       echo $form->field($model, 'field')->begin();
         echo Html::activeLabel($model, 'field', ["class"=>"control-label col-md-4"]); ?>
           <div class="col-md-5">
              <?php echo Html::activeDropDownList($model, 'field', $array_list, ['class'=>'form-control']); ?>
              <p><i><small>Please select field</small></i>.</p>
              <?php echo Html::error($model, 'field', ['class'=>'help-block']); ?>
           </div>
       <?php echo $form->field($model, 'field')->end(); 
    ActiveForm::end();?>
    
    0 讨论(0)
  • 2020-12-04 13:41

    It Seems there are many good answers for this question .So i will try to give a detailed answer

    active form and hardcoded data

    <?php
        echo $form->field($model, 'name')->dropDownList(['1' => 'Yes', '0' => 'No'],['prompt'=>'Select Option']);
    ?>
    

    or

    <?php
        $a= ['1' => 'Yes', '0' => 'No'];
        echo $form->field($model, 'name')->dropDownList($a,['prompt'=>'Select Option']);
    ?>
    

    active form and data from a db table

    we are going to use ArrayHelper so first add it to the name space by

    <?php
        use yii\helpers\ArrayHelper;
    ?>
    

    ArrayHelper has many use full functions which could be used to process arrays map () is the one we are going to use here this function help to make a map ( of key-value pairs) from a multidimensional array or an array of objects.

    <?php
        echo $form->field($model, 'name')->dropDownList(ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
    ?>
    

    not part of a active form

    <?php
        echo Html::activeDropDownList($model, 'filed_name',['1' => 'Yes', '0' => 'No']) ;
    ?>
    

    or

    <?php
        $a= ['1' => 'Yes', '0' => 'No'];
        echo Html::activeDropDownList($model, 'filed_name',$a) ;
    ?>
    

    not an active form but data from a db table

    <?php
        echo Html::activeDropDownList($model, 'filed_name',ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
    ?>
    
    0 讨论(0)
  • 2020-12-04 13:41
    <?= $form->field($model, 'attribute_name')->dropDownList(
             ArrayHelper::map(Table_name::find()->all(),'id','field_name'),
            ['prompt' => 'Select']
    ) ?>
    

    This will help you...Don't forget to use the class file in header.

    0 讨论(0)
  • 2020-12-04 13:48

    It is like

    <?php
    use yii\helpers\ArrayHelper;
    use backend\models\Standard;
    ?>
    
    <?= Html::activeDropDownList($model, 's_id',
          ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
    

    ArrayHelper in Yii2 replaces the CHtml list data in Yii 1.1.[Please load array data from your controller]

    EDIT

    Load data from your controller.

    Controller

    $items = ArrayHelper::map(Standard::find()->all(), 's_id', 'name');
    ...
    return $this->render('your_view',['model'=>$model, 'items'=>$items]);
    

    In View

    <?= Html::activeDropDownList($model, 's_id',$items) ?>
    
    0 讨论(0)
提交回复
热议问题