复选框列表默认选中全部yii

复选框列表默认选中全部yii,yii,checkboxlist,Yii,Checkboxlist,我是这样使用复选框列表的- CHtml::checkBoxList('Interests', $selectedInterests, CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all')); 我已经在使用“全选”框,但是我希望在用户第一次访问页面时默认选中所有框。然后,他们可以取消选中那些不适用的 当用户第一次使用Yii

我是这样使用复选框列表的-

CHtml::checkBoxList('Interests', $selectedInterests, CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all'));
我已经在使用“全选”框,但是我希望在用户第一次访问页面时默认选中所有框。然后,他们可以取消选中那些不适用的


当用户第一次使用Yii的checkboxlist进入页面时,默认情况下如何选中所有复选框?

您应该将$selectedInterests中的所有复选框值作为数组传递。 我现在无法测试它,但这可能会起作用:

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\u id'属性。

您应该将$selectedInterest中复选框列表的所有值作为数组传递。 我现在无法测试它,但这可能会起作用:

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是您的每个“兴趣id”属性。

复选框列表有三个参数

第一个参数是字段名 第二个参数是所选关键帧的数组。此参数使复选框列表处于选中状态。 第三个参数是选项数组 示例代码:

$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);
更多详细信息可在我的博客帖子中找到: 复选框列表有三个参数

第一个参数是字段名 第二个参数是所选关键帧的数组。此参数使复选框列表处于选中状态。 第三个参数是选项数组 示例代码:

$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);
更多详细信息可在我的博客帖子中找到: