我在Yii
中有dropdown
选项,就像这个一样
<?php echo $form->dropdownList($students,'student_name', CHtml::listData(Students::model()->findAll(), 'student_name', 'student_name'), array('selected'=>'Choose One')); ?>
哪个html output
类似于这个
<select selected="selected" name="Students[student_name]" id="Students_student_name">
<option value="Alex">Alex</option>
<option value="John">John</option>
<option value="Johny">Johny</option>
<option value="" selected="selected"></option>
</select>
但我希望Select One
应该是default selected value for the dropdown options
。因此,在没有选择任何选项的情况下,默认情况下会有Select One
。
[更新]
当我也尝试array('prompt'=>'Choose One')
和array('empty'=>'Choose One')
时,它将Select One
作为一个选项。
我认为您对默认选定值使用了错误的键。
尝试:
// array('prompt'=>'Choose One')
echo $form->dropdownList($students,'student_name', CHtml::listData(Students::model()->findAll(), 'student_name', 'student_name'), array('prompt'=>'Choose One')); ?>
使用名为empty
的属性
echo $form->dropdownList(
$students,
'student_name',
CHtml::listData(Students::model()->findAll(), 'student_name', 'student_name'),
array('empty'=>'Choose One') // boom!
);
当需要这样做时,我只需将其添加到listData生成的数组中。
<?php echo $form->dropdownList($students,'student_name', array(''=>'Select One') + CHtml::listData(Students::model()->findAll(), 'student_name', 'student_name')); ?>
对我来说一直都很好。