How to make the default value selected in the yii drop-down list

I am using yii dropDownList to create a dropdown in my form. I want a single value, for example '78'=>'selected' , to be selected by default in the drop-down list. My picture is

 dropDownList($testcontroller,'testid',CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)), 'id', 'phaseName')); 

Can anyone help me with this,

Thanks;)

+8
yii
source share
3 answers
 dropDownList($testcontroller, 'testid', CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)), 'id', 'phaseName'), array('options' => array('78'=>array('selected'=>true)))); 

if it comes from the database, use something like below (in the case of a list or multiple dropDownList allowed dropDownList use multiple=>true )

 foreach ($selections as $eachValue) $selectedOptions[$eachValue] = array('selected'=>'selected'); echo $form->dropDownList($model, 'testid', CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)), 'id', 'phaseName'), array('multiple'=>'true','prompt'=>'select ','options'=>$selectedOptions)); 

Learn more about dropDownList: dropDownList () method

+19
source share

This can be done by passing this in your html options for the drop down list.

dropDownList($testcontroller, 'testid', CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)), 'id', 'phaseName'), array('options' => array('78'=>array('selected'=>true))));

and 78 will be selected.

for a hint, you can do something like this:

dropDownList($testcontroller,'testid', CHtml::listData(Phases::model()->findAll(), 'id', 'phasename'), array('empty'=>'Select an option'));

check this out for help . Thanks.

-one
source share

Suppose you want to show the menu_name of menu_name from the Menu model and the selected value on the bass parent_menu_id , then you can use it as

  <?php echo $form->dropDownList($model,'parent_menu_id', CHtml::listData(Menu::model()->findAll(), 'menu_id', 'menu_name'), array('empty'=>'--please select--')); ?> 
-one
source share

All Articles