使用layui在显示数据表格进行多选的时候遇到的几个问题:java
一、增长监听,让你的数据表格能够进行复选。jquery
layui.use('table', function(){ var $ = layui.jquery; var table = layui.table; //监听表格复选框选择 table.on('checkbox(table)', function(obj){ console.log(obj) });
二、获取选择的项,这里注意几点,ajax
一、table.checkStatus('tablelist') 这里的tablelist是定义的table idsql
<table id="tablelist">
二、使用 checkStatus.data 来获取数据 ,数据格式为数组数组
三、定义 数组 ids 来存放id的时候,首先要初始化 var ids=[]; 设置为数组app
四、ids增长id的时候推荐使用push,删除id 使用poppost
$("#sync").on('click',function () { var checkStatus = table.checkStatus('tablelist'),data = checkStatus.data; var ids=[]; for(var i=0;i<data.length;i++){ ids.push(data[i].id); } console.log(ids); $.ajax({ type : 'post', url : '/goController/methon', data:{ids:ids}, success : function(response){ parent.layer.alert(response.msg); }, error : function (response) { parent.layer.alert(response.msg); } }); });
三、jfinal 在接收传过来的数组时有指定的方法,getparaValues ,这里要注意 写为 “ids”的时候获取值为Null,只有设置为 ids[] 时候才能够获取相关值ui
String[] ids = getParaValues("ids[]");
四、处理ids直接生成 select * from table where id in ("id1","id2");url
使用 Arrays.toString 方法直接转为 id1,id2, 这种形式,方便组装 sqlspa
String idstr = Arrays.toString(ids); idstr = idstr.substring(1,idstr.length()-1); sqlstr.append(" and id in (" +idstr +")");
方法其实挺简单,做者都提供了,但调试的时候不注意就要浪费不少时间,写下来做为备忘。