简体   繁体   English

React table - 在手动分页中强制重置页面索引

[英]React table - force reset page index in manual pagination

I face issue on resetting page index manually我在手动重置页面索引时遇到问题

I handle change in data manually from onPageChange , onPageSizeChange ... etc我从onPageChangeonPageSizeChange ...等手动处理数据更改

when I change page index with onPageChange I stored in high-level component and pass again ReactTable as prop called currentPage in same cases I need to reset currentPage to be 0, but the dosent works so I added componentDidUpdate callback to force reset page index当我使用onPageChange更改页面索引时,我存储在高级组件中并再次将 ReactTable 作为称为 currentPage 的道具传递,在相同的情况下,我需要将 currentPage 重置为 0,但剂量有效,因此我添加了componentDidUpdate回调以强制重置页面索引

ReactTable version 6.11.5 ReactTable 版本 6.11.5

componentDidUpdate(prevProps, prevState) {
    const reactTable = this.myRef.current;
    const { currentPage } = this.props;
    if (reactTable && currentPage !== reactTable.state.page) {
      reactTable.state.page = currentPage;
      reactTable.getResolvedState().page = currentPage;
    }
  }

render() {
    const {
      data,
      loading,
      totalNumberOfPages,
      currentPage,
      onPageChange,
      pageSize,
      onPageSizeChange,
      onFilteredChange,
      onSortedChange,
      filtered,
    } = this.props;
 return (<ReactTable
            ref={this.myRef}
            filtered={filtered}
            onFilteredChange={(filtered) => {
              onFilteredChange(filtered);
              this.setState({ filtered });
            }}
            manual
            sorted={tableSorted}
            onSortedChange={(sorted) => onSortedChange(sorted)}
            filterable
            loading={loading && !silentUpdate}
            pages={totalNumberOfPages}
            page={currentPage}
            onPageChange={onPageChange}
            defaultPageSize={pageSize}
            pageSize={pageSize}
            onPageSizeChange={onPageSizeChange}
            loadingText="Loading..."
            data={data}
            columns={columns}
            className="-striped -highlight"
        />)

CodeSandbox代码沙盒

https://codesandbox.io/s/set-initial-page-number-in-react-table-jthci?file=/App.js https://codesandbox.io/s/set-initial-page-number-in-react-table-jthci?file=/App.js

if you are in page 3 and press "Reset Page", it should be in the first page如果您在第 3 页并按“重置页面”,它应该在第一页

I created a small codesandbox problem to show you that it is working.我创建了一个小代码沙盒问题来向您展示它正在工作。 Check this link检查此链接

https://codesandbox.io/s/set-initial-page-number-in-react-table-mumef https://codesandbox.io/s/set-initial-page-number-in-react-table-mumef

So, by reading the source code of react-table , I saw that the component used for pagination is called ReactTablePagination所以,通过阅读react-table的源码,我看到用于分页的组件叫做ReactTablePagination

This component has the following line该组件具有以下行

{showPageJump ? renderPageJump(this.getPageJumpProperties()) : renderCurrentPage(page)}{' '}

which means that in order for the page index to be updated, this property showPageJump should be set to false .这意味着为了更新页面索引,此属性showPageJump应设置为false

I tried without setting at all this property and it was not working.我尝试不设置所有这些属性,但它不起作用。 When i set it to false, it worked.当我将其设置为 false 时,它起作用了。

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

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