版权声明:转载请留言告知,注明出处 https://blog.csdn.net/qq_36213352/article/details/87023315
本人使用的是ssm+postgresql
前端使用的是layui的表单
在此只截取关键部分代码,想要了解layui请自行百度
table.render({
elem: '#test'
, url: 'map/GetVillageList.in'
, toolbar: '#toolbarDemo'
, title: '村级数据表'
, cols: [[
{type: 'checkbox', fixed: 'left'}
, {field: 'cjqydm', title: 'ID', width: 180, fixed: 'left', totalRowText: '合计行'}
, {field: 'name', title: '村名', width: 200}
, {field: 'ph', title: 'PH值', width: 180}
, {field: 'organic', title: '有机质', width: 180}
, {field: 'n', title: '氮含量', width: 180}
, {field: 'p', title: '磷含量', width: 180}
, {field: 'k', title: '钾含量', width: 180}
, {fixed: 'right', title: '操作', fixed: 'right', toolbar: '#barDemo', width: 120}
]]
, page: {
layout: ['count', 'prev', 'page', 'next', 'skip', 'limit'] //自定义分页布局
, curr: 1 //设定初始在第 5 页
, groups: 5 //只显示 1 个连续页码
// , first: true //不显示首页
// , last: true //不显示尾页
, limit: 20
}
, height: 'full-200'
, cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
, request: { //发送请求
pageName: 'page', //页码的参数名称,默认:page
limitName: 'limit' //每页数据量的参数名,默认:limit
}
, response: {
statusCode: "success" //重新规定成功的状态码为 200,table 组件默认为 0
}
, parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
console.log(res);
return {
"code": res.type, //解析接口状态
"msg": '', //解析提示文本
"count": 1071, //解析数据长度
"data": res.data //解析数据列表
};
}
});
page是页码
limit是每页数据量
上面是layui请求的过程
要想实现表单的分页功能,数据库也要做相应的变通
1.调整数据的参数(在controller层对数据page进行处理)
@RequestMapping("/GetVillageList.in")
@ResponseBody
public String GetVillageList(@RequestParam Map map) {
Integer page = Integer.parseInt((String) map.get("page"));
Integer limit = Integer.parseInt((String) map.get("limit"));
map.put("page", (page - 1) * limit);
map.put("limit", limit);
ResultModel resultModel = new ResultModel();
List<Map> a = mapMapper.likeList(map);
if (a != null) {
resultModel.data = a;
} else {
resultModel.type = "error";
}
return gson.toJson(resultModel);
}
2.在mapper层对数据进行处理
<select id="likeList" parameterType="map" resultType="hashmap">
SELECT A.uid, A.name,A.type,A.ph,A.organic,A.n,A.p,A.k,A.cjqydm FROM tb_village A
<trim prefix="WHERE" suffixOverrides="AND | OR">
<if test="name!=null">
A.name LIKE CONCAT('%',#{name},'%') AND
</if>
</trim>
OFFSET #{page}::bigint
LIMIT #{limit}::bigint
</select>
其中OFFSET 是忽略掉查出来的前 page个数据
LIMIT 是限制返回的数据的个数