CheckBoxList默认选中全部yii

时间:2013-05-11 13:43:40

标签: yii checkboxlist

我正在使用checkBoxList -

CHtml::checkBoxList('Interests', $selectedInterests, CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all'));

我已经在使用该选项进行“全部检查”框,但我希望在用户第一次访问该页面时默认选中所有框。然后他们可以取消选中那些不适用的东西。

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

2 个答案:

答案 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有三个参数。

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

    $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