html层
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
<button class="layui-btn layui-btn-sm" lay-event="upd">修改</button>
<button class="layui-btn layui-btn-sm" lay-event="del">删除</button>
</div>
</script>
</body>
<script src="/js/layui/layui.js" charset="utf-8"></script>
<script>
var cardian="";
layui.use(["table","layer","form","jquery"], function() {
var table = layui.table
, layer = layui.layer
, $ = layui.jquery
, form = layui.form
table.render({
elem: '#test'//需要渲染的元件(id为test的表格)
, height: 'full'
, url: '/cardian/findall'//查询所有的请求指令
, method: 'post'
, toolbar: '#toolbarDemo'//增删改行按钮的监听
,defaultToolbar: []//limit 设置
,limits:[5,10,15]
,limit:5
, title: '4s店售后信息表'
, cellMinWidth: 80//单元最小宽度
, cols: [[
//渲染表格
{type: 'checkbox', fixed: 'left'}
, {field: 'id', title: 'ID'}
, {field: 'name', title: '姓名', edit: 'text'}
, {field: 'pid', title: '身份证', edit: 'text'}
, {field: 'phone', title: '手机号'}
, {field: 'affair', title: '事由'}
, {field: 'remarks', title: '备注'}
]]
, page: true//允许分页
, id: 'cardian'//起个名字
, parseData: function (rs) {//数据格式解析
return {
"code": rs.code,
"msg": rs.msg,
"count": rs.data.total,
"data": rs.data.list
}
},
response: { // 设置响应码
statusCode: 200 //规定成功的状态码,默认:0
}
});
//toolbar是上边自己声明的值,test是对应的表格的id
table.on('toolbar(test)',function (obj) {
var checkStatus = table.checkStatus(obj.config.id);
switch (obj.event) {
case 'del':
/*拿到对应的表格*/
var data=checkStatus.data;
/*判断用户是否选择*/
if(data.length==0){
layer.msg("请选择要删除的信息!")
return false;
}
//若已选择则拿到每一个id组成字符串传给后台
var ids=null;
for(let i=0,len=data.length;i<len;i++){
ids=ids+(data[i].id)+",";
}
layer.confirm('是否删除记录', function(index){
$.post("/del",{ids},function(data){
if (data=="success"){
layer.msg("删除成功!");
}else {
layer.msg("删除失败!");
}
});
window.parent.location.reload();
});
break;
}
})
});
controller层
@Controller
public class CardianCon {
@Autowired
private CardianSer cardianSer;
/**
* 删除方法
* @return
*/
@RequestMapping("/del")
@ResponseBody
public String del(String ids){
//去除字符串最后一个多余的逗号
String id=ids.substring(0,ids.length()-1);
//根据逗号把他分割成字符串组
String[] split = id.split(",");
List list=new ArrayList();
//放进一个list集合中
for (int i = 0; i <split.length ; i++) {
//拿到数组中的每一个值,转成integer类型放进list集合中
String idss=split[i];
Integer idint = Integer.valueOf(idss);
list.add(idint);
}
//调用方法
int pan=cardianSer.del(list);
if (pan>0){
return "success";
}else {
return "error";
}
}
}
service层
service接口层CardianSer.java(这是一个接口)
public interface CardianSer {
int del(List list);
}
service层实现层CardianSer.java(这是一个实现类,连接接口)
@Service
public class CardianSerImpl implements CardianSer{
@Autowired
private CardianMapper cardianMapper;
public int del(List list) {
return cardianMapper.del(list);
}
}
dao层接口层CardianMapper.java (这是一个接口)
public interface CardianMapper {
int del(List list);
}
dao层mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
/*这里是dao层接口的具体位置*/
<mapper namespace="com.ly.mapper.CardianMapper" >
<delete id="del" parameterType="list">
DELETE FROM cardian where id in
/*循环list集合,起别名为id,最开始一个括号,结束一个括号,中间用逗号隔开*/
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>