1. 程式人生 > >分頁工具類

分頁工具類

import java.util.List;
 
/**
 * 分頁類
 * @author hxl
 *
 * @param <T>
 */
 
public class Page<T> {
 
	private final static int PAGEITEMCOUNT = 10;  //顯示頁碼條目數,即頁碼數量頂多是10個
	
	private List<T> list;			//儲存查詢的結果集合
	private int totalRecord;		//總記錄數
	private int pageSize = 5;       //頁面顯示的數目
	private Integer totalPage;		    //總頁碼數
	private int currentPage = 1;    //當前頁碼
	private int previousPage;      	//前一頁
	private int nextPage;			//後一頁
	private int[] pageBar;			//條目數
	private int startIndex;			//開始頁
	private int endIndex;			//結束頁
	
/*	public int getSelectEndIndex() {     //oracle中的結束查詢條件,供傳入資料庫引數使用
		return this.getStartIndex()+this.pageSize - 1;  
	}*/
	
	public int getStartIndex() {
		this.startIndex = (this.currentPage-1)*this.pageSize;   
		return startIndex;
	}
	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}
	public int getEndIndex() {   //從資料庫中獲取的結束索引,供頁面使用
		int end = this.getStartIndex() + this.getPageSize();  //不包含最後一條記錄-1
		if(end>this.getTotalRecord()){
			end = this.getStartIndex() + (this.getTotalRecord()%this.getPageSize());
		}
		this.endIndex = end;
		return this.endIndex;
	}
	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public int getTotalRecord() {
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getTotalPage() {     		//得到總頁碼數
		if(this.totalRecord%this.pageSize==0){
			this.totalPage = this.totalRecord/this.pageSize;
		}else{
			this.totalPage = this.totalRecord/this.pageSize+1;
		}
		
		return totalPage;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPreviousPage() {
		if(this.currentPage-1<1){      //如果上一頁小於1,則說明當前頁碼已經是第一頁了
			this.previousPage = 1;
		}else{
			this.previousPage = this.currentPage-1;
		}
		return previousPage;
	}
	
	public int getNextPage() {  		 
		if(this.currentPage+1>=this.totalPage){   //如果下一頁大於總數頁,則說明當前頁碼已經是最後一頁了
			this.nextPage = this.totalPage;
		}else{
			this.nextPage = this.currentPage +1;
		}
		return nextPage;
	}
	
	public int[] getPageBar() {
		int startPage;      //記住頁碼的起始位置
		int endPage;		//記住頁碼的結束位置,因為頁碼條目是既定的,由startpage,endpage兩個變數通過迴圈就可以得全部頁碼
		int pageBar[] = null;
		if(this.getTotalPage() <= PAGEITEMCOUNT){    //當總頁碼不足或等於既定頁面大小時
			pageBar = new int[this.totalPage];
			startPage = 1;
			endPage = this.totalPage;
		}else{					//當總頁碼大於既定頁面大小時
			pageBar = new int[PAGEITEMCOUNT];
			startPage = this.currentPage - (PAGEITEMCOUNT/2-1);    //為了保證當前頁在中間
			endPage = this.currentPage + PAGEITEMCOUNT/2;
 
			if(startPage<1){
				startPage = 1;
				endPage = PAGEITEMCOUNT;
			}
			
			if(endPage>this.totalPage){
				endPage = this.totalPage;
				startPage = this.totalPage - (PAGEITEMCOUNT-1);
			}
		}
		
		int index = 0;
		for(int i=startPage;i<=endPage;i++){
			pageBar[index++] = i;
		}
		
		this.pageBar = pageBar;
		return this.pageBar;
	}
	
}