2011-03-04 74 views
1

我想显示一个有限的编号。的页面链接,说10 5个链接,并想知道是否有任何已知或尝试和测试方法来实现这一点。显示有限编号。页面链接分页

因此可以说,用户现在可以看到以下链接

以前,1(选择),2,3,4,5 ...下一个

用户点击,说4,现在他看到

以前... 3,4(选择的),5,6,7 ...下

现在他点击7

以前... 6,7(选择的),8, 9,10 ...下一个

现在我认为这是分页编程中非常普遍的事情。那么是否有任何已知的算法来做到这一点。我感觉自己懒得做饭!

编辑: - 这需要在服务器端实现。我正在研究C#,但是你可以用任何语言来进行算法研究。

+0

任何特定的编程语言? – DOK 2011-03-04 20:39:56

回答

1

退房jQuery和分页插件...

http://plugins.jquery.com/project/pagination

+0

jQuery插件是我寻找的第一件事..我甚至看到了这一个..但我是寻找服务器端的方法。不过现在看来这是最好的选择! – Jags 2011-03-05 07:03:51

+0

谢谢,如果有帮助,记住标记为接受?我将继续寻找C# – Orbit 2011-03-05 07:06:46

+0

的服务器端分页器以及我正在等待有人回答服务器端代码!..我会接受你的答案,如果没有答复在一天左右或更多:) ..谢谢你的帮助 – Jags 2011-03-05 08:24:56

0

你不能用HTML来做到这一点。您必须使用PHP或JavaScript(或其他一些脚本语言)来生成链接。

+0

是的..你是正确的..但是我试图找出那个..php,c#,java任何东西的逻辑 – Jags 2011-03-05 07:01:08

2

测试这一点,即使它可能是一个有点更加优美它的工作原理。我在这里使用C#,但任何语言的逻辑都应该是相同的。如果你想出一个更好的解决方案,请发布。如果您有任何问题,请发送至。我已经评论了代码,以帮助更好地解释发生了什么。

 //Get the current page we are on 
     int start = currentPage; 
     int end = currentPage; 

     //If the page cannot be devised by 5 enter the loop 
     if ((start % 5 != 0) && (end % 5 != 0)) 
     { 
      //Get the next nearest page that is divisible by 5 
      while ((end % 5) != 0) 
      { 
       end++; 
      } 

      //Get the previous nearest page that is divisible by 5 
      while ((start % 5) != 0) 
      { 
       start--; 
      } 
     } 
     //The page is divisible by 5 so get the next 5 pages in the pagination 
     else 
     { 
      end += 5; 
     } 
     //We are on the first page 
     if (start == 0) 
     { 
      start++; 
      end++; 
     } 
     //We are on the last page 
     if (end == lastpage) 
     { 
      end = lastpage; 
     } 

     //Post your pagination links below 
     for (int i = start; i < end; i++) 
     { 
      //Your code here 
     } 
3

有一些问题的答案,尤其是泰隆的问题是,它仅更新导航时,余数为0,如果你想它来更新每点击那么下面要好得多:

var start, 
end, 
pagesCutOff = 5, 
ceiling = Math.ceil(pagesCutOff/2), 
floor = Math.floor(pagesCutOff/2); 

if(numPages < pagesCutOff) { 
    start = 0; 
    end = numPages; 
} else if(currentPage >= 1 && currentPage <= ceiling) { 
    start = 0; 
    end = pagesCutOff; 
} else if((currentPage + floor) >= numPages) { 
    start = (numPages - pagesCutOff); 
    end = numPages; 
} else { 
    start = (currentPage - ceiling); 
    end = (currentPage + floor); 
} 

显然你通过当前页面和numPages自己发送到函数,这将使当前页面保持在分页列表的中心,显示的按钮数应该是奇数,以便所选页面可以“在中间“的名单。

然后,您可以执行以下循环:

for (var i = start; i < end; i++) { 
    //Your code here 
} 

如果你想下一个和前一个按钮添加到这个再简单地做一些事情,如:

 if(currentPage !== 1) { 
      $('<a href="javascript:void(0);" class="paginate-link" rel="' + (parseInt(currentPage) - 1) + '">&lt; Previous</a>').appendTo(navElement); 
     } 

凡navElement是一个jQuery对象$('#pagination-nav');你需要将列表添加到列表中。

希望这可以帮助别人!

干杯

1

有一些问题的答案,尤其是泰隆的问题是,它仅更新导航时,余数为0,如果你想它来更新每点击那么下面要好得多:

//Get the current page we are on 
    int start = currentPage; 
    int end = currentPage; 

    //If the page cannot be devised by 5 enter the loop 
    if ((start % 5 != 0) && (end % 5 != 0)) 
    { 
     //Get the next nearest page that is divisible by 5 
     while ((end % 5) != 0) 
     { 
      end++; 
     } 

     //Get the previous nearest page that is divisible by 5 
     while ((start % 5) != 0) 
     { 
      start--; 
     } 
    } 
    //The page is divisible by 5 so get the next 5 pages in the pagination 
    else 
    { 
     end += 5; 
    } 
    //We are on the first page 
    if (start == 0) 
    { 
     start++; 
     end++; 
    } 
    //We are on the last page 
    if (end == lastpage) 
    { 
     end = lastpage; 
    } 

    //Post your pagination links below 
    for (int i = start; i < end; i++) 
    { 
     //Your code here 
    }