1. 程式人生 > >通用分頁---pageBean

通用分頁---pageBean

簡單的網頁分頁Bean:

 

public class PageBean<T> {
    private List<T> data;//當前頁資料列表
    private int firstPage = 1;//首頁
    private int prePage;//上一頁
    private int curPage;//當前頁
    private int nextPage;//下一頁
    private int totalPage;//總頁數,或末頁
    private int count;//總記錄數
    private int pageSize;//每頁大小,用於設定每頁顯示多少條資料

    private int startIndex;//每次查詢開始的頁數
    private int start;//頁碼開始
    private int end;//頁碼結束

    //頁碼為前4後5
    private void jisuan() {
        if (getTotalPage() < 10){
            start = firstPage;
            end = totalPage;
        }else {
            start = curPage - 4;
            end = curPage + 5;
            if (start < 1){
                start = firstPage;
                end = 10;
            }
            if (end > totalPage){
                end = totalPage;
                start = totalPage - 9;
            }
        }
    }

    public PageBean(int curPage, int pageSize) {//初始化當前頁數、每頁大小、前一頁數後一頁數
        this.curPage = curPage;
        this.pageSize = pageSize;
        this.prePage = curPage - 1;
        this.nextPage = curPage + 1;
    }

    public PageBean(List<T> data, int firstPage, int prePage, int curPage, int nextPage, int totalPage, int count, int pageSize) {
        this.data = data;
        this.firstPage = firstPage;
        this.prePage = prePage;
        this.curPage = curPage;
        this.nextPage = nextPage;
        this.totalPage = totalPage;
        this.count = count;
        this.pageSize = pageSize;
    }

    public int getStartIndex() {
        startIndex = (curPage - 1) * pageSize;
        return startIndex;
    }

    public void setCount(int count) {
        this.count = count;
        totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;//計算總頁數
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getStart() {
        jisuan();
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        jisuan();
        return end;
    }
    //僅列出改動的getter 和 setter 方法
    //...
}
配套JS程式碼
將此方法直接使用或放入JS目錄下即可(也可以zixing新增分頁樣式)
//引數1:分頁物件
//引數2:訪問的地址
//引數3 以後 都以key=value的字串傳入
//該方法用來拼接字串的分頁 多個li 返回一個字串
//呼叫案例 如下
//var pageStr= addPage(resultInfo.data,"http://localhost:8080/travel/favoriterank.html" ,"key=value","key=value" ,"key=value");
function addPage(pageBean , myUrl){

    //拼接分頁
    //需要的引數格式 key=value
    var paramsTemp ="";
    for(var i = 2 ; i <arguments.length ; i++){
        if(i==2){
            paramsTemp+="&"
        }
        //拼接引數
        paramsTemp+=arguments[i];
        //除了最後一個引數不拼接& 其他的都拼接  &key=value&key=value&key=value
        //http://localhost:8080/專案名稱/favoriterank.html?curPage=1&key=value&key=value&key=value
        if(i!=arguments.length-1){
            paramsTemp+="&";
        }
    }


    //分頁
    var curPageInt = pageBean.curPage;
    //計算前一頁
    var backPage = pageBean.prePage;
    //計算後一頁
    var afterPage = pageBean.nextPage;
    //拼接首頁
    var   pageStr =" <li><a href=\""+myUrl+"?curPage=1"+paramsTemp+"\">首頁</a></li>";
    //拼接上頁
    if (curPageInt > 1){
        pageStr +=" <li><a href=\""+myUrl+"?curPage="+backPage+""+paramsTemp+"\">上頁</a></li>";
    }

    //拼接中間頁碼
    for(var i = pageBean.start ; i <= pageBean.end ; i ++ ){
        if(i == pageBean.curPage){
            //pageStr +=" <li class='curPage'><a href=\""+myUrl+"?curPage="+i+""+paramsTemp+"\">"+i+"</a></li>";
            pageStr +=" <li class='curPage'><a href='"+myUrl+"?curPage="+i+""+paramsTemp+"'>"+i+"</a></li>";
            //地址?curPage=1&key=value&key=value&key=value

        }else{
           // pageStr +=" <li><a href=\""+myUrl+"?curPage="+i+"\""+paramsTemp+">"+i+"</a></li>";
            pageStr +=" <li><a href='"+myUrl+"?curPage="+i+""+paramsTemp+"'>"+i+"</a></li>";
        }
    }
    //拼接下頁
    //pageStr +=" <li><a href=\""+myUrl+"?"+paramsTemp+"&curPage="+afterPage+"\">下頁</a></li>";
    if (curPageInt < pageBean.totalPage){
        pageStr +=" <li><a href='"+myUrl+"?curPage="+afterPage+""+paramsTemp+"'>下頁</a></li>";
    }
    //拼接尾頁
    pageStr +=" <li><a href='"+myUrl+"?curPage="+pageBean.totalPage+""+paramsTemp+"'>尾頁</a></li>";
    //pageStr +=" <li><a href=\""+myUrl+"?"+paramsTemp+"&curPage="+pageBean.totalPage+"\">尾頁</a></li>";
    return pageStr ;
}