amy_888 发表于 2016-12-17 06:21:59

分页【抄自ajax-solr-master,不懂先记下】

package com.risejoin.yunfang.util;
import java.util.ArrayList;
import java.util.List;
public class Page {
private int currentPage = 1;// 当前页数
public int totalPages = 0;// 总页数
private int pageSize = 0;// 每页显示数
private int totalRows = 0;// 总数据数
private int startNum = 0;// 开始记录
private int endNum = 0;
private int nextPage = 0;// 下一页
private int previousPage = 0;// 上一页
private boolean hasNextPage = false;// 是否有下一页
private boolean hasPreviousPage = false;// 是否有前一页
private List<String> pageNum;
public Page(int pageSize, int currentPage, int totalRows) {
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalRows = totalRows;
if ((totalRows % pageSize) == 0) {
totalPages = totalRows / pageSize;
} else {
totalPages = totalRows / pageSize + 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
currentPage = totalPages;
} else {
hasNextPage = true;
}
if (currentPage <= 1) {
hasPreviousPage = false;
currentPage = 1;
} else {
hasPreviousPage = true;
}
startNum = (currentPage - 1) * pageSize;
endNum = startNum + pageSize;
nextPage = currentPage + 1;
if (nextPage >= totalPages) {
nextPage = totalPages;
}
previousPage = currentPage - 1;
if (previousPage <= 1) {
previousPage = 1;
}
int inner = 1;
int outer = 0;
int cur = this.currentPage;
int total= totalPages;
int from = cur - inner;
int to = cur + inner;
List<Integer> list = new ArrayList<Integer>();
if(to > total){
from = Math.max(0, from - (to - total));
to = total;
}
if(from < 1){
to = Math.min(total, to + (1 - from));
from = 1;
}
list.add(1);
if(total<=1){
list.remove((Integer)1);
}
for (int i = 2; i <= Math.min(1 + outer, from - 1); i++) {
list.add(i);
}
if (1 + outer == from - 2) {
list.add(from - 1);
}
for (int i = Math.max(2, from); i <= Math.min(to, total - 1); i++) {
list.add(i);
}
if (total - outer == to + 2) {
list.add(to + 1);
}
for (int i = Math.max(total - outer, to + 1); i < total; i++) {
list.add(i);
}
if (total > 1) {
list.add(total);
}
List<String> pageNum = new ArrayList<String>();
Integer prev = null;
for (int i = 0, l = list.size(); i < l; i++) {
if (prev!=null && list.get(i) > prev + 1) pageNum.add("...");
pageNum.add(String.valueOf(list.get(i)));
prev = list.get(i);
}
this.pageNum = pageNum;
}

public List<String> getPageNum() {
return pageNum;
}

public boolean isHasNextPage() {
return hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
/**
* @return the nextPage
*/
public int getNextPage() {
return nextPage;
}
/**
* @param nextPage
*            the nextPage to set
*/
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
/**
* @return the previousPage
*/
public int getPreviousPage() {
return previousPage;
}
/**
* @param previousPage
*            the previousPage to set
*/
public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}
/**
* @return the currentPage
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
*            the currentPage to set
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return the pageSize
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize
*            the pageSize to set
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return the totalPages
*/
public int getTotalPages() {
return totalPages;
}
/**
* @param totalPages
*            the totalPages to set
*/
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
/**
* @return the totalRows
*/
public int getTotalRows() {
return totalRows;
}
/**
* @param totalRows
*            the totalRows to set
*/
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
/**
* @param hasNextPage
*            the hasNextPage to set
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @param hasPreviousPage
*            the hasPreviousPage to set
*/
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
/**
* @return the startNum
*/
public int getStartNum() {
return startNum;
}
/**
* @param startNum
*            the startNum to set
*/
public void setStartNum(int startNum) {
this.startNum = startNum;
}
public int getEndNum() {
return endNum;
}
public void setEndNum(int endNum) {
this.endNum = endNum;
}
}
页: [1]
查看完整版本: 分页【抄自ajax-solr-master,不懂先记下】