//渲染结束
//监听头部工具栏的事件
table.on('toolbar(test)',function (obj) {
switch (obj.event) {
case 'add':
layer.msg('添加');
openAddUser();
break;
case 'delAll':
layer.msg('批量删除');
deleteAllUser();
break;
}
});
//定义一个ids的数组
var ids = new Array();
//监听checkbox复选
table.on('checkbox(test)', function(obj){
console.log(obj.checked); //当前是否选中状态
console.log(obj.data); //选中行的相关数据
//alert(obj.data.id);
ids.push(obj.data.id)//添加要删除的数据对应的id
console.log(obj.type); //如果触发的是全选,则为:all,如果触发的是单选,则为:one
});
//批量删除
function deleteAllUser(){
/*for (var i = 0; i <ids.length ; i++) {
alert(ids[i])
}*/
$.ajax({
type: "POST",
url: "/user/deleteAllByIds",
traditional:true,
data:{ id:ids},
dataType: "text",//设置接受到的响应数据的格式
success:function(data){
alert(data)
//刷新数据表格
tableIns.reload();
//清空数组
ids.length = 0;
}
/*,async:false*/
});
/*$.post('/user/deleteAllByIds',{id:ids}, function (obj) {
alert(obj);
//刷新数据表格
tableIns.reload();
//清空数组
ids.length = 0;
})*/
}
traditional:true 不加的话data里面会进行进一步渲染比如[]93 ,[]94,[]95 这样后台获取不到
@RequestMapping("/deleteAllByIds")
@ResponseBody
public String deleteAllByIds(String[] id){
System.out.println("删除的id是:"+ Arrays.toString(id));
userService.deleteAllByIds(id);
return "deleteAll,success";
}
mapper文件中
<!--批量删除数据 delete from user where id in (90,91,92)-->
<select id="deleteAllByIds" parameterType="list">
delete from user
<where>
<foreach collection="list" open="id in (" close=")"
item="id" separator="," >
#{id}
</foreach>
</where>
</select>