layui-laypage后端分页

1 篇文章 0 订阅
1 篇文章 0 订阅

本项目为SSM项目,本示例始终使用layui后端分页时,需要返回总条数totalCount、当前页page,当前页条数limit

一、contraller代码

@RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public void inborrowInfo(HttpSession session, HttpServletRequest request, HttpServletResponse response)
            throws IOException {

        Map<String, Object> map = new HashMap<String, Object>();
        String page = request.getParameter("page");// 当前页
        String size = request.getParameter("size");// 条数

        int pages = Integer.parseInt(page);
        int rows = Integer.parseInt(size);
        int pageIndex = (pages - 1) * rows;
        map.put("page", pageIndex);// 起始条数
        map.put("size", rows);// 每页条数

        // 查询总数
        int count = infoService.selectCount(map);
        if (count > 0) {
            // 查询内部借阅信息
            List<Info> info = infoService.selectInfo(map);
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("info", info);
            result.put("totalCount", count);
            result.put("page", pages);
            result.put("limit", rows);
            //返回数据
            writeJSON(response, result);
        } else {
            writeJSON(response, "fail");
        }
    }

二、jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/qwjs.css"/>
        <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/layui/css/layui.css"/>

    </head>
    <body style="padding:0 24px">
    <div class=" table-second">
        <div class="table-container">
            <table class="layui-table" id="initInfo">
                <thead id="thead">
                </thead>
                <tbody id="tbody">
                </tbody>
            </table>
        </div>
    </div>
    <div id="page" style="text-align:right"></div>
    <!-- 隐藏当前页和条数,初始值为1,10 -->
    <input type="hidden" id="currPage" value="1">
    <input type="hidden" id="limit" value="10">
    <script src="<%=request.getContextPath()%>/js/jquery-1.11.3.js"></script>
    <script src="<%=request.getContextPath()%>/layui/layui.all.js"></script>
    <!-- 引入js文件 -->
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/infoPage.js"></script>
    </body>
</html>

三、info.js脚本

$(document).ready(function(){
    //ajax请求后台数据
    studentInfo();
    toPage();
});
//请求数据
function studentInfo(){
    var realName = $("#realName").val();//借阅人
    var page=$("#currPage").val();
    var size=$("#limit").val();
    $.ajax({
        type:"post",
        url:"getInfo",//对应controller的URL
        async:false,
        dataType: 'json',
        data:{
            "page":page,
            "size":size,
            },
        success:successFunction
    });
}
//数据请求成功
function successFunction(data){
    console.log(data);
    $("#tbody").html("");
    $("#currPage").val(data.page);//重新赋值当前页
    $("#limit").val(data.limit);//重新赋值本页条数
    var page=data.page;
    var limit=data.limit;
    total=data.totalCount;
    if(data.success=="fail"){//当前无数据时
        $("#tbody").html("<br/><span style='width:200px;height:30px;display:block;margin:0 auto;'>暂无数据</span>");
        return;
    }
    var list = data.extBorrowerInfo;
    var titles = "<tr><th><input type='checkbox'  /></th><th>姓名</th><th>年级</th><th>联系电话</th></tr>";
    $("#thead").html(titles);
    var tr="";
    for(var j=0;j < list.length;j++){
        tr+='<tr><td><input type="checkbox" class="exlCheck" id='+list[j].id+'></td>'
        tr+='<td>'+list[j].userName+'</td>';
        tr+='<td>'+list[j].grade+'</td>';
        tr+='<td>'+list[j].phoneNum+'</td></tr>';
    }
    $("#tbody").html(tr);
}
//分页
function toPage(){
    layui.use('laypage', function(){
          var laypage = layui.laypage;
           laypage.render({
                    elem: 'page' //注意,这里的 page 是 ID,不用加 # 号
                    ,//数据总数,从服务端得到
                    limits:[10,20,30,40,50],
                    prev:"<<",
                    next:">>",
                    theme:"#0099ff",
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                    count:total,
                    curr:page,
                    limit:limit,
                    jump:function(data, first){
                        var page=data.curr;
                        $("#currPage").val(page);
                        var limt=data.limit;
                        $("#limit").val(limt);
                        if(!first){ //点击右下角分页时调用
                            studentInfo();
                        }
                    }
        });
    })
}

四、页面效果图

这里写图片描述

要实现分页功能,你可以使用 Layui 自带的分页组件。具体步骤如下: 1. 首先在 HTML 中添加一个 div,作为分页组件的容器,比如: ```html <div id="pagination"></div> ``` 2. 在 JS 中引入 Layui分页组件,并初始化: ```javascript layui.use(['laypage'], function () { var laypage = layui.laypage; laypage.render({ elem: 'pagination', // 分页容器的 ID count: 25, // 数据总数 limit: 5, // 每页显示的数据条数 theme: '#daba91', // 分页样式 layout: ['page', 'next'], // 分页布局,只显示上一页、下一页和当前页码 jump: function(obj, first){ // 分页回调函数 if(!first){ // 每次分页时都会执行这个函数,可以在这里重新获取数据并更新页面 fetchData(obj.curr, obj.limit); } } }); }); ``` 3. 在 fetchData 函数中,使用 Ajax 或其他方式获取对应页码的数据,并将数据绑定到 Vue 实例的 data 中,例如: ```javascript function fetchData(pageIndex, pageSize) { axios.get(`/api/data?pageIndex=${pageIndex}&pageSize=${pageSize}`) .then(res => { // 更新 Vue 实例的数据 vm.dataList = res.data; }); } ``` 其中,`pageIndex` 和 `pageSize` 分别表示当前页码和每页显示的数据条数。在 Ajax 请求中,需要将这两个参数传递给后端,以获取对应页码的数据。 4. 在 Vue 实例的 data 中定义一个 dataList 数组,用于保存分页数据: ```javascript new Vue({ el: '#app', data: { dataList: [] } }); ``` 5. 在 HTML 中使用 v-for 循环遍历 dataList 数组,渲染数据列表: ```html <table> <thead> <tr> <th>编号</th> <th>名称</th> <th>价格</th> </tr> </thead> <tbody> <tr v-for="(item, index) in dataList" :key="index"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.price}}</td> </tr> </tbody> </table> ``` 这样就可以实现分页功能了。每次切换页码时,都会重新获取对应页码的数据并更新页面。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值