其他分享
首页 > 其他分享> > PageUtil分页工具

PageUtil分页工具

作者:互联网

package cn.gzsxt.util;

import java.util.List;

//封装了一个页面的全部信息
public class PageUtil {

	private int currPage; // 当前页
	private int pageSize; // 每页多少条数据
	private int navCount; // 导航条中的总数字
	private int rowCount; // 总数据量

	private int nextPage; // 下一页
	private int prevPage; // 上一页
	private int pageCount; // 总页数

	private int firstPage; // 首页
	private int lastPage; // 尾页

	private int startNav; // 起始导航数字
	private int endNav; // 结束导航数字

	private List pageData; // 一页的数据内容

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getNavCount() {
		return navCount;
	}

	public void setNavCount(int navCount) {
		this.navCount = navCount;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getFirstPage() {
		return firstPage;
	}

	public void setFirstPage(int firstPage) {
		this.firstPage = firstPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public int getStartNav() {
		return startNav;
	}

	public void setStartNav(int startNav) {
		this.startNav = startNav;
	}

	public int getEndNav() {
		return endNav;
	}

	public void setEndNav(int endNav) {
		this.endNav = endNav;
	}

	public List getPageData() {
		return pageData;
	}

	public void setPageData(List pageData) {
		this.pageData = pageData;
	}

	public PageUtil() {
	}

	public PageUtil(int currPage, int pageSize, int navCount, int rowCount) {
		this.currPage = currPage;
		this.pageSize = pageSize;
		this.navCount = navCount;
		this.rowCount = rowCount;

		this.pageCount = (int) Math.ceil(this.rowCount / (this.pageSize * 1.0));
		this.firstPage = 1;
		this.lastPage = this.pageCount;

		// 避免传过来的currPage不在首页和尾页之间
		this.currPage = this.currPage < this.firstPage ? this.firstPage
				: this.currPage;
		this.currPage = this.currPage > this.lastPage ? this.firstPage
				: this.currPage;

		this.prevPage = this.currPage - 1 <= 0 ? this.firstPage
				: this.currPage - 1;

		this.nextPage = this.currPage + 1 >= this.lastPage ? this.lastPage
				: this.currPage + 1;

		// 计算起始导航数字和结束导航数字: 始终保证this.endNav - this.startNav = this.navCount - 1;
		this.startNav = (this.currPage - this.navCount / 2) < this.firstPage ? this.firstPage
				: (this.currPage - this.navCount / 2);

		this.endNav = (this.startNav + this.navCount - 1) > this.lastPage ? this.lastPage
				: (this.startNav + this.navCount - 1);

		
		if (this.pageCount > this.navCount) {
			this.startNav = this.endNav - this.startNav < this.navCount - 1 ? this.endNav
					- this.navCount + 1
					: this.startNav;
		}

	}

	public static void main(String[] args) {
		PageUtil pu = new PageUtil(1, 10, 10, 89);
		System.out.println("总页数:" + pu.getPageCount());
		System.out.println("上一页:" + pu.getPrevPage());
		System.out.println("下一页:" + pu.getNextPage());
		System.out.println("起始导航数字:" + pu.getStartNav());
		System.out.println("结束导航数字:" + pu.getEndNav());
	}
}

  

标签:navCount,分页,int,void,PageUtil,currPage,return,工具,public
来源: https://www.cnblogs.com/cqming/p/11246590.html