Last updated on February 10, 2018
In this post, I would like to show you how to create a drop-down list in yii2. This tutorial will help you to display the array data or model data in the drop-down list. I am using activeForm.
Drop down list syntax
echo $form->field($model, 'name_field')->dropDownList( [items], [options] );
Drop down list with Array
field($model, 'name')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?>
Drop down list with prompt
$listData = ['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'];field($model, 'name')->dropDownList($listData, ['prompt'=>'Select item']);>
Drop down list With table Model Data
= Html::activeDropDownList($model, 'name', ArrayHelper::map(Items::find()->all(), 'item_id', 'item_name')) ?>