简体   繁体   English

如何解决反应表中的分页重置问题?

[英]How to fix pagination reset issue in react-table?

I am working for a feature where i have to apply a filter outside the component which is using react-table, but the current page number doesn't get reset after the filter is being applied.我正在为一个功能工作,我必须在使用 react-table 的组件外应用过滤器,但在应用过滤器后当前页码不会重置。 The result being fetched (have applied server-side pagination) shows the first page's data.获取的结果(已应用服务器端分页)显示第一页的数据。

I have tried to use the callback onFetchData to change the current page number but it does'nt get triggered when the filter from outside the component is applied.我尝试使用回调 onFetchData 来更改当前页码,但是当应用来自组件外部的过滤器时它不会被触发。

render() {
    const { orders, onUpdate } = this.props;
    const defaultPageSize = 50;

    return (
      <div className="admin-report-table">
        <ReactTable
          data={orders.ordersData}
          columns={this.columns}
          pages={Math.ceil(orders.count / defaultPageSize)}
          defaultPageSize={defaultPageSize}
          multiSort={false}
          showPageSizeOptions={false}
          showPaginationTop
          className="-striped -highlight"
          noDataText="No orders found. Please check your connection or change your filters."
          filterable
          manual // informs React Table that you'll be handling sorting and pagination server-side
          onFetchData={(state) => {
            const { page, pageSize, filtered, sorted } = state;
            const adjustedPage = page + 1; // since library page starts from 0

            onUpdate({
              page: adjustedPage,
              filtered,
              pageSize,
              sorted,
            });
          }}
        />
      </div>
    );
  }

The Page number should be reset to 1 eg current display is Page 2 of 3, after the filter from outside the table is applied, the result is fetched and shown but the Page 2 of 3 doesn't change while the result in the table is of page 1.页码应重置为 1 例如当前显示为第 2 页,共 3 页,在应用表格外部的过滤器后,将获取并显示结果,但第 2 页共 3 页不会更改,而表中的结果为第 1 页。

I am working for a feature where i have to apply a filter outside the component which is using react-table, but the current page number doesn't get reset after the filter is being applied.我正在为一个功能工作,其中我必须在使用react-table的组件外部应用过滤器,但是在应用过滤器后,当前页码不会被重置。 The result being fetched (have applied server-side pagination) shows the first page's data.提取的结果(已应用服务器端分页)显示了第一页的数据。

I have tried to use the callback onFetchData to change the current page number but it does'nt get triggered when the filter from outside the component is applied.我尝试使用回调onFetchData更改当前页码,但是当从组件外部应用过滤器时,它不会被触发。

render() {
    const { orders, onUpdate } = this.props;
    const defaultPageSize = 50;

    return (
      <div className="admin-report-table">
        <ReactTable
          data={orders.ordersData}
          columns={this.columns}
          pages={Math.ceil(orders.count / defaultPageSize)}
          defaultPageSize={defaultPageSize}
          multiSort={false}
          showPageSizeOptions={false}
          showPaginationTop
          className="-striped -highlight"
          noDataText="No orders found. Please check your connection or change your filters."
          filterable
          manual // informs React Table that you'll be handling sorting and pagination server-side
          onFetchData={(state) => {
            const { page, pageSize, filtered, sorted } = state;
            const adjustedPage = page + 1; // since library page starts from 0

            onUpdate({
              page: adjustedPage,
              filtered,
              pageSize,
              sorted,
            });
          }}
        />
      </div>
    );
  }

The Page number should be reset to 1 eg current display is Page 2 of 3, after the filter from outside the table is applied, the result is fetched and shown but the Page 2 of 3 doesn't change while the result in the table is of page 1.页码应重置为1,例如当前显示为第2页,共3页,应用表格外部的过滤器后,将提取并显示结果,但在显示表中的结果时,第2页,共3页不变第1页。

I am working for a feature where i have to apply a filter outside the component which is using react-table, but the current page number doesn't get reset after the filter is being applied.我正在为一个功能工作,其中我必须在使用react-table的组件外部应用过滤器,但是在应用过滤器后,当前页码不会被重置。 The result being fetched (have applied server-side pagination) shows the first page's data.提取的结果(已应用服务器端分页)显示了第一页的数据。

I have tried to use the callback onFetchData to change the current page number but it does'nt get triggered when the filter from outside the component is applied.我尝试使用回调onFetchData更改当前页码,但是当从组件外部应用过滤器时,它不会被触发。

render() {
    const { orders, onUpdate } = this.props;
    const defaultPageSize = 50;

    return (
      <div className="admin-report-table">
        <ReactTable
          data={orders.ordersData}
          columns={this.columns}
          pages={Math.ceil(orders.count / defaultPageSize)}
          defaultPageSize={defaultPageSize}
          multiSort={false}
          showPageSizeOptions={false}
          showPaginationTop
          className="-striped -highlight"
          noDataText="No orders found. Please check your connection or change your filters."
          filterable
          manual // informs React Table that you'll be handling sorting and pagination server-side
          onFetchData={(state) => {
            const { page, pageSize, filtered, sorted } = state;
            const adjustedPage = page + 1; // since library page starts from 0

            onUpdate({
              page: adjustedPage,
              filtered,
              pageSize,
              sorted,
            });
          }}
        />
      </div>
    );
  }

