How to make a dropdown in yii2?

How to make a dropdown in yii2 using activeform and model? Since all methods have been changed in yii2 , how is this done in the new?

+67
php drop-down-menu yii2
05 Feb '14 at 5:07
source share
11 answers

It looks 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 CHtml list data in Yii 1.1. [Please download the array data from your controller]

EDIT

Download 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) ?> 
+101
05 Feb '14 at 5:59
source share

You seem to have found your answer already, but since you mentioned the active form, I will make another contribution, even if it is slightly different.

 <?php $form = ActiveForm::begin(); echo $form->field($model, 'attribute') ->dropDownList( $items, // Flat array ('id'=>'label') ['prompt'=>''] // options ); ActiveForm::end(); ?> 
+82
Feb 05 '14 at
source share

There are some good solutions above, and mine is just a combination of the two (I came here to find a solution).

@ Sarvar Nishonboyev's solution is good because it supports creating an input form label and a help box for error messages.

I went with:

 <?php use yii\helpers\ArrayHelper; use app\models\Product; ?> <?= $form->field($model, 'parent_id') ->dropDownList( ArrayHelper::map(Product::find()->asArray()->all(), 'parent_id', 'name') ) ?> 

Again, full credit: @Sarvar Nishonboyev and @ippi

+48
Jun 05 '14 at 5:58
source share

There seem to be many good answers to this question. Therefore, I will try to give a detailed answer

active form and hard-coded 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 table db

we're going to use an ArrayHelper, so first add it to the namespace

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

ArrayHelper uses many functions that can be used to process map () arrays - this is the one we will use here this function helps to make a map (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 the 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 the active form, but the data from the db table

 <?php echo Html::activeDropDownList($model, 'filed_name',ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']); ?> 
+16
Sep 19 '15 at 6:37
source share

Check this out:

 use yii\helpers\ArrayHelper; // load classes use app\models\Course; ..... $dataList=ArrayHelper::map(Course::find()->asArray()->all(), 'id', 'name'); <?=$form->field($model, 'center_id')->dropDownList($dataList, ['prompt'=>'-Choose a Course-']) ?> 
+13
Apr 01 '14 at 5:27
source share

I might be wrong, but I think that SQL query from view is a bad idea

This is my way

In the controller

 $model = new SomeModel(); $items=ArrayHelper::map(TableName::find()->all(),'id','name'); return $this->render('view',['model'=>$model, 'items'=>$items]) 

And in the view

 <?= Html::activeDropDownList($model, 'item_id',$items) ?> 

Or using ActiveForm

 <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'item_id')->dropDownList($items) ?> <?php ActiveForm::end(); ?> 
+10
Jun 11 '15 at 16:53
source share
 <?= $form->field($model, 'attribute_name')->dropDownList( ArrayHelper::map(Table_name::find()->all(),'id','field_name'), ['prompt' => 'Select'] ) ?> 

This will help you ... Remember to use the class file in the header.

+8
May 09 '15 at 1:25
source share

In ActiveForm just use:

 <?= $form->field($model, 'state_id') ->dropDownList(['prompt' => '---- Select State ----']) ->label('State') ?> 
+5
Sep 12 '14 at 13:11
source share

We are talking about data generation, and therefore more correctly done from the model. Imagine if you ever wanted to change the way data is displayed in a drop-down list, say add a last name or something else. You will need to find each drop-down list and change arrayHelper . I use a function in my models to return data for a drop-down list, so I don't need to repeat the code in the views. This also has the advantage that I can specify a filter here and apply them to each drop-down list created from this model;

 /* Model Standard.php */ public function getDropdown(){ return ArrayHelper::map(self::find()->all(), 's_id', 'name')); } 

You can use this in your view file as follows:

 echo $form->field($model, 'attribute') ->dropDownList( $model->dropDown ); 
+5
Dec 16 '15 at 12:56
source share

If you have reached the bottom of the list. Save some php code and just return all the data from the database as you need it:

  $items = Standard::find()->select(['name'])->indexBy('s_id')->column(); 
0
Aug 18 '17 at 14:26
source share

The following can also be done. If you want to add a preview icon. It will be useful.

 <?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();?> 
-3
Mar 27 '14 at 12:28
source share



All Articles