material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Page Size is resetted when using remote data

Open jon-zu opened this issue 6 years ago • 4 comments

Describe the bug When changing the page size while using remote data and the size of the Grid which contains the table changes It will do a re-render and the page size will be resetted to the default value.

To Reproduce

  1. Go to the sandbox
  2. Set a specific page size(like 10)
  3. Click on a row
  4. Page size will be resetted to 5

Expected behavior Page size stays 10.

Sandbox https://codesandbox.io/s/cocky-brown-fu13t?file=/src/App.tsx

Desktop (please complete the following information):

  • OS: Fedora + Windows
  • Browser chrome + Firefox
  • Version latest

jon-zu avatar Apr 13 '20 05:04 jon-zu

Hi Jonas,

This has happened to me as well. You might try to save pagesize in state and then assign it to pagesize prop of material table. Like so: components={{ Pagination: props => <STablePagination {...props} onChangeRowsPerPage={(event: any) => { if (event.target.value) { let pageSize = event.target.value; if (event.target.value === -1) { pageSize = this.tableRef.current.dataManager.data.length; } this.setState({ pageSize }) } }} dataCount={this.tableRef.current ? this.tableRef.current.dataManager.data.length : 0} /> }} />

hikmert avatar Apr 20 '20 11:04 hikmert

Hi @mbrn,

I'm having trouble updating page size when fetching remote data. Is there something specific I should be implementing for this to work? In my case I don't have to do anything for the page size to revert to default.

Currently I'm using a simple table with a few columns and no options, and I'm passing a function that uses the query object to save some filters and then fetch the data.

When I go to change the page size and I select a different size than default, it instantly reverts to the default value and it doesn't show a different number of rows in the process.

The table:

<Table
        tableRef={tableRef}
        data={searchCallback}
        columns={tableOptions.columns}
/>

The method (simplified version for this post):

const searchCallback = useCallback(
    async (query) => {
      try {

        setIsLoading(true);
        const results = await fetchSomeStuff();
        setIsLoading(false);

        return {
          data: results.items,
          page: query.page,
          totalCount: results.total
        };
      } catch (reason) {
        doStuff();
        return emptyResults;
      }
    },
    [filters, searchMethod, searchOnMount]
  );

And I use the tableRef like this:

useEffect(() => {
    if (filters && tableRef.current) {
      tableRef.current.onQueryChange({ page: 0 });
    }
  }, [filters]);

eloyrubinos avatar May 27 '20 13:05 eloyrubinos

Hi, @JonasZ95 Here is your code sandbox with a working around that I used following @hikmert tip.

Just use:

const [pageSize, setPageSize] = useState(10)
<MaterialTable
      ...
      onChangeRowsPerPage={(items) => setPageSize(items)}
      options={{pageSize: pageSize}}/>

diegosps avatar Jun 04 '20 20:06 diegosps

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Sep 02 '20 21:09 stale[bot]