第一步:创建一个页面,jsp/html
引入静态资源,把路径改为自己的
<link href="${staticPath}/static/layuiadmin/layui/css/layui.css" rel="stylesheet">
<script src="${staticPath}/static/layuiadmin/layui/layui.js"></script>
第二步:
<script>
layui.use('table', function(){
var table = layui.table;
//实例
table.render({
elem: '#demo' //指定你创建的table表格的id
,height: 312 //高
,url: '/demo/table/user/' //数据接口,写自己的接口路径
,page: true //开启分页 默认会有2个参数传递,page=0&limit=10 默认值
,cols: [[ //表头 field对应你的字段名,很重要 sort
{field: 'id', title: 'ID', width:80,}
,{field: 'username', title: '用户名', width:80}
,{field: 'sex', title: '性别', width:80, }
,{field: 'city', title: '城市', width:80}
,{field: 'sign', title: '签名', width: 177}
,{field: 'experience', title: '积分', width: 80}
,{field: 'score', title: '评分', width: 80}
,{field: 'classify', title: '职业', width: 80}
,{field: 'wealth', title: '财富', width: 135}
]]
});
parseData: function(res){ //res 接口返回来的数据,重要,不写会出现接口异常或返回的code不对
console.log(res);
return {
"code": 0//解析接口状态 默认为0
,"msg":res.message//解析提示文本,错误时的提示消息
,"count": res.count//解析数据长度,count为sql查询出来的长度
,"data": res.list //解析数据列表,list为sql查询出来的数据
};
}
});
Controller中接收2个分页参数,在sql中加上linmt #{page},#{limit},把前台传来的2个参数放进去即可
@RequestMapping(value = "getDept", method = RequestMethod.GET)
@ResponseBody
public Object recordLosedepartmentId(Integer page,Integer limit,String archivesId,Integer status) {
List<DbWheretKey> where = new ArrayList<>();
where.add(new DbWheretKey(DBType.str, "archives_id", archivesId, DBCompareType.equal, true));
List<Map<String, Object>> tables = baseService.getListItemByKeys(DBTableName.T_BASE_ARCH_TABLE, DBColumnsFinal.T_BASE_ARCH_TABLE, where);
List<Map<String,Object>> getDept = new ArrayList<>();
for (Map<String, Object> item : tables) {
System.out.println(item.get("tableName").toString());
pageInfo.setNowpage(page);
pageInfo.setPagesize(limit);
List<Object> list = checkService.getListdept(item.get("tableName").toString(), status,pageInfo.getFrom(),limit);
Integer count = collcheckService.recordLosedepartmentId(item.get("tableName").toString(), status);
getDept.add(new HashMap<String, Object>() {{
put("tableType", item.get("tableIden"));//返回的状态
put("count", count);//返回的数据长度
put("list",list);//返回的数据列表
}});
}
return getDept;
}
现在运行页面接口没问题的话表头和数据已经有了
注意:Layui.table 不会帮你处理分页,只会默认携带2个参数过去,在Controller中接收并放入sql中再次返回去的数据就是分页后的数据了