分页工具类——PageInfo

使用方式

前端需要提交参数当前页数currentPage,后台从数据库中查询出数据并将数据封装到PageInfo类中,把封装好的pageInfo对象传递给前端并解析。

      //根据查询结果返回list集合的数据
	  List<Goods> data = userService.findAll();
	  //创建一个PageInfo对象,传入泛型类
      PageInfo<Goods> pageInfo = new PageInfo<Goods>();
      /* 
      封装pageInfo数据 :
     	 第一个参数为list集合的数据
     	 第二个参数为当前页
     	 第三个参数为每页显示数据条数
      */
      pageInfo.setPageInfo(data,currentPage,12);

前端代码

 
    <div class="col-md-12" style="text-align: center;">
        <div class="col-md-6 col-sm-offset-3" id="page_nav_area">

            <nav aria-label="Page navigation ">
                <ul class="pagination">
                 
                    <li><a href="${Path}/UserController/mainUI?currentPage=1">首页a>li>
                     
                    <c:if test="${pageInfo.hasPrePage}">
                        <li>
                        
                            <a href="${Path}/UserController/mainUI?currentPage=${pageInfo.prePage}" aria-label="Previous">
                                <span aria-hidden="true">«span>
                            a>
                        li>
                    c:if>
					
                    <c:forEach var="array" items="${pageInfo.pageArray}">
                        <c:choose>
                       		
                            <c:when test="${pageInfo.currentPage == array}">
                                <li class="active"><a href="#">${array}a>li>
                            c:when>
                            <c:otherwise>
                            
                                <li><a href="${Path}/UserController/mainUI?currentPage=${array}">${array}a>li>
                            c:otherwise>
                        c:choose>
                    c:forEach>
                    
                    <c:if test="${pageInfo.hasNextPage}">
                        <li>
                        
                            <a href="${Path}/UserController/mainUI?currentPage=${pageInfo.nextPage}" aria-label="Next">
                                <span aria-hidden="true">»span>
                            a>
                        li>
                    c:if>
                    
                    <li><a href="${Path}/UserController/mainUI?currentPage=${pageInfo.pageCount}">尾页a>li>
                ul>

                <ul class="pagination">
                
                    <li>
                        <span class="label label-info" style="font-size: 15px;">总${pageInfo.totalRecord}条,共${pageInfo.pageCount}页span>
                    li>
                ul>
            nav>
        div>
    div>

效果

PageInfo中的属性

	//当前页
    private int currentPage;
    //总页数
    private int pageCount;
    //总条数
    private int totalRecord;
    //页面大小
    private int pageSize;
    //是否有下一页
    private boolean hasNextPage;
    //是否有上一页
    private boolean hasPrePage;
    //当前页码数组
    private int[] pageArray;
    //当前页数据
    private List<T> list;
    //下一页
    private int nextPage;
    //上一页
    private int prePage;
    //是否是第一页
    private boolean isFirstPage;
    //是否是最后一页
    private boolean isLastPage;
    //开始行数
    private int startRow;
    //结束行数
    private int endRow;
    //需要显示多少页
    private int navigatePages;
    
	public PageInfo() {
        this.navigatePages = 5;		//默认显示5页
        this.hasPrePage = true;
        this.hasNextPage = true;
        this.isFirstPage = false;
        this.isLastPage = true;
    }

设置分页参数和数据

 	//计算页码逻辑
    public void setPageInfo(List<T> list,int currentPage,int pageSize){
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalRecord = list.size();
        this.pageCount = (int)Math.ceil((totalRecord*1.0) / pageSize);

        //判断是否有无前页或后页
        if (currentPage == 1){
            this.hasPrePage = false;
            this.prePage = -1;
            this.isFirstPage = true;
        }else {
            this.prePage = currentPage - 1;
        }
        if (currentPage == pageCount){
            this.hasNextPage = false;
            this.nextPage = -1;
            this.isLastPage = true;
        }else {
            this.nextPage = currentPage + 1;
        }

        this.pageArray = this.culArray(currentPage,pageSize,totalRecord,pageCount);
        this.startRow = (currentPage-1)*pageSize + 1;
        this.endRow = startRow+pageSize-1;
        if (endRow>totalRecord){
            endRow = totalRecord;
        }

        this.list = new ArrayList<>();
        for (int i=0;i<(endRow-startRow+1);i++){
            this.list.add(list.get(startRow-1+i));
        }
    }

    //计算显示的页数
    private int[] culArray(int currentPage,int pageSize,int totalRecord,int pageCount){
        int[] array;
        if (pageCount<=navigatePages){
            array = new int[pageCount];
            for (int i=0;i<pageCount;i++){
                array[i] = i + 1;
            }
        }else {
            array = new int[navigatePages];
            if (currentPage <= (navigatePages/2)){
                for (int i=0;i<navigatePages;i++){
                    array[i] = i + 1;
                }
            }
            else {
                int x = pageCount-(navigatePages/2);
                if (currentPage>x){
                    int start = pageCount - navigatePages + 1;
                    for (int i=0;i<navigatePages;i++){
                        array[i] = start + i;
                    }
                }else {
                    int start = currentPage - (navigatePages/2);
                    for (int i=0;i<navigatePages;i++){
                        array[i] = start + i;
                    }
                }
            }
        }
        return array;
    }

完整代码

码云地址:分页工具类PageInfo

你可能感兴趣的:(分页工具类——PageInfo)