1. 程式人生 > >YII2 Gridview 批量刪除

YII2 Gridview 批量刪除

this dir his div get html table gets oid

1. 給GridView取個ID

 <?= GridView::widget([
                ‘dataProvider‘ => $dataProvider, 
                ‘options‘ => [‘id‘ => ‘grid, ‘class‘ => ‘grid-view table-scrollable‘],

....

2.GridView的第一列:

        [
            ‘class‘ => ‘yii\grid\CheckboxColumn‘,
            ‘name‘ => ‘id‘,
        ]
,

3.添加一個批量刪除的按鈕(class=‘..gridview‘ 增加這個gridview是為了能夠被js使用):

<?= Html::a(‘<i class="fa fa-close"></i> 批量刪除‘, "javascript:void(0);", [‘class‘ => ‘btn btn-sm btn-warning gridview‘]) ?>

4.寫客戶端JS:

    $this->registerJs(‘
    $(".gridview").on("click", function () {
        var keys = $("#grid
").yiiGridView("getSelectedRows");     if (keys.length>0){     $.post("delete-all?id="+ (keys));     }     });   
‘);

5: controller裏delete-all方法:

        public function actionDeleteAll($id)
        {
            $model = new User(); //你要批量刪除的表
            $model->deleteAll(‘id in (‘ . $id
. ‘)‘); return $this->redirect(Yii::$app->request->referrer); }

YII2 Gridview 批量刪除