php 如何使用 Chtml::DropDownList()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17301927/
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
how to use Chtml::DropDownList()
提问by user2205196
i'm currently a newbie when it comes to the yii framework / php. I would like some assistance creating this Chtml::DropDownList.
我目前是 yii 框架/php 的新手。我需要一些帮助来创建这个 Chtml::DropDownList。
http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail
http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail
Chtml::dropDownList($name, $select, $data)
I understand that $data is the array of data I will be loading in from my Database.
我知道 $data 是我将从我的数据库加载的数据数组。
But can someone explain to me how $name, and $select truly works. I have a hard time finding documentation that explains this at a extremely dumbdown level.
但是有人可以向我解释 $name 和 $select 是如何真正起作用的。我很难找到在极其愚蠢的级别上解释这一点的文档。
I manage to get this piece of code working, but I would prefer using Chtml::dropdownlist.
我设法让这段代码工作,但我更喜欢使用 Chtml::dropdownlist。
<div class="row">
<?php
echo $form->dropDownList($model, 'id',
Chtml::listData(UsersTeam::model()->findAllByAttributes(array('coachId'=>$model->id)), 'id', 'teamName'),
array('empty'=>'Select Team'))
?>
</div>
I would like to be able to display all teamName for the current user that he is enlisted in.
我希望能够显示他入伍的当前用户的所有 teamName。
I'm currently displaying this in the model view of a User, but the information I need is from UserTeam which holds the teams for the users.
我目前正在用户的模型视图中显示它,但我需要的信息来自为用户保存团队的 UserTeam。
'memberOfTeams' => array(self::MANY_MANY, 'UsersTeam', '{{teamMembers}}(userId, teamId)'),
'coachOfTeams' => array(self::HAS_MANY, 'UsersTeam', 'coachId'),
回答by casraf
$name
is the name="mySelect"
form value it will have (the one that will be passed if sent as a form i.e. $_POST['mySelect']
).
$name
是name="mySelect"
它将具有的表单值(如果作为表单发送,将传递的值 ie $_POST['mySelect']
)。
$select
is the preselected ID. Say you have an array...
$select
是预选的 ID。假设你有一个数组...
$options = array('12' => 'Twelve', '10' => 'Ten');
And your dropdown looks like this...
你的下拉菜单看起来像这样......
echo CHtml::dropDownList('mySelect', '12', $options);
Then 'Twelve' will be the preselected item in the dropdown and $_POST['mySelect']
will be the value passed when the form is sent.
然后“十二”将是下拉列表中的预选项目,并将$_POST['mySelect']
是发送表单时传递的值。
You can add additional html options to each <option>
tag, using the fourth parameter CHtml::dropDownList
accepts, like so:
您可以<option>
使用第四个参数CHtml::dropDownList
accepts为每个标签添加额外的 html 选项,如下所示:
$htmlOptions = array(
// adds to the select element
'style' => 'cursor: pointer;',
// adds to the actual options
'options' => array(
'12' => array('title' => '12')
)
);
And updating the call to:
并将调用更新为:
echo CHtml::dropDownList('mySelect', '12', $options, $htmlOptions);
The finished list would look like this:
完成的列表将如下所示:
<select name="mySelect" style="cursor: pointer;">
<option value="12" selected="selected" title="12">Twelve</option>
<option value="10">Ten</option>
</select>
回答by dev1234
You can easily do the same with CHtml::activeDropDownList.
您可以使用 CHtml::activeDropDownList 轻松完成相同的操作。
So your code will look like
所以你的代码看起来像
<div class="row">
<?php
echo CHtml::activeDropDownList($model, 'id',
Chtml::listData(UsersTeam::model()->findAllByAttributes(array('coachId'=>$model->id)), 'id', 'teamName'),
array('empty'=>'Select Team'))
?>
</div>
Hope this helps you
希望这对你有帮助