layui實(shí)現(xiàn)數(shù)據(jù)表格table分頁(yè)功能(ajax異步)
layui實(shí)現(xiàn)數(shù)據(jù)表格table分頁(yè)功能,異步加載,表格渲染,含條件查詢。
一、引入layUI的相關(guān)資源
<link rel="stylesheet" href="${ctxPath}/vendor/layui/css/layui.css" >
<script src="${ctxPath}/vender/layui/layui.js" charset="utf-8"></script>
二、html頁(yè)面代碼
搜索表單:
<div class="layui-row"> <form class="layui-form layui-col-md12 we-search"> 項(xiàng)目搜索: <div class="layui-inline"> <input type="text" name="projectName" id="projectName" placeholder="項(xiàng)目名稱" autocomplete="off" class="layui-input"> </div> <div class="layui-input-inline"> <select name="businessOperatorId" id="businessOperatorId" lay-verify="" lay-search> <option value="">業(yè)務(wù)員</option> </select> </div> <div class="layui-input-inline"> <select name="mftRepresentativeId" id="mftRepresentativeId" lay-verify="" lay-search> <option value="">廠家代表</option> </select> </div> <div class="layui-input-inline"> <select name="channelId" id="channelId" lay-search> <option value="">渠道</option> </select> </div> <div class="layui-input-inline"> <select name="customerId" id="customerId" lay-search> <option value="">客戶</option> </select> </div> <div class="layui-input-inline"> <select name="projectPhase" id="projectPhase" lay-search> <option value="">項(xiàng)目階段</option> </select> </div> <div class="layui-input-inline"> <select name="goodsCondition" id="goodsCondition" lay-search> <option value="">貨物情況</option> </select> </div> <div class="layui-input-inline"> <select name="implementCondition" id="implementCondition" lay-search> <option value="">實(shí)施情況</option> </select> </div> <div class="layui-input-inline"> <select name="payCondition" id="payCondition" lay-search> <option value="">收款情況</option> </select> </div> <div class="layui-inline"> <input class="layui-input" placeholder="開(kāi)項(xiàng)時(shí)間" name="startTime" id="startTime"> </div> <div class="layui-inline"> <input class="layui-input" placeholder="結(jié)項(xiàng)時(shí)間" name="endTime" id="endTime"> </div> <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon"></i></button> </form> </div>
數(shù)據(jù)表格:
<table class="layui-hide" id="projectList" lay-filter="projectList"></table>
三、后臺(tái)接收分頁(yè)參數(shù)以及查詢條件,獲取并返回?cái)?shù)據(jù)
主要注意下:
page: 前臺(tái)分頁(yè)插件傳入的當(dāng)前頁(yè)數(shù),
limit: 前臺(tái)分頁(yè)插件傳入的每頁(yè)個(gè)數(shù),
projectInfo :接收前臺(tái)傳入的查詢條件的實(shí)體
jsonResult :后臺(tái)返回的相關(guān)數(shù)據(jù)的響應(yīng)實(shí)體
@ResponseBody
@RequestMapping("/project/list")
public JsonResult list(@RequestParam("page") Integer page, @RequestParam("limit") Integer size, ProjectInfoEntity projectInfo){
JsonResult jsonResult = projectService.getProjectList(page,size,projectInfo);
return jsonResult;
}
后臺(tái)響應(yīng)類必須包含code與count字段,因?yàn)榍芭_(tái)layui會(huì)自動(dòng)獲取
自定義后臺(tái)數(shù)據(jù)響應(yīng)實(shí)體 JsonResult:
package com.bhy702.jfkj.common.util;
/**
* JSON結(jié)果響應(yīng)
*
*/
@Data
public class JsonResult {
private static final String SUCCESS = "成功";
private static final String ERROR = "失敗";
/**
* 響應(yīng)狀態(tài)code,因?yàn)榍芭_(tái)layui默認(rèn)0為響應(yīng)成功,所以此處默認(rèn)為0
*/
private Integer code = 0;
/**
* 數(shù)據(jù)總條數(shù)
*/
private Long count = 0L;
/**
* 返回是否成功
*/
private Boolean result = false;
/**
* 返回提示信息
*/
private String msg = "";
/**
* 返回?cái)?shù)據(jù)信息
*/
private Object data;
private JsonResult() {
}
/**
* 成功的響應(yīng)
*
* @return
*/
public static JsonResult success() {
return result(true, SUCCESS, null,null);
}
public static JsonResult success(String msg) {
return result(true, msg, null,null);
}
public static JsonResult success(Object data) {
return result(true, SUCCESS, data,null);
}
public static JsonResult success(Object data,Long count) {
return result(true, SUCCESS, data,count);
}
public static JsonResult success(String msg, Object data) {
return result(true, msg, data,null);
}
public static JsonResult success(String msg, Object data,Long count) {
return result(true, msg, data,count);
}
/**
* 失敗的響應(yīng)
*
* @return
*/
public static JsonResult error() {
return result(false, ERROR, null,null);
}
public static JsonResult error(String msg) {
return result(false, msg, null,null);
}
public static JsonResult error(Object data) {
return result(false, ERROR, data,null);
}
public static JsonResult error(Object data,Long count) {
return result(false, ERROR, data,count);
}
public static JsonResult error(String msg, Object data) {
return result(false, msg, data,null);
}
public static JsonResult error(String msg, Object data,Long count) {
return result(false, msg, data,count);
}
public static JsonResult result(Boolean result, String msg, Object data,Long count) {
JsonResult jsonResult = new JsonResult();
jsonResult.setResult(result);
jsonResult.setMsg(msg);
jsonResult.setData(data);
jsonResult.setCount(count);
return jsonResult;
}
}
四、渲染table表格數(shù)據(jù)
主要注意下:
elem: ‘#projectList': projectList為表格id,
page: true: 設(shè)置表格分頁(yè),
url: ‘/project/list' :數(shù)據(jù)請(qǐng)求url
fixed: true:固定列
done : function(res, curr, count){…}:數(shù)據(jù)加載成功后的回調(diào)函數(shù)
var tableIns = table.render({
elem: '#projectList',
cellMinWidth: 150,
url: '/project/list',
cols: [
[{
// type: 'checkbox',fixed: 'left'
checkbox: true, fixed: true
}, {
field: 'id',title: 'ID',align:'center',width:50,fixed: true
}, {
field: 'name',title: '項(xiàng)目名稱',align:'center',fixed: true
}, {
field: 'businessOperatorStr',title: '經(jīng)辦人',align:'center',fixed: true
}, {
field: 'mftRepresentativeStr',title: '廠家代表',align:'center',fixed: true
}, {
field: 'channelStr',title: '渠道',align:'center',fixed: true
}, {
field: 'customerStr',title: '客戶',align:'center',fixed: true
}, {
field: 'projectPhaseStr',title: '項(xiàng)目階段',align:'center',fixed: true
}, {
field: 'amount',title: '金額',align:'center'
}, {
field: 'businessSource',title: '商機(jī)來(lái)源',align:'center'
}, {
field: 'mainProduct',title: '主要產(chǎn)品',align:'center'
}, {
field: 'productLineStr',title: '產(chǎn)品線',align:'center'
}, {
field: 'goodsConditionStr',title: '貨物情況',align:'center'
}, {
field: 'implementConditionStr',title: '實(shí)施情況',align:'center'
}, {
field: 'payAmount',title: '已付金額',align:'center'
}, {
field: 'payConditionStr',title: '收款情況',align:'center'
}, {
field: 'startTime',title: '開(kāi)項(xiàng)時(shí)間',align:'center'
}, {
field: 'endTime',title: '結(jié)項(xiàng)時(shí)間',align:'center'
}, {
field: 'remark',title: '備注',align:'center'
}, {
field: 'operate',title: '操作',toolbar: '#operateTpl',fixed: 'right',unresize: true
}]
],
id: 'testReload',
// skin: 'row', //表格風(fēng)格
even: true, //隔行背景
event: true,
page: true,
done : function(res, curr, count){
$('#totalProjectNumber').text("共有數(shù)據(jù):"+count+" 條");
table_data=res.data;
layer.closeAll('loading');
// layer.close(layer.index); //它獲取的始終是最新彈出的某個(gè)層,值是由layer內(nèi)部動(dòng)態(tài)遞增計(jì)算的
// layer.close(index); //返回?cái)?shù)據(jù)關(guān)閉loading
}
});
五、監(jiān)聽(tīng)搜索表單的提交事件,并重新渲染table表格數(shù)據(jù)
主要注意下:
sreach: 為搜索按鈕的lay-filter=“sreach”,
where 中的數(shù)據(jù)對(duì)應(yīng)搜索表單,為搜索的條件,后臺(tái)使用這些條件進(jìn)行篩選數(shù)據(jù)返回
form.on('submit(sreach)', function(data){
layer.load();
tableIns.reload({
url:"/project/list",
page: {
curr: 1 //重新從第 1 頁(yè)開(kāi)始
},
where:{
name:data.field.projectName,
businessOperatorId:data.field.businessOperatorId,
mftRepresentativeId:data.field.mftRepresentativeId,
channelId:data.field.channelId,
customerId:data.field.customerId,
projectPhase:data.field.projectPhase,
goodsCondition:data.field.goodsCondition,
implementCondition:data.field.implementCondition,
payCondition:data.field.payCondition,
startTime:data.field.startTime,
endTime:data.field.endTime
}
});
return false; //阻止表單跳轉(zhuǎn)。如果需要表單跳轉(zhuǎn),去掉這段即可。
});
六、效果展示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS獲取子節(jié)點(diǎn)、父節(jié)點(diǎn)和兄弟節(jié)點(diǎn)的方法實(shí)例總結(jié)
這篇文章主要介紹了JS獲取子節(jié)點(diǎn)、父節(jié)點(diǎn)和兄弟節(jié)點(diǎn)的方法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript針對(duì)子節(jié)點(diǎn)、父節(jié)點(diǎn)和兄弟節(jié)點(diǎn)獲取相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2018-07-07
js實(shí)現(xiàn)精確到秒的倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)精確到秒的倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
微信小程序使用this.setData()遇到的問(wèn)題及解決方案詳解
this.setData估計(jì)是小程序中最經(jīng)常用到的一個(gè)方法,但是要注意其實(shí)他是有限制的,忽略這些限制的話,會(huì)導(dǎo)致數(shù)據(jù)無(wú)法更新,下面這篇文章主要給大家介紹了關(guān)于微信小程序使用this.setData()遇到的問(wèn)題及解決方案,需要的朋友可以參考下2022-08-08
WordPress中利用AJAX技術(shù)進(jìn)行評(píng)論提交的實(shí)現(xiàn)示例
這篇文章主要介紹了WordPress中利用AJAX技術(shù)進(jìn)行評(píng)論提交的實(shí)現(xiàn)示例,整個(gè)處理的關(guān)鍵點(diǎn)在于文中的ajax_comment函數(shù),需要的朋友可以參考下2016-01-01
JavaScript 代碼分割的實(shí)現(xiàn)步驟
JavaScript代碼分割是一種優(yōu)化策略,通過(guò)將代碼拆分成較小的塊,只在需要時(shí)加載,可以降低初始加載時(shí)間,減小頁(yè)面體積,本文主要介紹了JavaScript代碼分割的實(shí)現(xiàn)步驟,感興趣的可以了解一下2024-01-01
利用javaScript實(shí)現(xiàn)點(diǎn)擊輸入框彈出窗體選擇信息
這篇文章主要是對(duì)利用javaScript實(shí)現(xiàn)點(diǎn)擊輸入框彈出窗體選擇信息進(jìn)的實(shí)例行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
javascript淡入淡出效果的實(shí)現(xiàn)思路
這個(gè)思路是最近寫(xiě)XScroll.js類的時(shí)候想明白的。平常我們說(shuō)的淡入淡出效果,一般分成兩部分,一半是淡入,另一半就是淡出了。不過(guò)經(jīng)過(guò)分析,我覺(jué)得其實(shí)只需要一半就行了2012-03-03

