页面大小更改时的默认页码

时间:2020-05-14 23:35:42

标签: algorithm pagination

当用户更改页面大小(每页项)时,我正在尝试设置默认页码的值

我正在谈论secenarion,我在第10页的第10页上有10行,然后将其更改为25,它将更新为第4页,然后将更改为50,将更新为第2页。

另一种方法是,当我在第100页的第2页上,然后切换到50行时,我将在第3页上结束,然后更改为每页25个项目-第5页和第10页第11页

我以为

defaultPageNo = round(((currentPageNo * prevRowSize) / currentRowSize )) + 1;

可以使用,但每页向下只能从100个项目开始。

任何提示如何使其看起来正确?

1 个答案:

答案 0 :(得分:1)

tldr

nextPage = floor(oldPage * oldLimit / nextLimit)

或更好

nextPage = floor(offset / nextLimit)

一种“标准”的思维方式是根据offsetlimit

  • offset是您跳过的元素数(更确切地说是您所在元素的索引)
  • limit是您显示的元素数量

在您的词汇表中,

  • paginationlimit
  • currentPage = floor(offset / limit)

当您更改分页(也称为limit)时,您不想更改offset,只有页面,并且(分区中)确实不知道限制是否增加。

现在,因为偏移量是不变的,我们肯定想知道它。

  • 如果您知道它,请使用它。

nextPage = floor(offset / nextLimit)

  • 否则,我们可能会近似:您看到在申请限价时我们丢失了信息。我假设我们希望当前显示的第一个元素显示在newPage上
if currentPage == k, then offset == k * limit + r (k,r integers, r < limit)

由于我们要显示第一个,所以我们将说offset = k * limit + 0 = currentPage * limit

最后

nextPage = floor(offset / nextLimit) = floor(oldPage * oldLimit / nextLimit)

例如

const changePagination = (old, newLimit) => {
  const next = { page: Math.floor(old.page * old.limit / newLimit), limit: newLimit }
  console.log(next, 'next display at offset:', next.page * next.limit)
  return next
}
let old = { page: 238, limit: 1 } // make some coll
old = changePagination(old, 1)
old = changePagination(old, 2)
old = changePagination(old, 4) // notice we show a few "new" elements
old = changePagination(old, 8)
old = changePagination(old, 5)
old = changePagination(old, 11)
console.log('ill stuff')
old = changePagination({ page: 60, limit: 1 }, 21)
old = changePagination(old, 20)
old = changePagination(old, 21)
old = changePagination(old, 20)
old = changePagination(old, 21) // first offset is 0 ?!! while initial is 60

  • 如您所见,显示的第一个元素可能低于我们想要的元素。
  • !然后我们(错误地)假定显示的 first 元素是我们希望在新限制之后显示的元素。

因此,如果您有偏移量,请保持,而不要从旧分页中猜测