如下所示:
主要在前端頁面加:
搜索ID:
userid
content
搜索
在
reload:function () {
var keyWord=$("#keyWord").val();
var keyType=$("#key_type option:selected").val();
table.reload('contenttable',{
method:'post',
where:{keyWord:keyWord,keyType:keyType}
});
}
layui.use('table', function(){
var table = layui.table;
//渲染
table.render({
elem: '#test' //綁定table表格
,height: 450
,url: '/admin/backContent' //后臺springmvc接收路徑
,page:true //true表示分頁
,limit: 10
,id:'contenttable'
,toolbar: '#toolbarDemo'
,cols: [[
{type: 'checkbox', fixed: 'left'}
,{field:'id', title:'id', width:80, fixed: 'left', unresize: true, sort: true}
,{field:'content', title:'內容', width:120}
,{field:'userid', title:'用戶id', width:80, sort: true}
,{field:'nice', title:'點贊數', width:100}
,{field:'createtime', title:'分享時間', width:80, sort: true}
,{field:'pic1', title:'圖片1', width:120,templet:'
@ResponseBody
public ResultMap> backContent(Page page, @RequestParam("limit") int limit){
page.setRows(limit);
ListcontentList=contentService.selectPageList(page);
int totals=contentService.selectPageCount(page);
page.setTotalRecord(totals);
return new ResultMap>(0,"",totals,contentList);
}
因為layui返回的參數不單單是json數組,要符號其的數據格式才能在jsp頁面顯示數據,所以用ResultMap類來處理返回數據的格式。
package net.stxy.one.model;
/**
*
* layui數據表格返回數據處理類
* Created by ASUS on 2018/5/19
*
* @Authod Grey Wolf
*/
public class ResultMap {
private String msg;
private T data;
private int code;
private int count;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ResultMap(int code,String msg, int count,T data) {
this.code = code;
this.msg = msg;
this.count = count;
this.data = data;
}
public ResultMap() {
}
}
其中mapper的語句:
select
from content
AND userid like '%' #{keyWord} '%'
AND content like '%' #{keyWord} '%'
order by id DESC
limit #{start},#{rows}
select count(1) from content
AND userid like '%' #{keyWord} '%'
AND content like '%' #{keyWord} '%'
以上這篇layui的數據表格+springmvc實現搜索功能的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持前端開發者。