How to checked some checkbox at cgridview?
-
13-07-2021 - |
Question
Q : how to checked some checkbox at cgridview?
Status : I've done a gridview with checkbox. but I don't know how to pre-check some check box. $current_reviewers is an array. I would like to match with $current_reviewers and checkbox to pre-check at gridview.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'acc-recei-grid',
'dataProvider'=>$model->search_reviewerlist(),
'filter'=>$model,
'columns'=>array(
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => 2,
'checkBoxHtmlOptions' => array(
'name' => 'userids[]',
),
'value'=>'$data->id',
'checked'=>'(in_array($data->id, $current_reviewers) ? 1 : ""',
),
'username',
array(
'type'=>'raw',
'value'=>'$data->id',
//'filter'=>array('style'=>'visible:none'),
//'headerHtmlOptions'=>array('style'=>'width:0px; display:none; border:none; textdecoration:none'),
'htmlOptions'=>array('style'=>'display:none; border:none;', 'class'=>'user-id'),
//'header'=>false,
//'filter'=>false,
),
),
)); ?>
Solution
The problem lies in the $current_reviewers
variable, it is not accessible within the php expression that is passed as the checked
value. For this you can use an anonymous function and to use the outside variable, use the use
keyword:
'checked'=>function($data, $row) use ($current_reviewers){
return in_array($data->id, $current_reviewers);
}
Check the usage of use
keyword.
OTHER TIPS
Try this:
array(
'id'=>'id',
'class'=>'CCheckBoxColumn',
'selectableRows' => '50',
'checked'=>'($data->id==$current_reviewers)?(1):(0)',
),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow