/**
* 分页器(照片):上一页 1 ... 8 9 10 11 12 13 14 ... 16 下一页
* @param totalRows 总记录数
* @param pageSize 每页显示记录数
* @param start 开始记录数
* @param bizData 业务参数
* @param displayCount 跨度
* @param actionUrl 因为链接
* @return
*/
public static String generatePager(int totalRows, int pageSize, int start,
Map<Object, Object> bizData, int displayCount, String actionUrl) {
//参数检验
if (start <= 0 || start > totalRows || pageSize <= 0) {
return "";
}
//计算总页数
int totalPages = 1;
if (totalRows != pageSize) {
totalPages = (int) Math.ceil((double) totalRows / (double) pageSize);
}
//计算当前页
int currentPage = start / pageSize;
if (start % pageSize != 0) {
currentPage ++;
}
//计算左右跨度显示的起始页
int low = currentPage - displayCount > 0 ? currentPage - displayCount : 1;
int high = currentPage + displayCount;
StringBuilder pageBuilder = new StringBuilder(100);
//总记录数小于每页记录数,则只显示一页
if (totalRows <= pageSize) {
pageBuilder.append(new ATag("javascript:void(0);", new HashMap<Object, Object>(), "1", STYLE_ACTIVE));
//pageBuilder.append("1");
//当前页是中间某一页
} else {
//当前页不是第一页,则显示“上一页”
if (currentPage > 1) {
bizData.put("start", (currentPage - 2) * pageSize + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "上一页", STYLE_NON_ACTIVE));
//pageBuilder.append(" 上一页 ");
}
if (low - 1 > 1) {
bizData.put("start", 1);
pageBuilder.append(new ATag(actionUrl, bizData, "1", STYLE_NON_ACTIVE));
//pageBuilder.append(" 1 ");
pageBuilder.append("<span>...</span>");
while(low < currentPage) {
bizData.put("start", (low - 1) * pageSize + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "" + low++, STYLE_NON_ACTIVE));
//pageBuilder.append(" " + low++ + " ");
}
} else {
low = 1;
while(low < currentPage) {
bizData.put("start", (low - 1) * pageSize + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "" + low++, STYLE_NON_ACTIVE));
//pageBuilder.append(" " + low++ + " ");
}
}
pageBuilder.append(new ATag("javascript:void(0);", new HashMap<Object, Object>(), "" + currentPage, STYLE_ACTIVE));
//pageBuilder.append(" " + currentPage + " ");
int temp = currentPage + 1;
if (totalPages - high > 1) {
while(temp <= high) {
bizData.put("start", (temp - 1) * pageSize + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "" + temp++, STYLE_NON_ACTIVE));
//pageBuilder.append(" " + temp ++ + " ");
}
pageBuilder.append("<span>...</span>");
bizData.put("start", (totalPages - 1) * pageSize + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "" + totalPages, STYLE_NON_ACTIVE));
//pageBuilder.append(" " + totalPages + " ");
} else {
while(temp <= totalPages) {
bizData.put("start", (temp - 1) * pageSize + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "" + temp++, STYLE_NON_ACTIVE));
//pageBuilder.append(" " + temp ++ + " ");
}
}
//当前页不是最后一页,则显示“下一页”
if (currentPage < totalPages) {
bizData.put("start", currentPage * pageSize + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "下一页", STYLE_NON_ACTIVE));
//pageBuilder.append(" 下一页 ");
}
}
return pageBuilder.toString();
}
分页算法(二)
最新推荐文章于 2022-09-17 15:53:55 发布