当谈到yii framework / php时,我现在是个新手。我想要一些帮助来创建这个Chtml :: DropDownList。
http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail
Chtml::dropDownList($name, $select, $data)
据我所知,$ data是我将从我的数据库中加载的数据数组。
但有人可以向我解释$ name和$ select是否真的有效。我很难找到能够解决这个问题的文档。
我设法让这段代码工作,但我更喜欢使用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>
我希望能够显示他所加入的当前用户的所有teamName。
我目前正在用户的模型视图中显示此信息,但我需要的信息来自UserTeam,它为用户保留了团队。
'memberOfTeams' => array(self::MANY_MANY, 'UsersTeam', '{{teamMembers}}(userId, teamId)'),
'coachOfTeams' => array(self::HAS_MANY, 'UsersTeam', 'coachId'),
答案 0 :(得分:15)
$name
是它将拥有的name="mySelect"
表单值(如果以$_POST['mySelect']
形式发送,将会传递的值。
$select
是预先选择的ID。假设你有阵列......
$options = array('12' => 'Twelve', '10' => 'Ten');
你的下拉列表看起来像这样......
echo CHtml::dropDownList('mySelect', '12', $options);
然后'Twelve'将成为下拉列表中的预选项目,$_POST['mySelect']
将是表单发送时传递的值。
您可以使用第四个参数<option>
接受,为每个CHtml::dropDownList
标记添加其他html选项,如下所示:
$htmlOptions = array(
// adds to the select element
'style' => 'cursor: pointer;',
// adds to the actual options
'options' => array(
'12' => array('title' => '12')
)
);
并将通话更新为:
echo CHtml::dropDownList('mySelect', '12', $options, $htmlOptions);
完成的列表如下所示:
<select name="mySelect" style="cursor: pointer;">
<option value="12" selected="selected" title="12">Twelve</option>
<option value="10">Ten</option>
</select>
答案 1 :(得分:2)
您可以使用CHtml :: activeDropDownList轻松完成相同的工作。
所以你的代码看起来像
<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>
希望这有助于你