/**
* 分页器(评论):上一页 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 pagerComment(int totalRows, int pageSize, int start,
Map<Object, Object> bizData, int displayCount, String actionUrl) {
//参数检验
if (start <= 0 || start > totalRows || pageSize <= 0) {
return "";
}
start = (start - 1) * pageSize + 1;
//计算总页数
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));
//当前页是中间某一页
} else {
//当前页不是第一页,则显示“上一页”
if (currentPage > 1) {
bizData.put("start", (currentPage - 1));
pageBuilder.append(new ATag(actionUrl, bizData, "上一页", STYLE_NON_ACTIVE));
}
if (low - 1 > 1) {
bizData.put("start", 1);
pageBuilder.append(new ATag(actionUrl, bizData, "1", STYLE_NON_ACTIVE));
pageBuilder.append("<span>...</span>");
while(low < currentPage) {
bizData.put("start", low);
pageBuilder.append(new ATag(actionUrl, bizData, "" + low++, STYLE_NON_ACTIVE));
}
} else {
low = 1;
while(low < currentPage) {
bizData.put("start", low);
pageBuilder.append(new ATag(actionUrl, bizData, "" + low++, STYLE_NON_ACTIVE));
}
}
pageBuilder.append(new ATag("javascript:void(0);", new HashMap<Object, Object>(), "" + currentPage, STYLE_ACTIVE));
int temp = currentPage + 1;
if (totalPages - high > 1) {
while(temp <= high) {
bizData.put("start", temp);
pageBuilder.append(new ATag(actionUrl, bizData, "" + temp++, STYLE_NON_ACTIVE));
}
pageBuilder.append("<span>...</span>");
bizData.put("start", totalPages);
pageBuilder.append(new ATag(actionUrl, bizData, "" + totalPages, STYLE_NON_ACTIVE));
} else {
while(temp <= totalPages) {
bizData.put("start", temp);
pageBuilder.append(new ATag(actionUrl, bizData, "" + temp++, STYLE_NON_ACTIVE));
}
}
//当前页不是最后一页,则显示“下一页”
if (currentPage < totalPages) {
bizData.put("start", currentPage + 1);
pageBuilder.append(new ATag(actionUrl, bizData, "下一页", STYLE_NON_ACTIVE));
}
}
return pageBuilder.toString();
}
分页算法(三)
最新推荐文章于 2023-03-18 12:02:54 发布
