php Yii 下拉列表空值作为默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16057637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii Dropdown List Empty Value as Default
提问by groovekiller
I have a dropdownlist in my _form model and i want to add a empty value (wich I want as default). I have the following: In _form:
我的 _form 模型中有一个下拉列表,我想添加一个空值(我想要作为默认值)。我有以下内容:在_form中:
<?php echo $form->labelEx($model,'country_id'); ?>
<?php echo $form->dropDownList($model,'country_id',Country::items(),array('empty' => '--Select a country--')); ?>
<?php echo $form->error($model,'country_id'); ?>
In Model Country:
在示范国家:
public static function items()
{
return CHtml::listData(Country::model()->findAllBySql(
'SELECT * from country'),
'id', 'name');
}
Even my empty option is in the first row in dropdownlist, the 1st country in list shows as default.
即使我的空选项也在下拉列表的第一行,列表中的第一个国家显示为默认值。
I tried:
我试过:
<?php echo $form->dropDownList($model,'country_id',
Country::items(),array('empty'=>'--Select a country--',
'options'=>
array(
'3'=>array('selected'=>'selected')
)
));
?>
In this way i can choose the default option, but cant set it to empty value, just countries that came from model:items.
通过这种方式,我可以选择默认选项,但不能将其设置为空值,只能将来自模型:项目的国家/地区设置为空值。
Any idea?
任何的想法?
回答by ezze
Are you sure that country_id
property of your model is not set to anything when you print the dropdown list? The following works for me if $model
instance is created using new Country()
operator but not by populating properties from database:
您确定country_id
打印下拉列表时模型的属性没有设置为任何内容吗?如果$model
使用new Country()
运算符创建实例而不是通过从数据库填充属性,则以下对我有用:
<?php echo $form->dropDownList(
$model,
'country_id',
Country::items(),
array(
'empty'=>'--Select a country--')
);
?>
回答by Bogdan Burym
Read documentation. There is 'prompt' parameter.
阅读文档。有“提示”参数。
Try this:
尝试这个:
<?php
echo $form->dropDownList($model,'country_id',Country::items(), array(
'prompt' => '--Select a country--'
));
?>
See more details here http://www.yiiframework.com/forum/index.php/topic/11195-how-to-edit-the-default-option-in-dropdownlist/
回答by Soyale
You always can do something like array_merge in your items
method
你总是可以在你的items
方法中做类似 array_merge 的事情
public static function items()
{
return array_merge(array(''=>'--Select a country--'), CHtml::listData(Country::model()->findAllBySql(
'SELECT * from country'),
'id', 'name'));
}
回答by D.Mill
I believe youre looking for:
我相信你正在寻找:
echo $form->dropDownList($model,'country_id',Country::items(),array('prompt'=>''));
回答by Sonef
if you use yiibooster maybe this will help
如果你使用 yiibooster 也许这会有所帮助
<?php echo $form->dropDownListGroup(
$model,
'kode_cuti_sub2',
array(
'empty'=>'--Select a country--',
'widgetOptions' => array(
'data' => array('Something ...', 'Pilih Jenis Cuti'=>Chtml::listData(Cuti::model()->cuti_sub2(),'kode','jenis_cuti')),
'options' => array(
'placeholder' => 'Pilih NIP Pegawai',
'width' => '100%',
),
),
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
)
); ?>
in my case it's worked
就我而言,它有效