layui之?dāng)?shù)據(jù)表格--與后臺交互獲取數(shù)據(jù)的方法
jsp或html
<table id="userList" lay-filter="userList"></table>
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 '<a class="layui-blue" href="mailto:'+d.userEmail+'" rel="external nofollow" >'+d.userEmail+'</a>';
}},
{field: 'userSex', title: '用戶性別', align:'center'},
{field: 'userStatus', title: '用戶狀態(tài)', align:'center',templet:function(d){
return d.userStatus == '0' ? "正常使用":"<span class='layui-red'>限制使用</span>";
}},
{field: 'userGrade', title: '用戶等級', align:'center'},
{field: 'userEndTime', title: '最后登錄時間', align:'center',minWidth:150},
{title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"}
]]
});
UserAction.java
// 接收前端發(fā)送來的分頁參數(shù)
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<User> 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);
// 將其轉(zhuǎn)換為JSON數(shù)據(jù),并壓入值棧返回
ActionContext.getContext().getValueStack().set("jsonData", JSONObject.fromObject(result));
return "success";
}
struts.xml配置
<!-- 用戶配置 -->
<package name="user" namespace="/" extends="common,json-default">
<action name="userAction_*" class="userAction" method="{1}">
<result name="success" type="json">
<param name="root">jsonData</param>
</result>
</action>
</package>
返回JSON數(shù)據(jù)格式,也是layui數(shù)據(jù)表格要求的格式
{
"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需要與返回的數(shù)據(jù)key相對應(yīng),比如field: “userName”與“userName”: “asd”
2.layui的數(shù)據(jù)表格向url發(fā)送請求時,會附上page以及l(fā)imit這兩個參數(shù),所以服務(wù)端需要接收并以此為條件查找數(shù)據(jù)
前端展示效果

以上這篇layui之?dāng)?shù)據(jù)表格--與后臺交互獲取數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JS 打印功能代碼可實現(xiàn)打印預(yù)覽、打印設(shè)置等
一個不錯的JS 打印功能代碼,包括打印預(yù)覽、打印設(shè)置等,里面整合了很多知識,是一個不錯的學(xué)習(xí)案例2014-10-10
JS 滾動事件window.onscroll與position:fixed寫兼容IE6的回到頂部組件
這篇文章主要介紹了JS 滾動事件window.onscroll與position:fixed寫兼容IE6的回到頂部組件的相關(guān)資料,需要的朋友可以參考下2016-10-10
Javascript DOM的簡介,節(jié)點和獲取元素詳解
下面小編就為大家分享一篇詳談DOM的簡介,節(jié)點和獲取元素,具有非常好的參考價值,一起跟隨小編過來看看吧,希望對大家有所幫助2021-11-11
EasyUi中的Combogrid 實現(xiàn)分頁和動態(tài)搜索遠程數(shù)據(jù)
jquery easyui中的combogrid比較特殊,算是combo和grid的組合,combogrid結(jié)合一個可編輯的文本框和下拉數(shù)據(jù)網(wǎng)格面板,可以讓用戶迅速找到并選擇,又可以進行搜索,展示與當(dāng)前輸入的字符相匹配的數(shù)據(jù)。下面給大家介紹EasyUi中的Combogrid 實現(xiàn)分頁和動態(tài)搜索遠程數(shù)據(jù)2016-04-04
微信小程序開發(fā)之map地圖組件定位并手動修改位置偏差
這篇文章主要介紹了微信小程序開發(fā)之map地圖組件,定位,并手動修改位置偏差,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08

