【问题标题】:How to add pagination in ReactTable with API call on click of next button如何在单击下一步按钮时通过 API 调用在 ReactTable 中添加分页
【发布时间】:2021-05-28 02:18:19
【问题描述】:

我想在 ReactTable 中添加带有上一个和下一个按钮的分页,默认情况下显示 10 行。我不确定数据库中是否有超过 10 行。

我想首先显示前 10 行,然后单击下一个按钮,应调用 API,该 API 将获取其他 10 行(如果存在)。

这是 ReactTable:

< ReactTable
columns = {
  [{
      Header: "Id",
      accessor: "id"
    },
    {
      Header: "Name",
      id: "name"
    }
  ]
}
manual
data = {
  data
}
pages = {
  pages
} < -- -- -- -- - I don 't know the total number of pages
loading = {
  loading
}
onFetchData = {
  () => this.fetchData()
}
/>

这里的问题是我不知道数据库中存在的总行数,因此我可以将其传递给页面。在不知道总页数的情况下如何做到这一点,如果有任何行,只需单击“下一步”按钮,就会显示出来。

【问题讨论】:

    标签: reactjs react-redux pagination material-ui react-table


    【解决方案1】:

    你需要从你的数据库中获取元素的总数

    const DEFAULTPAGESIZE= 30
    const total //from your bd
    pagination={{
              defaultPageSize: DEFAULTPAGESIZE,
              total,
              onChange: page => {
                doChange(
                  DEFAULTPAGESIZE,
                  page * DEFAULTPAGESIZE - DEFAULTPAGESIZE
                )
              },
            }}
    

    预付款应该是这样的:

    doChange(limit, skip)
    doChange(DEFAULTPAGESIZE, 1,2...n * DEFAULTPAGESIZE - DEFAULTPAGESIZE )
    

    数学是:

    1x30-30=0
    2x30-30=30
    3x30-30=60
    

    【讨论】:

    • 我没听懂你。我需要知道数据库中的总行数吗?
    • @shaluj 是的,你需要
    • 不知道总行数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2021-06-12
    • 1970-01-01
    • 2020-11-02
    相关资源
    最近更新 更多