/** * */ package com.sobanks.utils;
import java.util.List;
/** * * @author hh * */ public class Pagination{ // 總共的數據量 private int total;
// 每頁顯示多少條 private static int pageSize=2;
// 共有多少頁 private int totalPage;
// 當前是第幾頁 private int index;
// 數據 private List<?> data;
// 鏈接路徑 private String path = "";
/** * 頁碼HTML信息 */ @SuppressWarnings("unused") private String pageHTML;
private int startPage; // 開始頁面
private int endPage; // 結束頁面
private int displayNum = 20; // 顯示的頁數 public Pagination(int total, int index, String path) { super(); this.total = total; this.index = index; this.path = path; }
public Pagination() { super(); }
public static void setPageSize(int pageSize) { Pagination.pageSize = pageSize; }
/** * @return the startPage */ public int getStartPage() { return startPage; }
/** * @return the endPage */ public int getEndPage() { return endPage; }
/** * @return the path */ public String getPath() { return path; }
public void setIndex(int index) { this.index = index; } /** * 返回起始行數 * @param index * @return */ public static int getStartRow(int index){ return index-1; } public static int getPageSize(){ return pageSize; } /** * 設置路徑前綴,頁面第一頁index爲1 * * @param path * 此path含有參數形式,如/aa/article?page=,或者/bb/article/list/ */ public void setPath(String path) { this.path = path; }
public int getTotalPage() { return (this.total + pageSize - 1) / pageSize; }
public int getIndex() { return index; }
public List<?> getData() { return data; }
public void setData(List<?> data) { this.data = data; }
/** * @return the total */ public int getTotal() { return total; }
/** * @param total * the total to set */ public void setTotal(int total) { this.total = total; } public String getPageHTML() { totalPage = getTotalPage(); StringBuffer displayInfo = new StringBuffer(); if (totalPage != 0 && pageSize != 0) { displayInfo.append("<div class='pager'>"); displayInfo.append("<span class='item nolink'>共<span class='num'>" + totalPage + "</span>頁 <span class='num'>" + total + "</span>條記錄</span> "); //displayInfo.append("<span class='item nolink'>/<span class='num'>" + total + "</span>條記錄</span>"); // 判斷若是當前是第一頁 則「首頁」和「第一頁」失去連接 if (index <= 1) { displayInfo.append("<span class='item nolink'>首頁</span> "); displayInfo.append("<span class='item nolink'>上一頁</span> "); } else { displayInfo.append("<span class='item'><a href='" + path + "1'>首頁</a></span> "); displayInfo.append("<span class='item'><a href='" + path + (index - 1) + "'>上一頁</a></span> "); }
countPages(); displayInfo.append("<span class='item nums'>"); for (int i = startPage; i <= endPage; i++) { if (i == index) { displayInfo.append("<span class='nolink'>" + i + "</span> "); } else { displayInfo.append("<a href='" + path + i + "'>" + i + "</a> "); } } displayInfo.append("</span> ");
if (index >= totalPage) { displayInfo.append("<span class='item nolink'>下一頁</span> "); displayInfo.append("<span class='item nolink'>尾頁</span>"); } else { displayInfo.append("<span class='item'><a href='" + path + (index + 1) + "'>下一頁</a></span> "); displayInfo.append("<span class='item'><a href='" + path + totalPage + "'>尾頁</a></span>"); } displayInfo.append("</div>"); } return displayInfo.toString(); }
/** * 計算起始頁和結束頁 */ public void countPages() {
if (index - displayNum / 2 < 1) { startPage = 1; endPage = displayNum > totalPage ? totalPage : displayNum; } else if (index + displayNum / 2 > totalPage) { int n = totalPage - displayNum + 1; startPage = n > 0 ? n : 1; endPage = totalPage; } else { startPage = index - displayNum / 2; endPage = startPage + displayNum - 1; } }
/** * @param pageHTML the pageHTML to set */ public void setPageHTML(String pageHTML) { this.pageHTML = pageHTML; }
public static void main(String[] args) { Pagination p = new Pagination(); p.setTotal(1002); p.setPath("/bb/article/list/"); p.setIndex(33); System.out.println(p.getPageHTML()); } }