批量删除
参考:开发者工具 - 错误日志处理
勾选指定行,批量删除
view视图文件:
PHP Code复制内容到剪贴板
- <?= GridView::widget([
- 'dataProvider' => $dataProvider,
- 'options' => ['id' => 'grid'], //'class' => 'table table-bordered table-hover table-responsive',
- 'columns' => [
- [
- 'class' => 'yii\grid\CheckboxColumn',
- 'name' => 'id',
- ],
- 'id',
- ...
- ['class' => 'yii\grid\ActionColumn'],
- ],
- ]); ?>
添加一列删除checkbox框,再添加一个删除按钮:
PHP Code复制内容到剪贴板
- <?= Html::button('批量删除', ['class' => 'btn btn-danger deleteAll']) ?>
在页面底部 添加js,传输的id要与删除的属性一致:
PHP Code复制内容到剪贴板
- <?php $this->beginBlock('js') ?>
- <script>
- $(".deleteAll").on("click", function () {
- var keys = $("#grid").yiiGridView("getSelectedRows");
- if (!keys.length) {
- layer.msg("请先勾选要删除的信息");
- return false;
- }
- layer.confirm('是否确定删除?', {
- btn: ['确定', '再想想'] //可以无限个按钮
- }, function(index, layero){
- //按钮【确定】的回调
- layer.load(2);
- $.post("<?=Url::to(['delete-select'])?>", {"id": keys},
- function (ret) {
- layer.closeAll("loading");
- if (ret.errcode == 0) {
- layer.msg(ret.errmsg, {
- time: 1000 //1秒后刷新
- }, function(){
- document.location.reload();
- });
- } else {
- layer.open({
- type: 1,
- content: ret.errmsg
- });
- }
- }, "json");
- }, function(index){
- //按钮【再想想】的回调
- });
- });
- </script>
- <?php $this->endBlock() ?>
delete-select方法在controller中作统一action:
PHP Code复制内容到剪贴板
- public function actions()
- {
- return [
- 'delete-select' => [
- 'class' => 'backend\\actions\\Delete',
- 'attribute' => 'id', // 根据什么字段属性删除
- 'entity' => SystemLog::className()
- ],
- ];
- }
清空数据表,在控制器中添加:
PHP Code复制内容到剪贴板
- public function actions()
- {
- return [
- // 删除指定ID
- 'delete-select' => [
- 'class' => 'backend\\actions\\Delete',
- 'attribute' => 'id', // 根据什么字段属性删除
- 'entity' => AdminLog::className()
- ],
- // 删除全部
- 'delete-all' => [
- 'class' => 'backend\\actions\\DeleteAll',
- 'entity' => AdminLog::tableName()
- ],
- ];
- }
视图文件中,随便找个地方:
写法一:
PHP Code复制内容到剪贴板
- <?= Html::a('清空日志', Url::to(['delete-all']), [
- 'class' => 'btn btn-danger',
- 'data' => [
- 'ajax' => 1,
- 'confirm' => '是否确认清空,该操作不可恢复?',
- 'method' => 'post',
- 'refresh' => 1,
- ],
- ]) ?>
写法二:
PHP Code复制内容到剪贴板
- <?= Html::a('清空日志', Url::to(['delete-all']), [
- 'class' => 'btn btn-danger',
- 'data-ajax' => 1,
- 'data-confirm' => '是否确认清空,该操作不可恢复?',
- 'data-method' => 'post',
- 'data-refresh' => 1,
- ]) ?>
清空指定数据表的指定项目的数据
控制器:
PHP Code复制内容到剪贴板
- public function actions()
- {
- return [
- // 当前项目下的部门
- 'clear' => [
- 'class' => 'backend\\actions\\DeleteByProject',
- 'entity' => Department::className(),
- ]
- ];
- }
视图:
PHP Code复制内容到剪贴板
- <?= Html::a('清空部门信息', Url::to(['clear']), [
- 'class' => 'btn btn-danger',
- 'data-ajax' => 1,
- 'data-confirm' => '是否确认清空,该操作不可恢复?',
- 'data-method' => 'post',
- 'data-refresh' => 1,
- ]) ?>
上一篇 图标选择插件
下一篇 a标签删除/提交【ajax】