Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make default value get selected in yii dropdown list

Tags:

yii

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 ;)

like image 573
am123 Avatar asked Dec 01 '12 06:12

am123


1 Answers

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

like image 51
arun Avatar answered Oct 31 '22 19:10

arun