猜测 javaye的分页设计

非常喜欢javaeye的分页 ,自己也写了一个 按照逻辑测试了一下

代码如下:

  int curPage = pageInfo.getPageNo();
        int pageCount = pageInfo.getPageCount();

        if (curPage > pageCount) {   //当前页大于最大页
            curPage = pageCount;
        }

        StringBuffer sb = new StringBuffer();
        sb.append("<div class=\"pagination\">");
        if (curPage > 1) {    // 添加 "上一页"
            // 加上链接 curpage-1
            sb.append("<a href=\"" + getHref(curPage - 1)
                    + "\" class=\"prev_page\" rel=\"prev start\">"
                    + PREVIOUS_PAGE + "</a>");
        } else {
            sb.append("<span class=\"disabled prev_page\">" + PREVIOUS_PAGE + "</span>");
        }

        if ((curPage < liststep) && (pageCount - curPage + 1 < liststep)) {  //显示全部
            for (int i = 1; i <= pageCount; i++) {
                if (i == curPage) {
                    sb.append("<span class=\"current\">" + i + "</span>");
                } else {
                    sb.append(goHref(i));
                }
            }
        } else if ((curPage < liststep) && (pageCount - curPage + 1 >= liststep)) { //显示 1 2 3 ... 45 46
            int left = curPage < 3 ? 3 : curPage + 1;
            for (int i = 1; i <= left; i++) {
                if (i == curPage) {
                    sb.append("<span class=\"current\">" + i + "</span>");
                } else {
                    sb.append(goHref(i));
                }
            }
            sb.append("<span class=\"gap\">&hellip;</span>");
            sb.append(goHref(pageCount - 1));
            sb.append(goHref(pageCount));
        } else if ((curPage >= liststep) && (pageCount - curPage + 1 >= liststep)) {  //显示 1 2 。。。5 6 7 。。45  46
            sb.append(goHref(1));
            sb.append(goHref(2));
            sb.append("<span class=\"gap\">&hellip;</span>");
            sb.append(goHref(curPage - 1));
            sb.append("<span class=\"current\">" + curPage + "</span>");
            sb.append(goHref(curPage + 1));
            sb.append("<span class=\"gap\">&hellip;</span>");
            sb.append(goHref(pageCount - 1));
            sb.append(goHref(pageCount));
        } else if ((curPage >= liststep) && (pageCount - curPage + 1 < liststep)) {  //显示  1 2 。。。 44 45 46
            sb.append(goHref(1));
            sb.append(goHref(2));
            sb.append("<span class=\"gap\">&hellip;</span>");

            int begin = pageCount - curPage + 1 < 3 ? pageCount - 2 : curPage - 1;
            for (int i = begin; i <= pageCount; i++) {
                if (i == curPage) {
                    sb.append("<span class=\"current\">" + i + "</span>");
                } else {
                    sb.append(goHref(i));
                }
            }
        }

        // 显示下-页
        if (curPage != pageCount) {
            // 加上链接 curpage+1
            sb.append("<a href=\"" + getHref(curPage + 1)
                    + "\" class=\"next_page\" rel=\"next\">" + NEXT_PAGE
                    + "</a>");
        } else {
            sb.append("<span class=\"disabled next_page\">" + NEXT_PAGE
                    + "</span>");
        }

        sb.append("</div>");


效果如图:










你可能感兴趣的:(分页,ITeye,page,JavaEye,pagecount)