感觉javaeye的分页非常的好,所以就仿写了一个类似功能的分页组件
截图:
代码:
package com.yeyong.ah.util;
import com.yeyong.ah.model.PageInfo;
public class BuildPageInfo {
private static final long serialVersionUID = -6177964015900139500L;
public static String build(PageInfo pageInfo, boolean showTotal) {
int recordCount = pageInfo.getRecordCount();
int pageCount = pageInfo.getPageCount();
int recordPerPage = pageInfo.getRecordPerPage();
int curPage = pageInfo.getCurPage();
String url = pageInfo.getUrl();
String parmPrefix = pageInfo.getParmPrefix();
StringBuilder sb = new StringBuilder(200);
sb.append("<div class='page'>").append("<ul>");
if(showTotal) {
sb.append("<li><a href='javascript:void(0)' class='pagetotal'> 共");
sb.append(recordCount).append("条 </a></li>");
}
char split = url.indexOf('?') == -1 ? '?': '&';
StringBuilder prefix = new StringBuilder(200);
prefix.append("<li><a href='javascript:void(0)' onclick=\"window.location.href='");
prefix.append(url).append(split).append(parmPrefix).append("page=");
StringBuilder sizeAndTotal = new StringBuilder(200);
sizeAndTotal.append('&').append(parmPrefix).append("pagesize=").append(recordPerPage);
sizeAndTotal.append('&').append(parmPrefix).append("total=").append(recordCount);
String suffix = "</a></li>";
if (curPage > 1) {
//sb.append(prefix).append(1).append(sizeAndTotal).append("'\" class='pre'> 首页 ").append(suffix);
sb.append(prefix).append(curPage - 1).append(sizeAndTotal).append("'\" class='pre'>? 上一页 ").append(suffix);
} else {
//sb.append("<li><span class='disable'> 首页 </span></li>");
sb.append("<li><span class='disable'>? 上一页 </span></li>");
}
int size = 3;
if (pageCount <= size) {
for (int i = 1; i <= pageCount; i++) {
if (i!=curPage ) {
sb.append(prefix).append(i).append(sizeAndTotal).append("'\" class='pageNo'>").append(i).append(suffix);
} else {
sb.append("<li><span class='curpageno'>").append(i).append("</span></li>");
}
}
} else {
if (curPage != 1) {
sb.append(prefix).append(1).append(sizeAndTotal).append("'\" class='pageNo'>").append(1).append(suffix);
} else {
sb.append("<li><span class='curpageno'>").append(1).append("</span></li>");
}
int start = (curPage - 1) / size * size + 1;
int end = pageCount > (start + size - 1 ) ? (start + size - 1) : pageCount;
start = (start==1) ? 2 : start;
for (int i = start; i <= end ; i++) {
if (i!=curPage ) {
sb.append(prefix).append(i).append(sizeAndTotal).append("'\" class='pageNo'>").append(i).append(suffix);
} else {
sb.append("<li><span class='curpageno'>").append(i).append("</span></li>");
}
}
if (end < pageCount) {
boolean hasPutDot = false;
hasPutDot = addQuickPage(sb, prefix, sizeAndTotal, suffix, curPage, pageCount, 0, 30, 70, 200000, 50, hasPutDot);
hasPutDot = addQuickPage(sb, prefix, sizeAndTotal, suffix, curPage, pageCount, 0, 300, 700, 10000, 500, hasPutDot);
hasPutDot = addQuickPage(sb, prefix, sizeAndTotal, suffix, curPage, pageCount, 200, 1000, 3000,20000, 2000, hasPutDot);
hasPutDot = addQuickPage(sb, prefix, sizeAndTotal, suffix, curPage, pageCount, 1000, 3000, 7000, 30000, 5000, hasPutDot);
hasPutDot = addQuickPage(sb, prefix, sizeAndTotal, suffix, curPage, pageCount, 3000, 7000, 20000, 100000, 50000, hasPutDot);
sb.append("<li><a href='javascript:void(0)' class='pageNo'>..</a></li>");
start = end < pageCount - 2 ? pageCount-1:pageCount;
for (int j = start; j <= pageCount ; j++) {
sb.append(prefix).append(j).append(sizeAndTotal).append("'\" class='pageNo'>").append(j).append(suffix);
}
}
}
if (curPage < pageCount) {
sb.append(prefix).append(curPage + 1).append(sizeAndTotal).append("'\" class='nxt'> 下一页 ?").append(suffix);
//sb.append(prefix).append(pageCount).append(sizeAndTotal).append("'\" class='nxt'> 尾页 ").append(suffix);
} else {
sb.append("<li><span class='disable'> 下一页 ?</span></li>");
//sb.append("<li><span class='disable'> 尾页 </span></li>");
}
sb.append("<li><a href='javascript:void(0)' onclick=\"window.location.href='");
sb.append(url).append(split).append(parmPrefix).append("refresh").append("'\" class='pagereload'> 刷新 ").append(suffix);
sb.append("</ul>").append("</div>");
return sb.toString();
}
private static boolean addQuickPage(StringBuilder sb, StringBuilder prefix,
StringBuilder sizeAndTotal, String suffix, int curPage, int pageCount,
int minPage, int maxPage, int minPageCount, int maxPageCount, int quickPage, boolean hasPutDot) {
if(curPage >= minPage && curPage <= maxPage && pageCount >= minPageCount && pageCount <= maxPageCount) {
if (!hasPutDot) {
sb.append("<li><a href='javascript:void(0)' class='pageNo'>..</a></li>");
hasPutDot = true;
}
sb.append(prefix).append(quickPage).append(sizeAndTotal).append("'\" class='pageNo'>").append(quickPage).append(suffix);
}
return hasPutDot;
}
}