1.前台使用layui分页,官方文档展示如下:
就是用这个分页条来进行分页
2.完成的效果如下:
不理解的部分可以看一下官方帮助文档,说白了就是将官方静态分页效果用后台数据进行填充而已
注意:分页条用的layui,所以需要引入一套layui,表格用的bootstrap表格的类,请引入bootstrap,当然首先必须引入jquery
3.下面是前台代码:前台向后台传递俩参数,一个curr,一个limit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="js/layui/css/layui.css">
<link rel="stylesheet" href="js/bootstrap-table.min.css"></link>
<link rel="stylesheet" href="js/bootstrap.min.css"></link>
</head>
<body>
<table class="table table-striped">
<thead>
<th>优惠券类型</th>
<th>发放对象</th>
<th>发放时间</th>
<th>发放面额</th>
<th>每用户发放数量</th>
<th>有效期</th>
</thead>
<tbody id="tbs">
</tbody>
</table>
<div id="demo8"></div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-table.min.js"></script>
<script src="js/layui/layui.js"></script>
<script>
$(function(){
fenye();
})
function fenye(){
layui.use(['laypage', 'layer'], function(){
var laypage = layui.laypage
,layer = layui.layer;
//自定义排版
$.ajax({
type:'post',
dataType:'json',
data:{'curr':1,'limit':10},
url:'../../DataShow3',
success:function(data){
showData(data);
laypage.render({
elem: 'demo8'
,count: data.ct
,layout: ['limit', 'prev', 'page', 'next']
,jump:function(obj){
//分页切换的回掉
$.ajax({
type:'post',
dataType:'json',
data:{'curr':obj.curr,'limit':obj.limit},
url:'../../DataShow3',
success:function(data){
showData(data);
}
})
}
});
}
})
});
}
function showData(ds){
$("#tbs").empty();
var htmlStr="";
for(var i=0;i<ds.data.length;i++){
htmlStr+='<tr><td>';
htmlStr+=ds.data[i].id;
htmlStr+='</td><td>';
htmlStr+=ds.data[i].className;
htmlStr+='</td><td>';
htmlStr+=ds.data[i].classSumNum;
htmlStr+='</td><td>';
htmlStr+=ds.data[i].teacherName;
htmlStr+='</td><td>';
htmlStr+=ds.data[i].fdyName;
htmlStr+='</td><td>';
htmlStr+=ds.data[i].fdyNum;
htmlStr+='</td></tr>'
}
$("#tbs").append(htmlStr);
}
</script>
</body>
</html>
4.controller代码:
@ResponseBody
@RequestMapping("/DataShow3")
public Map<String,Object> fdf(int curr, int limit){
Map<String,Object> map=new HashMap<String,Object>();
List<ClassTable> ct=dataShowService.queryInfox(curr,limit);
//总数居条数
int countx=dataShowService.queryAllCountxx();
map.put("data",ct);
map.put("ct",countx);
return map;
}
5.service代码:
public interface DataShowService {
List<ClassTable> queryInfox(int curr, int limit);
int queryAllCountxx();
}
6.serviceImpl代码
public class DataShowServiceImpl implements DataShowService {
@Autowired
private DataShowServiceMapper dataShowServiceMapper;
@Override
public List<ClassTable> queryInfox(int curr, int limit) {
curr=(curr-1)*limit;
return dataShowServiceMapper.queryInfox(curr,limit);
}
@Override
public int queryAllCountxx() {
return dataShowServiceMapper.queryAllCountxx();
}
}
注意:这里根据当前页算出起始数据是第几条
7.dao代码:
public interface DataShowServiceMapper {
List<ClassTable> queryInfox(@Param("curr")int curr, @Param("limit")int limit);
int queryAllCountxx();
}
8.mapper代码:
<select id="queryInfox" resultType="com.ansheng.entity.ClassTable">
select * from classtable limit #{curr},#{limit}
</select>
<select id="queryAllCountxx" resultType="java.lang.Integer">
select count(*) from classtable
</select>
9.结束了,是不是很简单
感兴趣的小伙伴可以扫码关注下公众号哦,公众号会分享高质量的技术文章哦