tp5实现后台模块全选删除
技术栈:ajax layer.js tp5
一个让你想到即可做到的web弹窗/层 解决方案 http://layer.layui.com/
实现复选框全选
1 2
| <th class="text-center"><input type="checkbox" class="all" style="!important; opacity: 1.2;position: static;"> </th>
|
1 2 3 4 5 6 7
| <form class="piliangshanchan"> //为了实现ajax 序列化表单的值 <td class="text-center"><input type="checkbox" value="{$value.id}" name="id[]" id="checkbox" class="cheidbox" style="!important; opacity: 1.2;position: static;"> </td> </form>
|
1 2 3
| <button type="submit" tooltip="删除管理员" class="btn btn-danger btn-sm shi" onclick="delAll()"><i class="fa fa-trash-o"></i> 批量删除 </button>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $('#all').change(function () { if ($(this).is(':checked')) { $(':checkbox').prop("checked", true); } else { $(':checkbox').prop("checked", false); } });
$(".cheidbox").change(function(){ var len=$(".cheidbox").length; var len2=$("input.cheidbox:checked").length; if(len==len2){ $('#all').prop("checked",true); }else{ $('#all').prop("checked",false); }
});
|
发送ajax请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| function delAll() { layer.confirm('确认要删除吗?', function (index) { if ($('.cheidbox').is(':checked')) { $.ajax({ url: "{:url('admin/delAll')}", data: $('.piliangshanchan').serialize(), dataType: "json", type: 'post', async: false, success: function (res) { if (res.status == 1) { layer.alert(res.message + ",共删除" + res.num + "数据", {icon: 6}, function () { window.location.reload(); var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index);
}); } else { layer.alert(res.message, {icon:1}, function () { window.location.reload(); var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index);
}); } }
}) } else { layer.msg('请选择~~', {icon: 2});
} }); }
|
后台控制器
1 2 3 4 5 6 7 8 9 10 11 12 13
| public function delAll() { $data = $this->request->param(); $allid = $data['id']; $res=AdminModel::destroy($allid); $num=count($allid) if($res){ return ['status'=>1,'message'=>'删除成功','num'=>$num]; }else{ return ['status'=>0,'message'=>'删除失败']; } }
|