请教各位大侠显示数字页码分页问题

abcxiaoye 2015-01-19 10:41:20

protected int recPerPage = 10; // 分页中每页的记录数
protected Locale locale = null; // 本地语言信息
protected MessageResources message = null;// 消息资源
// 页容量
//private int pageSize = 10;
// 总页数
private int pages;
// 总记录数
private int allCount;
// 开始页码
private int startPage = 1;
// 结束页码
private int endPage;
private int iCurrPage;//当前页

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 获取Locale信息
this.locale = this.getLocale(request);
// 获取消息资源对象
this.message = this.getResources(request);
// 如果用户没有登录,跳转到登录页面
//if (request.getSession().getAttribute("user") == null) {
//return mapping.findForward("login");
//}
return super.execute(mapping, form, request, response);
}

/**
* 分页
* @param hql hql语句(不包含select,从from子句开始)
* @param recPerPage 每页的记录数
* @param currPage 当前页码
* @param action 请求提交的action地址
* @param where 条件数组
* @return Map集合(装载结果集对象及分页条)
*/
public Map getPage(String hql, int recPerPage, String currPage,
String action, Object[] where) {
// 实例化一个Map对象
Map map = new HashMap();
// 分页条
StringBuffer pagingBar = new StringBuffer();
List list = null; // 结果集
iCurrPage = 1; // 当前页码
// 如果传递了页码则对当前页码赋值
if (currPage != null && !currPage.isEmpty()) {
iCurrPage = Integer.parseInt(currPage);
}
// 实例化SupperDao对象
SupperDao dao = new SupperDao();
pages = 0; // 总页数
// 获取总记录数
Long l = (Long) dao.uniqueResult("select count(*) " + hql, where);
allCount = l.intValue(); // 将总记录数转为int型
if (allCount > 0) {
// 计算总页数
if (allCount % recPerPage == 0) {
pages = allCount / recPerPage;
} else {
pages = allCount / recPerPage + 1;
}
if (iCurrPage > pages) {
iCurrPage = pages;
}
if (iCurrPage < 1) {
iCurrPage = 1;
}

// 显示页码计算
if (iCurrPage > 0) {
startPage = iCurrPage;
endPage = iCurrPage+9;
}
if (endPage > pages) {
if (pages>10)
startPage = pages - 9;
else
startPage = 1;
endPage = pages;
}
if (startPage < 1) {
startPage = 1;
}
// 分页查询获取结果集
list = dao.findPaging(hql, (iCurrPage - 1) * recPerPage,
recPerPage, where);
// 构造分页条
pagingBar.append("<form name='pagingForm' action='" + action
+ "' method='post'>");
// 在分页条中添加总记录数
pagingBar.append(message.getMessage(locale, "page.totalRecord")
+ allCount);
pagingBar.append(" ");
pagingBar.append(message.getMessage(locale, "system.total") + " "
+ pages + " " + message.getMessage(locale, "page.page"));
pagingBar.append(" ");
// 页数大于1显示上一页超链接,否则不显示超链接
if (iCurrPage > 1) {
pagingBar.append("<a href=" + action + "&currPage=1>"
+ message.getMessage(locale, "page.first") + "</a>");
pagingBar.append(" ");
pagingBar.append("<a href=" + action + "&currPage="
+ (iCurrPage - 1) + ">"
+ message.getMessage(locale, "page.previous") + "</a>");
pagingBar.append(" ");
} else {
pagingBar.append(message.getMessage(locale, "page.first"));
pagingBar.append(" ");
pagingBar.append(message.getMessage(locale, "page.previous"));
pagingBar.append(" ");
}
//显示数字页码,显示不出来,不知为什么?

for(int i=startPage; i<=endPage; i++){
if(i==iCurrPage){
pagingBar.append("<font color='red'>" + iCurrPage + "</font>");
pagingBar.append(" ");
}else{
pagingBar.append("<a href=" + action + "&currPage=" + i + ">"+i+"</a> ");
pagingBar.append(" ");
}
}
// 显示当前页码


// 页数小于总页数显示下一页超链接,否则不显示超链接
if (iCurrPage < pages) {
pagingBar.append("<a href=" + action + "&currPage="
+ (iCurrPage + 1) + ">"
+ message.getMessage(locale, "page.next") + "</a>");
pagingBar.append(" ");
pagingBar.append("<a href=" + action + "&currPage=" + pages
+ ">" + message.getMessage(locale, "page.last")
+ "</a>");
} else {
pagingBar.append(message.getMessage(locale, "page.next"));
pagingBar.append(" ");
pagingBar.append(message.getMessage(locale, "page.last"));
}
pagingBar.append(" ");
pagingBar.append("<input type='text' name='currPage' size='1'>");
pagingBar.append("<input type='submit' value='GO'>");
pagingBar.append("</form>");
}
map.put("list", list);// 结果集
map.put("bar", pagingBar.toString());// 分页条的字符串形式
return map;
}
}

