我正在使用checkBoxList -
CHtml::checkBoxList('Interests', $selectedInterests, CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all'));
我已经在使用该选项进行“全部检查”框,但我希望在用户第一次访问该页面时默认选中所有框。然后他们可以取消选中那些不适用的东西。
当用户第一次使用Yii的复选框列表进入页面时,如何默认选中所有框?
答案 0 :(得分:3)
您应该将$ selectedInterests中checkBoxList的所有值作为数组传递。 我现在无法测试它,但可能这应该有效:
CHtml::checkBoxList('Interests', CHtml::listData($interests, 'interest_id','interest_id'), CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all'));
看一下该方法的源代码,你应该在这一行中返回true:
$checked=!is_array($select) && !strcmp($value,$select) || is_array($select) && in_array($value,$select);
其中$ select是您的$ selectedInterest,而您的案例中的$ value是您的'interest_id'属性。
答案 1 :(得分:1)
checkBoxList
有三个参数。
示例代码:
$books = CHtml::listData(Book::model()->findAll(), 'id', 'name');
$selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);
我的博客文章中提供了更多详细信息: 的 How to make Yii checkBoxList selected 强>