2

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

Richard perris
  • 511
  • 1
  • 6
  • 15

3 Answers3

5

This seems to be a little older question, but this is my approach:

[
    'class' => '\kartik\grid\CheckboxColumn',
    'checkboxOptions' => function($model) {
        if(!$model->status){
           return ['disabled' => true];
        }else{
           return [];
        }
     },
],

This would work even with default yii CheckboxColumn

Nimer
  • 357
  • 6
  • 12
4

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.

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
talki
  • 194
  • 1
  • 8
  • That works. Thank you. I am just finalising some amendments to the code including the completed javascript function. I will post below when done. Thank you. – Richard perris Mar 14 '17 at 13:22
1

My working code based on the answer by Tai Kitron.

added the below Grid col

'header'=>Html::checkbox('selection_all', false, ['class'=>'select-on-check-all', 'value'=>1,
            'onclick'=>'
                        $(".kv-row-checkbox").prop("checked", $(this).is(":checked"));
                        if($(".kv-row-checkbox").prop("checked") === true) $(".delete_ready").attr("class","delete_ready warning");
                        if($(".kv-row-checkbox").prop("checked") === false) $(".delete_ready").attr("class","delete_ready");


                        ']),
        'contentOptions'=>['class'=>'kv-row-select'],
        'content'=>function($model, $key){

            if(isset($model->stopDelete)&&($model->stopDelete!==1))
            return Html::checkbox('id[]', false, ['class'=>'kv-row-checkbox ',
                    'value'=>$key, 'onclick'=>'$(this).closest("tr").toggleClass("warning");', 'disabled'=> isset($model->stopDelete)&&($model->stopDelete===1)]);
            else
                return '';
            //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,
        'width'=>'50px'
    ],

added the following row options

'row_options'=>function($model){
            if(empty($model->stopDelete)){
                return ['class' => 'delete_ready'];
            }

used the following javascript //delete selected rows $(document).on('click', '#deleteSelected', function(){

        var table = $('#deleteSelected').attr('value');
        var keys = new Array();//[];//$('.deletSelectedItem:checked').val();
        var path = 'global/deleteselected';
        var current_page = location.pathname.substring(1);//window.location;

        var c = 0;
        $("input[name='id[]']:checked").each( function () {

                keys[c] = $(this).val();
                c++;
        });

        if( current_page.indexOf("index") >= 0){
            path = '../global/deleteselected';
        }


        if(keys.length ==0){

            krajeeDialog.alert("No Recorlds Selected")

        }
        else {

            if(keys.length ==1)
                var s = '';
            else
                var s ='s';

            krajeeDialog.confirm("Are you sure you want to delete the selected "+keys.length+" recorld"+s+"?", function (result) {

                if (result) {

                    $.post({
                        url: path,
                        data: {keylist: keys, table:table},
                        success: function (data) {

                            if( current_page.indexOf("index") >= 0)
                                $.pjax.reload({container: "#"+table});
                            else
                                $.pjax.reload({container: "#"+table, url: table});


                        }, error: function (xhr, status, error) {
                            alert(status + error);// check status && error
                        },
                    });

                }
            });
        }


});

Worked fine. Thanks for your help Tai Kitron

Richard perris
  • 511
  • 1
  • 6
  • 15