Layui获取表格数据与java实现多删除问题
【1】绑定数据表格
//1-页面数据加载
$(function () {
console.log(music.volume );
//【1】加载&初始化layui模块
layui.use(["layer", "table",], function () {
table = layui.table;//【2】获取layui的table模块的属性
layer = layui.layer;//弹出层
//加载table模块数据
layuiTable();
});
});
//【2】加载table模块数据
function layuiTable(){
tabMusicInfor = table.render({
//【1】指定table的容器选择器或 DOM
elem: "#tabMusicInfor",
//【2】指定加载数据的路径
// url: "${ctx}/web/MyMusicServlet?method=musicList",
//【3】控制整个table元素的宽
width : 1500,
even: true,
//【4】设置表头。值是一个二维数组
cols: [[
{ type: "checkbox", fixed :"left",width:"10%"},
{ type: "numbers", title: "序号",width:"10%"},
{ field: "userSpaceID", title: "userSpaceID" ,hide:"true"},
{ field: "fileMD5", title: "fileMD5" },
{ field: "fileUrl", title: "路径" },
{ field: "music", title: "音乐名" ,width:"20%"},
{ field: "songer", title: "歌手" ,width:"20%"},
{ fixed :"right",title:"操作", toolbar: "#barDemo",align:"center" ,width:"10%"},
]],
//【5】开启分页,limit设置当前页面显示的记录值
page: {
limits : [5,10, 15,20,30,50 ]
,limit : 5
},
//【6】用于对分页请求(将分页重新向服务器请求数据)
request: {
pageName: "currentPage",
limitName: "rows",
},
//【7】存储当前页的列表数据
data:[],
});
//【8】音乐列表数据重载
searchTabMusic();
//【9】监听工具条事件
layuiTabletool();
}
【2】获取表格全部数据
var index=table.cache.tabMusicInfor;
【3】获取被选中复选框的表格数据
var checkStatus = table.checkStatus("tabtransmitInfor");
1)案例实现多选删除的注意事项:
1,注意后台只能接受字符串类型,为了方便传递参数,将参数拼接为字符串的形式传递到后台
//多选删除
$("#del").click(function(){
var strID ="";
var checkStatus = table.checkStatus("tabtransmitInfor");
for ( var i = 0; i < checkStatus.data.length; i++) {
strID+=checkStatus.data[i].fileRecordID+"*";
}
strID = strID.substr(0, strID.length - 1);
//删除
$.post("${ctx}/web/FileRecordServlet",{"method":"delFileRecord",
"strID":strID},function(data){
if(data=="1"){
layer.alert("删除成功!",{icon: 6} );
tabtransmitInfor.reload();
}
});
});
- 在后台分割字符串时要注意
直接使用String[] strArr=strID.split("*");是不正确的,报错Dangling meta character '*' near index 0,因为+、*、|、\等符号在正则表达示中有相应的不同意义,所以在使用时要进行转义处理。
解决方法:String[] strArr=strID.split("\\*");
public void delFileRecord(HttpServletRequest request,HttpServletResponse response) throws IOException {
String strID= request.getParameter("strID");
int y=0;
int x=0;
System.out.println(strID);
String[] strArr=strID.split("\\*");
for (int i = 0; i < strArr.length; i++) {
int ID=Integer.parseInt(strArr[i]);
int flage=ifrs.deleteById(ID);
y+=flage;
x=i+1;
}
PrintWriter out =response.getWriter();
if(y==x){
out.write("1");
}
out.flush();
out.close();
}