欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

可以移动的分页条 博客分类: others JSPCC++C# 

程序员文章站 2024-03-16 08:21:58
...
 /**
  *
  * @param recordsCount int:总记录数
  * @param pageSize int:每页要显示的记录数
  * @param currentPage int:当前的页数
  * @param url String:链接的路径
  * @return String:导航条

 url还可能追加参数
示例调用:

--------------------------------------------------
String str = turnPage.printTurnPageBar(100, 35, 1,
                                        "searchshow.jsp?t=1&s1=N&s2=1&c=1");
 System.out.println("str=" + str);
--------------------------------------------------
*/

 public static String printTurnPageBar(int recordsCount, int pageSize,
                                       int currentPage, String url) {
     String barStr = "";
     int index = url.indexOf("?");
     if (recordsCount <= pageSize) {
         if (recordsCount == 0) {
             barStr = "没有相关记录";
         } else {
             barStr = "[1]";
         }
         return barStr;
     }
     int pageCount = recordsCount % pageSize == 0 ?
                     recordsCount / pageSize :
                     recordsCount / pageSize + 1;
     int begin = 0, end = 0;
     boolean b1 = false;
     /*
         当总页数小于10页时
      */
     if (pageCount <= 10) {
         begin = 1;
         end = pageCount;
     } else {
         if (currentPage == 1) {
             begin = 1;
             end = 10;
         } else {
             if (index == -1) {
                 barStr = "<a href=\"" + url + "?page=" +
                          (currentPage - 1) + "\">上一页</a> ";
             } else {
                 barStr = "<a href=\"" + url + "&page=" +
                          (currentPage - 1) + "\">上一页</a> ";
             }

             if (currentPage - 5 > 1) {
                 if (currentPage + 4 <= pageCount) {
                     begin = currentPage - 5;
                     end = currentPage + 4;
                     b1 = true;
                 } else {
                     end = pageCount;
                     begin = pageCount - 9;
                 }
             } else {
                 begin = 1;
                 end = 10;
             }
         }
     }
     for (int i = begin; i <= end; i++) {
         if (index == -1) {
             if (i == currentPage) {
                 barStr += "<a href=\"" + url + "?page=" + i +
                         "\">" + i + "</a> ";
             } else {
                 barStr += "<a href=\"" + url + "?page=" + i +
                         "\">[" + i + "]</a> ";
             }
         } else {
             if (i == currentPage) {
                 barStr += "<a href=\"" + url + "&page=" + i +
                         "\">" + i + "</a> ";
             } else {
                 barStr += "<a href=\"" + url + "&page=" + i +
                         "\">[" + i + "]</a> ";
             }
         }
     }
     if (b1) {
         if (index == -1) {
             barStr += "<a href=\"" + url + "?page=" +
                     (currentPage + 1) +
                     "\">下一页</a>";
         } else {
             barStr += "<a href=\"" + url + "&page=" +
                     (currentPage + 1) +
                     "\">下一页</a>";
         }
     }
     return barStr;
 }
相关标签: JSP C C++ C#