天天看點

pageInfo類屬性含義小結

pageInfo類屬性含義小結

pageInfo類可用于分頁操作。以下為屬性含義:

private static final long serialVersionUID = 1L;
    private int pageNum;      //目前頁的頁碼
    private int pageSize;      //每頁的數量
    private int size;            //目前頁的數量

可以在頁面中"顯示startRow到endRow 共size條資料"
    private int startRow; //目前頁面第一個元素在資料庫中的行号
    private int endRow;//目前頁面最後一個元素在資料庫中的行号
    
    private long total;     //總記錄數
    private int pages;       //總頁數
    private List<T> list;      //結果集
    
    private int prePage;    //上一頁
    private int nextPage;   //下一頁
    
    private boolean isFirstPage;   //是否為第一頁
    private boolean isLastPage;   //是否為最後一頁
    private boolean hasPreviousPage;       //是否有前一頁
    private boolean hasNextPage;          //是否有下一頁
    
    private int navigatePages;          //導航頁碼數
    private int[] navigatepageNums;       //所有導航頁号
    private int navigateFirstPage;       //導覽列上的第一頁
    private int navigateLastPage;     //導覽列上的最後一頁

           

構造函數

public PageInfo() {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
    }
 /** 
     * 包裝Page對象 
     * 
     * @param list 
     */  
    public PageInfo(List<T> list) {
        this(list, 8);
    }
 /** 
     * 包裝Page對象 
     * 
     * @param list          page結果 
     * @param navigatePages 頁碼數量 
     */  
    public PageInfo(List<T> list, int navigatePages) {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
        if (list instanceof Page) {
            Page page = (Page)list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
            this.total = page.getTotal();
            if (this.size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
            //由于結果是>startRow的,是以實際的需要+1  
                this.startRow = page.getStartRow() + 1;
                 //計算實際的endRow(最後一頁的時候特殊)  
                this.endRow = this.startRow - 1 + this.size;
            }
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.pages = this.pageSize > 0 ? 1 : 0;
            this.list = list;
            this.size = list.size();
            this.total = (long)list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }

        if (list instanceof Collection) {
            this.navigatePages = navigatePages;
            this.calcNavigatepageNums();       //計算導航頁 
            this.calcPage();          //計算前後頁,第一頁,最後一頁  
            this.judgePageBoudary();     //判斷頁面邊界
        }

    }
           

計算導航頁

private void calcNavigatepageNums() {
        int i;
        if (this.pages <= this.navigatePages) {
            this.navigatepageNums = new int[this.pages];

            for(i = 0; i < this.pages; ++i) {
                this.navigatepageNums[i] = i + 1;
            }
        } else {
            this.navigatepageNums = new int[this.navigatePages];
            i = this.pageNum - this.navigatePages / 2;
            int endNum = this.pageNum + this.navigatePages / 2;
            int i;
            if (i < 1) {
                i = 1;

                for(i = 0; i < this.navigatePages; ++i) {
                    this.navigatepageNums[i] = i++;
                }
            } else if (endNum > this.pages) {
                endNum = this.pages;

                for(i = this.navigatePages - 1; i >= 0; --i) {
                    this.navigatepageNums[i] = endNum--;
                }
            } else {
                for(i = 0; i < this.navigatePages; ++i) {
                    this.navigatepageNums[i] = i++;
                }
            }
        }

    }
           

計算前後頁,第一頁,最後一頁

private void calcPage() {
        if (this.navigatepageNums != null && this.navigatepageNums.length > 0) {
            this.navigateFirstPage = this.navigatepageNums[0];
            this.navigateLastPage = this.navigatepageNums[this.navigatepageNums.length - 1];
            if (this.pageNum > 1) {
                this.prePage = this.pageNum - 1;
            }

            if (this.pageNum < this.pages) {
                this.nextPage = this.pageNum + 1;
            }
        }

    }
           

判斷頁面邊界

private void judgePageBoudary() {
        this.isFirstPage = this.pageNum == 1;
        this.isLastPage = this.pageNum == this.pages || this.pages == 0;
        this.hasPreviousPage = this.pageNum > 1;
        this.hasNextPage = this.pageNum < this.pages;
    }
           

轉載來源:https://www.e-learn.cn/content/qita/758537