The Page number should be reset to 1 eg current display is Page 2 of 3, after the filter from outside the table is applied, the result is fetched and shown but the Page 2 of 3 doesn't change while the result in the table is of page 1.页码应重置为1,例如当前显示为第2页,共3页,应用表格外部的过滤器后,将提取并显示结果,但在显示表中的结果时,第2页,共3页不变第1页。

I am working for a feature where i have to apply a filter outside the component which is using react-table, but the current page number doesn't get reset after the filter is being applied.我正在为一个功能工作,其中我必须在使用react-table的组件外部应用过滤器,但是在应用过滤器后,当前页码不会被重置。 The result being fetched (have applied server-side pagination) shows the first page's data.提取的结果(已应用服务器端分页)显示了第一页的数据。

I have tried to use the callback onFetchData to change the current page number but it does'nt get triggered when the filter from outside the component is applied.我尝试使用回调onFetchData更改当前页码,但是当从组件外部应用过滤器时,它不会被触发。

render() {
    const { orders, onUpdate } = this.props;
    const defaultPageSize = 50;

    return (
      <div className="admin-report-table">
        <ReactTable
          data={orders.ordersData}
          columns={this.columns}
          pages={Math.ceil(orders.count / defaultPageSize)}
          defaultPageSize={defaultPageSize}
          multiSort={false}
          showPageSizeOptions={false}
          showPaginationTop
          className="-striped -highlight"
          noDataText="No orders found. Please check your connection or change your filters."
          filterable
          manual // informs React Table that you'll be handling sorting and pagination server-side
          onFetchData={(state) => {
            const { page, pageSize, filtered, sorted } = state;
            const adjustedPage = page + 1; // since library page starts from 0

            onUpdate({
              page: adjustedPage,
              filtered,
              pageSize,
              sorted,
            });
          }}
        />
      </div>
    );
  }

The Page number should be reset to 1 eg current display is Page 2 of 3, after the filter from outside the table is applied, the result is fetched and shown but the Page 2 of 3 doesn't change while the result in the table is of page 1.页码应重置为1,例如当前显示为第2页,共3页,应用表格外部的过滤器后,将提取并显示结果,但在显示表中的结果时,第2页,共3页不变第1页。

I am working for a feature where i have to apply a filter outside the component which is using react-table, but the current page number doesn't get reset after the filter is being applied.我正在为一个功能工作,其中我必须在使用react-table的组件外部应用过滤器,但是在应用过滤器后,当前页码不会被重置。 The result being fetched (have applied server-side pagination) shows the first page's data.提取的结果(已应用服务器端分页)显示了第一页的数据。

I have tried to use the callback onFetchData to change the current page number but it does'nt get triggered when the filter from outside the component is applied.我尝试使用回调onFetchData更改当前页码,但是当从组件外部应用过滤器时,它不会被触发。

render() {
    const { orders, onUpdate } = this.props;
    const defaultPageSize = 50;

    return (
      <div className="admin-report-table">
        <ReactTable
          data={orders.ordersData}
          columns={this.columns}
          pages={Math.ceil(orders.count / defaultPageSize)}
          defaultPageSize={defaultPageSize}
          multiSort={false}
          showPageSizeOptions={false}
          showPaginationTop
          className="-striped -highlight"
          noDataText="No orders found. Please check your connection or change your filters."
          filterable
          manual // informs React Table that you'll be handling sorting and pagination server-side
          onFetchData={(state) => {
            const { page, pageSize, filtered, sorted } = state;
            const adjustedPage = page + 1; // since library page starts from 0

            onUpdate({
              page: adjustedPage,
              filtered,
              pageSize,
              sorted,
            });
          }}
        />
      </div>
    );
  }

The Page number should be reset to 1 eg current display is Page 2 of 3, after the filter from outside the table is applied, the result is fetched and shown but the Page 2 of 3 doesn't change while the result in the table is of page 1.页码应重置为1,例如当前显示为第2页,共3页,应用表格外部的过滤器后,将提取并显示结果,但在显示表中的结果时,第2页,共3页不变第1页。

I am using react table's out of the box pagination and the issue I had was how to extract the current pageIndex from react table.我正在使用反应表的开箱即用分页,我遇到的问题是如何从反应表中提取当前的 pageIndex。 Because every time there was an action on the row of React table, that did not alter the no.因为每次在 React 表的行上有一个动作,都不会改变 no。 of rows, the React table would go back to page 0.行,React 表将返回到第 0 页。

Following worked for me, hacky but works -以下对我有用,hacky但有效-

On parent component calling React table - signalTableData is where I have my data for columns and datarows.在调用 React 表的父组件上 - signalTableData是我拥有列和数据行数据的地方。 this could be your data.这可能是您的数据。

const [pageIndex, setPageIndex] = useState(0);

<ReactTable
     columns={signalTableData ? signalTableData.headerRow : <div>Loading...</div>}
                  data={signalTableData.dataRows ? signalTableData.dataRows : <div>Loading...</div>}
                  sortbyCollumn='originDate'
                  desc={true}
                  pageIndex = {pageIndex}
                  setPageIndex = {setPageIndex}
              />

One React Table js -一个 React Table js -

function Table({ columns, data, sortbyCollumn, desc, pageIndex, setPageIndex }) {...

and

useTable(
    {
      columns,
      data,
      defaultColumn, // Be sure to pass the defaultColumn option
      filterTypes,
      autoResetPage: false,
      initialState: {
        pageSize: 5,
        pageIndex: pageIndex,
        sortBy: [
          {
            id: sortbyCollumn,
            desc: desc
          }
        ] },
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM