2012-12-01 38 views
8

我正在使用yii dropDownList在我的表单中创建下拉列表。我想例如'78'=>'selected'一个值被下拉默认选中的。我的下拉是如何在yii下拉列表中选择默认值

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

任何一个可以帮助我在做这个

感谢;)

回答

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

如果从数据库中来,使用类似如下(在列表框或多个的情况下,允许dropDownList使用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)); 

更多DROPDOWNLIST细节:dropDownList() method

+0

这就是我在寻找的谢意;) – am123

+0

它正在为简单的dropDownList工作,但不适用于多个dropDownList。 – israr

-1

这可以这样做在下拉菜单中的HTML选项通过这个..

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

和值78将被选中。对于help

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

检查:

的提示可以做这样的事情。谢谢。

+4

它将产生'<选项值= “”> 78' 不'<选项值= “78”> ABCDEFG' –

+0

更正了答案 –

-1

假设您想要显示从Menu model下拉menu_name和对parent_menu_id贝司选定值,然后就可以使用像这样

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