Yii 2 : how to bulk delete data in kartik grid view?

前端 未结 3 1530
花落未央
花落未央 2020-12-15 12:53

Kartik grid view in yii2 provides an option to display checkboxes in grid view. How do I delete bulk data by checking the checkboxes? Any help would be greatful. Here is my

3条回答
  •  被撕碎了的回忆
    2020-12-15 13:12

    1. Add custom class or id to pjax container

    Either add class or id to your pjax container with GridView, so you are not depending from auto generated classes and ids (or in case you have multiple GridView widgets in one page).

    kartik\grid\CheckboxColumn is just extended version of yii\grid\CheckboxColumn.

    kartik\grid\View has containerOptions, you can specify class here, it seems like id is auto generated and can not be changed using this property.

    'containerOptions' => ['class' => 'hotel-pjax-container'],
    

    Example of generated output:

    ...

    yii\grid\View\ has options, you can specify id here. The result container id will be prefixed with passed value, for example:

    'options' => ['id' => 'hotel-pjax'],
    

    Generated output:

    ...

    Class is ignored in this case.

    I recommend specifying id.

    2. Create or modify action for deletion in controller

    By default delete action auto generated with gii has redirect, so we can create another action for multiple deletion (or you can handle this in one, it's up to you).

    public function actionDeleteMultiple()
    {
        $pk = Yii::$app->request->post('pk'); // Array or selected records primary keys
    
        // Preventing extra unnecessary query
        if (!$pk) {
            return;
        }
    
        return Hotel::deleteAll(['hotel_id' => $pk]);
    }
    

    Note that if you didn't specify any condition in deleteAll(), all records in the table will be deleted! Be accurate with that.

    You can also specify the action in VerbFilter like this:

    use yii\filters\VerbFilter;
    
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                    'delete-multiple' => ['post'],
                ],
            ],
        ];
    }
    

    3. Write some javascript to tie all together

    You can get primary keys of selected rows like this:

    $('#hotel-pjax-container').yiiGridView('getSelectedRows');
    

    Add this javascript (to the button click for example):

    $.post(
        "delete-multiple", 
        {
            pk : $('#hotel-pjax-container').yiiGridView('getSelectedRows')
        },
        function () {
            $.pjax.reload({container:'#hotel-pjax-container'});
        }
    );
    

    You can find more information about updating the GridView with pjax in this issue. Maybe try this: $('#hotel-pjax-container').yiiGridView('applyFilter'); as alternative; Include js using assets or just with registerJs();

提交回复
热议问题