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

前端 未结 3 1524
花落未央
花落未央 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:05

    Try this , i hope this will help you, instead of this kartik\grid\CheckboxColumn use this yii\grid\CheckboxColumn

    Step 1: create a button for multiple delete in index.php

    
    

    Step 2: In your index.php(same page) use this javascript

    registerJs(' 
    
        $(document).ready(function(){
        $(\'#MyButton\').click(function(){
    
            var HotId = $(\'#w4\').yiiGridView(\'getSelectedRows\');
              $.ajax({
                type: \'POST\',
                url : \'index.php?r=hotel/multiple-delete\',
                data : {row_id: HotId},
                success : function() {
                  $(this).closest(\'tr\').remove(); //or whatever html you use for displaying rows
                }
            });
    
        });
        });', \yii\web\View::POS_READY);
    
    ?>
    

    this jquery is used to get the value(id) of selected row

    Step 3: in controller of hotel (HotelController.php) create a action for multiple-delete

    public function actionMultipleDelete()
        {
            $pk = Yii::$app->request->post('row_id');
    
            foreach ($pk as $key => $value) 
            {
                $sql = "DELETE FROM hotel WHERE hotel_id = $value";
                $query = Yii::$app->db->createCommand($sql)->execute();
            }
    
            return $this->redirect(['index']);
    
        }
    

    Try this surely this will help you...

提交回复
热议问题