算法/伪code创建分页链接?分页、算法、链接、code

2023-09-10 23:26:43 作者:無言°

someome可以提供code或伪$ C $下如何产生的计算器分页链接?

Can someome provide code or pseudo-code for how the paging links on StackOverflow are generated?

我把货架我的大脑却想不出一个体面的方式来构建动态链接总是显示围绕当前的2页,再加上第一和最后一个。

I keep racking my brain but can't think of a decent way to build the dynamic links that always show the 2 pages around the current, plus the first and last.

例如: 1 ... 5 6 7 ... 593

推荐答案

有几个其他的答案了,但我想告诉你我把解决这个问题的办法: 首先,让我们看看堆栈溢出如何处理一般案件和边缘情况。我的每个页面显示10个结果,所以要找出它为1页,发现有不到11个条目的标签:可用性工作至今。我们可以看到的是什么也不显示,这是有道理的。

There are several other answers already, but I'd like to show you the approach I took to solve it: First, let's check out how Stack Overflow handles normal cases and edge cases. Each of my pages displays 10 results, so to find out what it does for 1 page, find a tag that has less than 11 entries: usability works today. We can see nothing is displayed, which makes sense.

怎么样2页?发现有11至20项( emacs的今天作品)的标签。我们看到: 1 2下一页或preV 1 2 ,这取决于我们是在哪一页

How about 2 pages? Find a tag that has between 11 and 20 entries (emacs works today). We see: "1 2 Next" or "Prev 1 2", depending on which page we're on.

3页? 1 2 3 ... 3下,preV 1 2 3下,和preV 1 ... 2 3 。有趣的是,我们可以看到,堆栈溢出本身不处理这种边缘情况非常好:它应该显示 1 2 ... 3下一页

3 pages? "1 2 3 ... 3 Next", "Prev 1 2 3 Next", and "Prev 1 ... 2 3". Interestingly, we can see that Stack Overflow itself doesn't handle this edge case very well: it should display "1 2 ... 3 Next"

4页? 1 2 3 ... 4下,preV 1 2 3 ... 4下,preV 1 ... 2 3 4下一步和preV 1 ... 3 4

4 pages? "1 2 3 ... 4 Next", "Prev 1 2 3 ... 4 Next", "Prev 1 ... 2 3 4 Next" and "Prev 1 ... 3 4"

最后,让我们来看看一般的情况下,N个页面: 1 2 3 ... N下一步,preV 1 2 3 ... n下,preV 1 ... 2 3 4,...,N下一步,preV 1 ... 3 4 5。 。N下一步,等等。

Finally let's look at the general case, N pages: "1 2 3 ... N Next", "Prev 1 2 3 ... N Next", "Prev 1 ... 2 3 4 ... N Next", "Prev 1 ... 3 4 5 ... N Next", etc.

让基于我们所看到的概括:   该算法似乎有这些共同特点:

Let's generalize based on what we've seen: The algorithm seems to have these traits in common:

如果我们不是第一页上,显示的链接,以$ P $光伏 始终显示第一页号 始终显示当前页码 始终显示在网页这个页面之前,并在页面此页面后。 始终显示在最后一页号 如果我们不是在最后一页上,显示的链接到下一个

让我们忽略了一个单页的边缘情况,并做好第一次尝试在算法:(如已经提到的,code实际打印出来的联系将更为复杂,试想一下,每一个我们把一个地方页码,preV或下一个函数调用,将返回正确的URL。)

Let's ignore the edge case of a single page and make a good first attempt at the algorithm: (As has been mentioned, the code to actually print out the links would be more complicated. Imagine each place we place a page number, Prev or Next as a function call that will return the correct URL.)

function printPageLinksFirstTry(num totalPages, num currentPage)
  if ( currentPage > 1 )
    print "Prev"
  print "1"
  print "..."
  print currentPage - 1
  print currentPage
  print currentPage + 1
  print "..."
  print totalPages
  if ( currentPage < totalPages )
    print "Next"
endFunction

此功能工作正常,但它并没有考虑到我们是否正在附近的第一个或最后一页。综观上述例子中,我们仅想显示的...如果当前页面是两个或更多个远

This function works ok, but it doesn't take into account whether we're near the first or last page. Looking at the above examples, we only want to display the ... if the current page is two or more away.

function printPageLinksHandleCloseToEnds(num totalPages, num currentPage)
  if ( currentPage > 1 )
    print "Prev"
  print "1"
  if ( currentPage > 2 )
    print "..."
  if ( currentPage > 2 )
    print currentPage - 1
  print currentPage
  if ( currentPage < totalPages - 1 )
    print currentPage + 1
  if ( currentPage < totalPages - 1 )
    print "..."
  print totalPages
  if ( currentPage < totalPages )
    print "Next"
endFunction

正如你所看到的,我们有一些重复这里。我们可以继续前进,清理一下的可读性:

As you can see, we have some duplication here. We can go ahead and clean that up for readibility:

function printPageLinksCleanedUp(num totalPages, num currentPage)
  if ( currentPage > 1 )
    print "Prev"
  print "1"
  if ( currentPage > 2 )
    print "..."
    print currentPage - 1
  print currentPage
  if ( currentPage < totalPages - 1 )
    print currentPage + 1
    print "..."
  print totalPages
  if ( currentPage < totalPages )
    print "Next"
endFunction

有只有两个遗留问题。首先,我们不打印出正确的一页,其次,我们会打印出1的两倍,如果我们的第一个或最后一页。让我们来清理这些均上涨一气呵成:

There are only two problems left. First, we don't print out correctly for one page, and secondly, we'll print out "1" twice if we're on the first or last page. Let's clean those both up in one go:

function printPageLinksFinal(num totalPages, num currentPage)
  if ( totalPages == 1 )
    return

  if ( currentPage > 1 )
    print "Prev"

  print "1"

  if ( currentPage > 2 )
    print "..."
    print currentPage - 1

  if ( currentPage != 1 and currentPage != totalPages )
    print currentPage

  if ( currentPage < totalPages - 1 )
    print currentPage + 1
    print "..."

  print totalPages

  if ( currentPage < totalPages )
    print "Next"

endFunction

其实,我撒谎了:我们有一个尚未解决的问题。当你至少有4页,并且是第一个或最后一页,你在你的显示器得到一个额外的页面。而不是 1 2 ...... 10下一页你 1 2 3 ... 10下一步。为了配合这是怎么回事,在堆栈溢出正好,你必须检查这种情况:

Actually, I lied: We have one remaining issue. When you have at least 4 pages and are on the first or last page, you get an extra page in your display. Instead of "1 2 ... 10 Next" you get "1 2 3 ... 10 Next". To match what's going on at Stack Overflow exactly, you'll have to check for this situation:

function printPageLinksFinalReally(num totalPages, num currentPage)
  if ( totalPages == 1 )
    return

  if ( currentPage > 1 )
    print "Prev"

  print "1"

  if ( currentPage > 2 )
    print "..."
    if ( currentPage == totalPages and totalPages > 3 )
      print currentPage - 2
    print currentPage - 1

  if ( currentPage != 1 and currentPage != totalPages )
    print currentPage

  if ( currentPage < totalPages - 1 )
    print currentPage + 1
    if ( currentPage == 1 and totalPages > 3 )
      print currentPage + 2
    print "..."

  print totalPages

  if ( currentPage < totalPages )
    print "Next"

endFunction

我希望这有助于!

I hope this helps!