Yii2 CheckboxColumn

0

Add a checkbox in the datagrid columns to be able to delete several records together, everything works fine. What I am looking for is that when selecting the rows to be deleted, they are painted in one color. Is there any way to do it?. I'm working with Yii2. Thanks.

<?= GridView::widget([
        'id' => 'grid',
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'layout' => '{items}{pager}{summary}',
        'pager' => [
            'options' => ['class' => 'pagination float-right'], // clase para el elemento <ul> de la paginacion
            'linkOptions' => ['class' => 'page-link'], // clase para los links <a href> de la paginacion
            'linkContainerOptions' => ['class' => 'page-item'], //clase para los elementos <li> de la paginacion
            'disabledListItemSubTagOptions' => ['class' => 'page-link'], // clase para los span que tienen flechas
        ],
        'columns' => [
            [ 
                'class' => \common\components\CheckboxColumnCustom::className () ,
                'content' => function($model) {
                    return  Html::checkBox ( 'selection[]' , false , [ 'id' => "customCheck" . $model->id, 'value' => $model->id] ) . '<label for="customCheck' . $model->id . '"></label>';
                } ,
                'contentOptions' => function($model, $key, $index, $column) {
                    return [
                        'class' => 'md-checkbox' ,
                    ];
                } ,
                'headerOptions' => ['class'=>'md-checkbox'],
                'header' => Html::checkBox ( 'selection_all' , false , [ 'id' => 'customCheck', 'class' => 'select-on-check-all' ] ) . '<label for="customCheck" class="header"></label>'   
            ] ,
            'tela',
            'descripcion',
            'nom_proveedor',
            'composicion',            
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => Helper::filterActionColumn('{view} {delete}'),
                'buttons' => [
                    'delete' => function ($url) {
                        return Html::a("<button class='btn btn-danger p-2'><i class='fa fa-trash text-center'></i></button>", $url, [
                            'title' => 'Eliminar',
                            'class' => 'delete',
                        ]);
                    }, //fin delete
                    'view' => function ($url) {
                        return Html::a(Yii::t('yii', "<button class='btn btn-warning p-2'><i class='fas fa-palette text-center'></i></button>"), $url, [
                            'title' => Yii::t('yii', 'Colores'),
                            'class' => 'view',
                        ]);
                    }, //fin view
                ] // fin buttons
            ],
        ],
    ]); 
    ?>
    
asked by Pablo JS 05.10.2018 в 20:42
source

1 answer

0

This can be resolved in the view where you have the GridView.

You just have to add the JavaScript or jQuery code to control the checkboxes when they change.

For example:

<?php
$script = <<<JS
$(function(){
    var checks = $("input[type='checkbox']"); // Obtengo todos los checkbox

    checks.change(function(){
        var trObject = $(this).parent().parent(); // tr que contiene el check
        var check = $(this); // check
        if(check.prop('checked')){
            trObject.css({background:'red'});
        }else{
            trObject.css({background:'none'});
        }
    });
});
JS;

$this->registerJs($script); // Registro el script javascript en el view 
?>

I hope you find it useful.

    
answered by 19.10.2018 / 01:59
source