Delete multiple rows selected by checkbox column

2019-08-03 04:17发布

问题:

I refer many questions on stackoverflow like Yii 2 : how to bulk delete data in kartik grid view? . but dont know why it is not working.

here is code: index.php

     <input type="button" class="btn btn-info" value="Multiple Delete" id="MyButton" >
....
        echo GridView::widget([
                        'tableOptions' => ['id' => 'companies_grid_table',
                        'class' => 'table table-striped table-bordered table-hover'],
                        'id'=> 'companies_grid_display',
                        'dataProvider' => $dataProvider,
                        'filterModel' => $searchModel,
                        'columns' => $gridColumns,             
                    ]); 
     .....

        <script type="text/javascript">
            <?php 

            $this->registerJs(' 

            $(document).ready(function(){
            $(\'#MyButton\').click(function(){

                var CompId = $(\'#companies_grid_display\').yiiGridView(\'getSelectedRows\');
                  $.ajax({
                    type: \'POST\',
                    url : \'index.php?r=companies/multiple-delete\',
                    data : {row_id: CompId},
                    success : function() {
                      $(this).closest(\'tr\').remove(); //or whatever html you use for displaying rows
                    }
                });

            });
            });', \yii\web\View::POS_READY);

        ?>
        </script>

controller

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

how to delete multiple rows from gridview?

标签: gridview yii2