使用layui+springboot分页demo(第二版,常用版)

1.前台使用layui分页,官方文档展示如下:

就是用这个分页条来进行分页:

效果图如下:

比第一个版本多了一个模糊搜索框,这也是实际开发中常用到的

注意:分页条用的layui,所以需要引入一套layui,表格用的bootstrap表格的类,请引入bootstrap,当然首先必须引入jquery 

3.下面是前台代码:前台向后台传递俩参数,一个curr,一个limit,一个模糊查询字段mh

<!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>
<div style="width: 100%;height: 80px;">
<input type="text" placeholder="请输入班级进行模糊查询" id="mh" style="height: 30px;width: 174px;margin-left: 1224px;margin-top: 25px;">
    <button style="width: 70px;height: 30px;background: #fff;border: 1px solid #c6c6c5;" id="ss">搜索</button>
</div>
<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($("#mh").val());
    })
    function fenye(MH){
        layui.use(['laypage', 'layer'], function(){
            var laypage = layui.laypage
                ,layer = layui.layer;
            //自定义排版
            $.ajax({
                type:'post',
                dataType:'json',
                data:{'curr':1,'limit':10,'mh':MH},
                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,'mh':MH},
                               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);
}

//点击搜索进行模糊查询
    $("#ss").click(function(){
        //获取文本框字段
        var MH=$("#mh").val();
        fenye(MH);
    })
</script>
</body>
</html>
4.controller代码:
    public Map<String,Object> fdf(int curr, int limit,String mh){
        Map<String,Object> map=new HashMap<String,Object>();
        List<ClassTable> ct=dataShowService.queryInfox(curr,limit,mh);
        //总数居条数
        int countx=dataShowService.queryAllCountxx(mh);
        map.put("data",ct);
        map.put("ct",countx);
        return map;
    }

5.service代码:

   List<ClassTable> queryInfox(int curr, int limit, String mh);

    int queryAllCountxx(String mh);

6.serviceImpl代码

    @Override
    public List<ClassTable> queryInfox(int curr, int limit, String mh) {
    //根据当前页算出来起始数据是第几条
        curr=(curr-1)*limit;
        return dataShowServiceMapper.queryInfox(curr,limit,mh);
    }

    @Override
    public int queryAllCountxx(String mh) {
        return dataShowServiceMapper.queryAllCountxx(mh);
    }

7.dao代码:

扫描二维码关注公众号,回复: 6195264 查看本文章
    List<ClassTable> queryInfox(@Param("curr") int curr, @Param("limit") int limit, @Param("mh")String mh);

    int queryAllCountxx(String mh);

8.mapper代码:

 <select id="queryInfox" resultType="com.ansheng.entity.ClassTable">
        select * from classtable where className LIKE '%${mh}%' limit #{curr},#{limit}
    </select>
    <select id="queryAllCountxx" resultType="java.lang.Integer">
          select count(*) from classtable
        <if test='_parameter != "" and _parameter != null'>
            where className LIKE '%${_parameter}%'
        </if>
    </select>

结束了,是不是感觉so easy

猜你喜欢

转载自blog.csdn.net/royal1235/article/details/83451012