am123
am123

Reputation: 191

How to make default value get selected in yii dropdown list

I am using an yii dropDownList for creating a drop-down in my form. I want one value for example '78'=>'selected' gets default selected in drop-down .My drop down is

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

Can any one help me in doing this

Thanks ;)

Upvotes: 9

Views: 36481

Answers (3)

Coder anonymous
Coder anonymous

Reputation: 927

This can be done by passing this in your html options of dropdown..

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

and the value 78 will be selected.

for prompt one can do something like this:

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

check this for help. Thanks.

Upvotes: -1

Ashwani Panwar
Ashwani Panwar

Reputation: 4578

suppose you want to show drop down of menu_name from Menu model and selected value on the basses of parent_menu_id then you can use it like this

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

Upvotes: -1

arun
arun

Reputation: 3677

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

if its coming from database, use something like below (in case of list box or multiple 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));

More details about dropDownList : dropDownList() method

Upvotes: 22

Related Questions