Page Size is resetted when using remote data
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
- Go to the sandbox
- Set a specific page size(like 10)
- Click on a row
- 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
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}
/>
}} />
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]);
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}}/>
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.