【问题标题】:Default page number on page size change页面大小更改的默认页码
【发布时间】:2020-05-15 14:35:40
【问题描述】:

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

我说的是场景,我在第 10 页(共 100 页,有 10 行)上,然后我将更改为 25,它将更新到第 4 页,然后更改为 50,它将更新到第 2 页。

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

我以为

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

会起作用,但它只能从每页 100 个项目开始。

任何提示应该如何正确显示?

【问题讨论】:

    标签: algorithm pagination


    【解决方案1】:

    tldr

    nextPage = floor(oldPage * oldLimit / nextLimit)
    

    或更好

    nextPage = floor(offset / nextLimit)
    

    一种“标准”的思维方式是offset、limit

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

    在你的词汇中,

    • pagination 是limit
    • 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
    • 如您所见,显示的第一个元素可能低于我们想要的元素。
    • !那么我们(错误地)假设显示的第一个元素是我们希望在新限制之后显示的元素。

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

    【讨论】:

    • 我让它与“默认”页面一起工作。非常感谢有据可查的解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多