Search code examples
checkboxyii2kartik-v

yii2 \kartik\grid\CheckboxColumn


I have added a check box column in my yii2 grid and created a button that can delete all selected values. However in my action col i have made it possible that the delete icon only shows dependant as follows.

'delete' => function ($model, $key, $index) {

/* add public function in model i,e GetstopDelete*/

if(empty($model->stopDelete))
return true;
else
return $model->stopDelete == 1 ? false : true;
}


},

BUT, how to i hide the cell of the check box based on same parameters as I do not want the check box available if you cannot delete the row?

I am using http://demos.krajee.com/grid#checkbox-column and http://www.yiiframework.com/doc-2.0/yii-grid-checkboxcolumn.html


Solution

  • You could perform a trick such as using a normal column and drawing the checkbox + events yourself. It would look like:

    [
        'header'=>Html::checkbox('selection_all', false, ['class'=>'select-on-check-all', 'value'=>1, 'onclick'=>'$(".kv-row-checkbox").prop("checked", $(this).is(":checked"));']),
        'contentOptions'=>['class'=>'kv-row-select'],
        'content'=>function($model, $key){
            return Html::checkbox('selection[]', false, ['class'=>'kv-row-checkbox', 'value'=>$key, 'onclick'=>'$(this).closest("tr").toggleClass("danger");', 'disabled'=> isset($model->stopDelete)&&!($model->stopDelete===1)]);
        },
        'hAlign'=>'center',
        'vAlign'=>'middle',
        'hiddenFromExport'=>true,
        'mergeHeader'=>true,
    ],
    

    This way you have more control over the behavior.