1. 给GridView取个ID
<?= GridView::widget([ 'dataProvider' => $dataProvider, 'options' => ['id' => 'grid', 'class' => 'grid-view table-scrollable'], ....
2.GridView的第一列:
[ 'class' => 'yiigridCheckboxColumn', '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); }