Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 dropdown selected value

I want to show selected value in Yii2 dropdown,

$_GET Value:

  $id = $_GET["cid"];

Drop down code

  $form->field($model, 'userid')
    ->dropDownList(
          [User::getUser()],
          //[ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name')],
          ['prompt'=>'Select a user','id'=>'user_dropdown'],    
          ['options' =>
                    [                        
                      $id => ['selected' => true]
                    ]
          ]

        )->label('');           

but this method is not working!

like image 929
Muhammad Shahzad Avatar asked Dec 20 '14 14:12

Muhammad Shahzad


People also ask

How to get value from dropDown in Yii2?

Show activity on this post. $form->field($model, 'userid') ->dropDownList( [User::getUser()], //[ArrayHelper::map(User::findAll(['active' => '1']), 'id', 'name')], ['prompt'=>'Select a user','id'=>'user_dropdown'], ['options' => [ $id => ['selected' => true] ] ] )->label(''); but this method is not working! Arah..

How can I show the selected value of DropDownList in Yii?

If you use $model as the model for the dropdownlist you'll save the ID of the selected value to the database. So then when you're going to update the record, the $model->prj_id will be set to the saved value, so that is the value it will display.


1 Answers

Try this.

$model->userid=$id;
$form->field($model, 'userid')
->dropDownList(...)
->label('');
like image 178
Nakarin Kongsumrit Avatar answered Sep 26 '22 01:09

Nakarin Kongsumrit