数字页码显示:
第1页
1 2 3 4 5 6 7 8 9 10
点击第2页
2 3 4 5 6 7 8 9 10 11
点击第3页
3 4 5 6 7 8 9 10 11 12
也就是说点击第几页,它就跳转到第几页开始显示,这样晃得眼花。
能不能点击前面9个页码,数字不变;点击最后一个页码数字才变?
比如:一开始显示
1 2 3 4 5 6 7 8 9 10
点击第10页时页码变为
11 12 13 14 15 16 17 18 19 20

上面代码怎么修改?请各位大侠指点,谢谢!!
...全文
176 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yschat2012 2015-01-22
  • 打赏
  • 举报
回复
高手都跑哪去了?
abcxiaoye 2015-01-21
  • 打赏
  • 举报
回复
引用 4 楼 yschat2012 的回复:
把  // 显示页码计算  
            if (iCurrPage > 0) {  
                startPage = iCurrPage;  
                endPage = iCurrPage+9;  
            }  
            if (endPage > pages) {  
                if (pages>10)  
                    startPage = pages - 9;  
                else  
                    startPage = 1;  
                endPage = pages;  
            }  
            if (startPage < 1) {  
                startPage = 1;  
            }
替换成
startPage=iCurrPage>9?iCurrPage+1>pages?pages>10?pages-9:1:iCurrPage+1:1;
endPage=iCurrPage>9?iCurrPage+1>pages?pages:iCurrPage+10:pages>10?10:pages;
然后修改成你要的效果
我试了一下,点击前9页,1 2 3 4 5 6 7 8 9 10 这些数字都没变,点击第10页也会自动变为 11 12 13 14 15 16 17 18 19 20,但是从这里开始又会变了。 比如点13页, 12 13 14 15 16 17 18 19 20 21 而不是 11 12 13 14 15 16 17 18 19 20 而且点到最后显示也有问题。比如总共32页。当我点到30时,却显示 31 32 33 34 35 36 37 38 39 40
abcxiaoye 2015-01-21
  • 打赏
  • 举报
回复
辛苦各位了!!
startPage=iCurrPage>4?iCurrPage+5>pages?pages>10?pages-9:1:iCurrPage+4:1;
endPage=iCurrPage>4?iCurrPage+5>pages?pages:iCurrPage+5:pages>10?10:pages;
哪位高手能解释一下这段代码的意思?谢谢!!
abcxiaoye 2015-01-20
  • 打赏
  • 举报
回复
高手在哪里啊??急!!
yschat2012 2015-01-20
  • 打赏
  • 举报
回复
把  // 显示页码计算  
            if (iCurrPage > 0) {  
                startPage = iCurrPage;  
                endPage = iCurrPage+9;  
            }  
            if (endPage > pages) {  
                if (pages>10)  
                    startPage = pages - 9;  
                else  
                    startPage = 1;  
                endPage = pages;  
            }  
            if (startPage < 1) {  
                startPage = 1;  
            }
替换成
startPage=iCurrPage>9?iCurrPage+1>pages?pages>10?pages-9:1:iCurrPage+1:1;
endPage=iCurrPage>9?iCurrPage+1>pages?pages:iCurrPage+10:pages>10?10:pages;
然后修改成你要的效果
abcxiaoye 2015-01-20
  • 打赏
  • 举报
回复
类似http://gzs.sgzjkj.com/zylist.aspx?classid=3&itemid=9
cfm2000 2015-01-20
  • 打赏
  • 举报
回复
for(int i=startPage; i<=endPage; i++){   这句修改掉就可以了 for(int i=iCurrPage/10*10; i<=iCurrPage/10*10+10; i++){   不过这个改法在整10页和最后几页效果不是很好,不过这样也就将就了。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