【问题标题】:jQuery Pagination with Sets of Pages带有页面集的 jQuery 分页
【发布时间】:2015-09-01 19:59:22
【问题描述】:

我正在为我的 Web 应用程序实现分页,并且希望一次只显示 5 个页面(第 1-5 页、第 6-10 页等)

假设我返回的总页数是 100;使用 for 循环,我可以将我的 5 页集从 1-5 更新为 6-10,直到达到总页数?所以是这样的:

 numOfElemnets = 5 //pages to be displayed at once
 totalPages = 100;
 for(var i=0; i<totalPages; i++) {
     $("#toolbar .pagination li a").attr('href', 'page=" + i + "'); //div containing <a> elements
 }

或者如果我完全错误地理解了这个概念并且有更好的实现方式,请告诉我!

【问题讨论】:

    标签: javascript jquery pagination


    【解决方案1】:

    应该是这样的:

     numOfElemnets = 5 //pages to be displayed at once
     totalPages = 100; //total pages
     currentPage = 3; //this is shouldn't be static, since it's the current page the user is at
    
     /**
     * If the user is on page 1 to 5, the first page will be 1. If he's not, then the page will be the current page minus 5.
     * This is to stop it from starting the loop from a negative number, since you don't have a -4 page.
     */
     if(currentPage > numOfElemnets) {
        i = currentPage - numOfElemnets;
     } else {
        i = 1;
     }
    
     // The same thing as above for the end. You will take 5 pages after the current page, but if you're on page 99, you should pick just till 100 that is your limit.
     if(currentPage + numOfElemnets > totalPages) {
        end = totalPages;
     } else {
        end = currentPage + numOfElemnets;
     }
    
      // I changed the for to a while, since the i parameter had to get out of the for. You can still use a for and give the i as a parameter. Whatever.
     while(i <= end) {
        $("#toolbar .pagination li a").attr('href', 'page=" + i + "'); //div containing <a> elements
        i++;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      相关资源
      最近更新 更多