layui之数据表格--与后台交互获取数据

本文介绍了一种使用layui数据表格展示用户列表的方法,通过JS和Java实现前后端交互,完成用户信息的分页显示。重点讲解了如何配置前端table组件与后端Action交互,以及如何设置分页参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jsp或html

1 js

//用户列表
var tableIns = table.render({
elem: ‘#userList’,
url : ‘userAction_findAll.action’,
cellMinWidth : 95,
page : true,
height : “full-125”,
limits : [10,15,20,25],
limit : 10,
id : “userListTable”,
cols : [[
{type: “checkbox”, fixed:“left”, width:50},
{field: ‘userName’, title: ‘用户名’, minWidth:100, align:“center”},
{field: ‘userEmail’, title: ‘用户邮箱’, minWidth:200, align:‘center’,templet:function(d){
return ‘’+d.userEmail+’’;
}},
{field: ‘userSex’, title: ‘用户性别’, align:‘center’},
{field: ‘userStatus’, title: ‘用户状态’, align:‘center’,templet:function(d){
return d.userStatus == ‘0’ ? “正常使用”:“限制使用”;
}},
{field: ‘userGrade’, title: ‘用户等级’, align:‘center’},
{field: ‘userEndTime’, title: ‘最后登录时间’, align:‘center’,minWidth:150},
{title: ‘操作’, minWidth:175, templet:’#userListBar’,fixed:“right”,align:“center”}
]]
});

UserAction.java

// 接收前端发送来的分页参数
private int page = 1;
public void setPage(int page) {
this.page = page;
}
public int getPage() {
return page;
}
private int limit = 2;
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}

public String findAll(){
List allUser = this.getUserService().findAllUser(this.getPage(), this.getLimit());
Map<String, Object> result = new HashMap<String, Object>();
result.put(“code”, 0);
result.put(“msg”, “”);
result.put(“count”, this.getUserService().findTotalRecord(key));
JSONArray array = JSONArray.fromObject(allUser);
result.put(“data”, array);
// 将其转换为JSON数据,并压入值栈返回
ActionContext.getContext().getValueStack().set(“jsonData”, JSONObject.fromObject(result));
return “success”;
}

struts.xml配置

jsonData

返回JSON数据格式,也是layui数据表格要求的格式

{
“code”: 0,
“msg”: “”,
“count”: 15,
“data”: [
{
“usersId”: “1”,
“userName”: “user12”,
“userEmail”: "123@qq.com",
“userSex”: “女”,
“userStatus”: “0”,
“userGrade”: “倔强青铜”,
“userEndTime”: “2018-11-11 15:22:33”
},{
“usersId”: “2”,
“userName”: “asd”,
“userEmail”: "123@qq.com",
“userSex”: “男”,
“userStatus”: “1”,
“userGrade”: “秩序白银”,
“userEndTime”: “2018-11-11 15:22:33”
},
]
}

注意:

1.js中table的field需要与返回的数据key相对应,比如field: “userName”与“userName”: “asd”
2.layui的数据表格向url发送请求时,会附上page以及limit这两个参数,所以服务端需要接收并以此为条件查找数据

作者:张育嘉
来源:优快云
原文:https://blog.youkuaiyun.com/code_shadow/article/details/80459080
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值