Reputation: 2642
I'm using checkBoxList like so-
CHtml::checkBoxList('Interests', $selectedInterests, CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all'));
I'm already utilising the option to have a 'check all' box, but I'd like all boxes to be checked by default when the user first comes to the page. They can then unCheck those that don't apply.
How can I have all boxes checked by default when the user first comes to the page using Yii's checkboxlist?
Upvotes: 2
Views: 5444
Reputation: 11699
checkBoxList
have three parameters.
Sample code:
$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);
More details are available in my blog post : How to make Yii checkBoxList selected
Upvotes: 1
Reputation: 388
You should pass all the values of your checkBoxList in $selectedInterests as array. I can't test it now but probably this should work:
CHtml::checkBoxList('Interests', CHtml::listData($interests, 'interest_id','interest_id'), CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all'));
Taking a look at the source code of that method, you should return true in this line:
$checked=!is_array($select) && !strcmp($value,$select) || is_array($select) && in_array($value,$select);
Where $select is your $selectedInterest, and $value in your case are each of your 'interest_id' attribute.
Upvotes: 3