V2.0 - 批量操作/批量勾选
一、批量操作
1、view视图页面,加id,加checkbox
PHP Code复制内容到剪贴板
- <?= \yii\grid\GridView::widget([
- 'dataProvider' => $dataProvider,
- 'options' => ['id' => 'orgGridView'],
- 'columns' => [
- [
- 'class' => 'yii\grid\CheckboxColumn',
- 'name' => 'id',
- ],
- ...
- ]);
- ?>
2、加按钮,和JS事件
PHP Code复制内容到剪贴板
- <?= Html::button('通知年检', ['class' => 'btn btn-danger ml-20 noticeAll']) ?>
3、JS:
PHP Code复制内容到剪贴板
- <?php $this->beginBlock('js') ?>
- <script>
- $(".noticeAll").on("click", function () {
- var keys = $("#orgGridView").yiiGridView("getSelectedRows");
- if (!keys.length) {
- layer.msg("请先勾选需通知的社会组织");
- return false;
- }
- layer.confirm('是否确定发送短信通知?', {
- title:'询问',
- icon:3,
- btn: ['确定', '再想想'] //可以无限个按钮
- }, function(index, layero){
- //按钮【确定】的回调
- $.modal.loading();
- $.post("<?=Url::to(['send-msg-to-org'])?>", {"ids": keys},
- function (res) {
- $.modal.unloading();
- if (res.code != undefined && res.code != '200' && res.code != '201') {
- layer.open({
- title: '错误'
- ,icon:"2"
- ,content: res.message || '操作失败'
- });
- return;
- }
- layer.msg(res.message, {
- time: 1000 //1秒后刷新
- }, function(){
- document.location.reload();
- });
- }, "json");
- }, function(index){
- //按钮【再想想】的回调
- });
- });
- </script>
- <?php $this->endBlock() ?>
二、批量删除
视图文件,ID是主键,且是唯一值
XML/HTML Code复制内容到剪贴板
- <?= Html::button('批量删除', ['class' => 'btn btn-danger ml-20 deleteAll']) ?>
PHP Code复制内容到剪贴板
- <?= GridView::widget([
- 'dataProvider' => $dataProvider,
- 'filterModel' => $searchModel,
- 'options' => [
- 'id' => 'timuGridView'
- ], //'class' => 'table table-bordered table-hover table-responsive',
- 'columns' => [
- [
- 'class' => 'yii\grid\CheckboxColumn',
- 'name' => 'id',
- ],
- 'id',
- //...,
- ],
- ]); ?>
XML/HTML Code复制内容到剪贴板
- <?php $this->beginBlock('js') ?>
- <script>
- $(".deleteAll").on("click", function () {
- // console.log('按钮点击事件')
- var keys = $("#timuGridView").yiiGridView("getSelectedRows");
- if (!keys.length) {
- layer.msg("请先勾选需删除的题目");
- return false;
- }
- layer.confirm('是否确定全部删除?', {
- title:'询问',
- icon:3,
- btn: ['确定', '再想想'] //可以无限个按钮
- }, function(index, layero){
- //按钮【确定】的回调
- $.modal.loading();
- $.post("<?=\yii\helpers\Url::to(['delete-select'])?>", {"id": keys},
- function (ret) {
- $.modal.unloading();
- if (ret.code == '200' || ret.code == '201') {
- layer.open({
- icon: 1,
- title: '成功',
- content: ret.message,
- // btn: ['登录', '我知道了'],
- yes:function(index){ //另一个应该是no:function
- document.location.reload();
- }
- });
- }else{
- layer.open({
- title: '错误'
- , icon: "2"
- , content: ret.message || '操作失败'
- });
- }
- }, "json");
- }, function(index){
- //按钮【再想想】的回调
- });
- });
- </script>
- <?php $this->endBlock() ?>
控制器添加(Delect action被按钮组的delete占用,所以这里更名为DeleteBatch):
PHP Code复制内容到剪贴板
- public function actions()
- {
- return [
- // 删除指定ID
- 'delete-select' => [
- 'class' => 'backend\\actions\\DeleteBatch',
- 'attribute' => 'id', // 根据什么字段属性删除
- 'entity' => Timu::class // 这里注意是模型的classname
- ],
- ];
- }
更多用法参照:批量删除
上一篇 V2.0 - 阿里云短信
下一篇 V2.0 - 工作流使用教程