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,
        ),

    ),
)); ?>
Était-ce utile?

La 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.

Autres conseils

Try this:

array(
            'id'=>'id',
            'class'=>'CCheckBoxColumn',
            'selectableRows' => '50',   
            'checked'=>'($data->id==$current_reviewers)?(1):(0)',   

        ),
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top