HTML页面
<!--定义一个table表格位置-->
<table id="userTable" lay-filter="userEvent"></table>
<!--定义行操作(这里添加了判定)-->
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
{{# if(d.userRole==1){}}
<a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled" lay- event="del">删除</a>
{{# } else if(d.userRole==2){}}
<a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
{{# }}}
</script>
JS代码
layui.use('table',function(){ //这里必须添加依赖table面板
var table =layui.table;
table.render({
elem:'#userTable'
,url:'/ControlManager/userController/selAllUserInfo' //controller
,page:true
,limit:5
,limits : [5,10,15]
,groups:3
,cols: [[//表头
{field:'userId',title:'ID',width:'20%',align:'center',sort:true}
,{field:'userCode',title:'用户名',width:'20%',align:'center'}
,{field:'userRole',title:'角色',width:'30%',align:'center',templet:function(d){
if (d.userRole=="1"){
return "管理员"
}else {
return "普通用户"
}
}}
,{field:'operate',title:'操作',width:'30%',toolbar:'#barDemo',align:'center',templet:'#barDemo'}
]]
,done:function () { //加载完成的回调函数
layer.msg("数据加载完成...",{icon:1,time:1500});
}
});
});
Controller层
@ResponseBody
@RequestMapping(value = "/objectMethod" )
public Object objectMethod(@RequestParam("page") String page1,@RequestParam("limit")String limit1){
int page = Integer.valueOf(page1); //分页参数,根据数据库不同进行改变(这里是Oracle数据库)
int limit = Integer.valueOf(limit1);
int before = limit * (page -1) +1;
int after = page * limit;
List<Object> userList = objectService.selAllObject(); //查询要显示的信息集合
HashMap<String,Object> objMap = new HashMap<String,Object>(); //HashMap集合,用于返回json数据
objMap.put("code",0); //code,可用于前端判断
objMap.put("msg", ""); //类似code,msg可赋予字符串,用作提示;
objMap.put("count",userService.countUser()); //调用方法查询全部
Object arry = JSONArray.toJSON(userList);
objMap.put("data",arry);
return JSONArray.toJSONString(objMap);
}