Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 dropdownList select default option

I am returning cat_id value by GET in url to say that my dropdown list, which Item must be select. But it's not working.

<?= $form->field($model, 'cat_id')->dropDownList(
ArrayHelper::map(DeviceCats::find()
->where(['is_deleted' => 'no'])->all(),'id','title')
,['options' => [$_GET['cat_id'] => ['selected'=>true]]
, 'prompt' => ' -- Select Category --']) ?>
like image 390
Mojtaba Avatar asked Jun 28 '15 21:06

Mojtaba


2 Answers

Finally solved with an unbelievable change. Just changed the first letter of selected to capital ('selected' should be 'Selected'). Here is the code:

<?= $form->field($model, 'cat_id')->dropDownList(
ArrayHelper::map(DeviceCats::find()
->where(['is_deleted' => 'no'])->all(),'id','title')
,['options' => [$_GET['cat_id'] => ['Selected'=>'selected']]
, 'prompt' => ' -- Select Category --']) ?>
like image 81
Mojtaba Avatar answered Oct 23 '22 06:10

Mojtaba


'Selected' must be written with a capital letter 'S':

'options'=>['72'=>['Selected'=>true]]
like image 45
sylvain Avatar answered Oct 23 '22 05:10

sylvain