mui-datatables
mui-datatables copied to clipboard
How can I enable pagination for an unknown number of rows?
I'm trying to implement server-side pagination but the server doesn't return the total number of items.
In the MaterialUI TablePagination component docs it says that count prop should be -1 to enable pagination, I tried to set options.count to -1 as it's passed down to the MuiTablePagination component but the pagination buttons are disabled.
Is there a way I can enable pagination for an unknown number of items?
Your Environment
| Tech | Version |
|---|---|
| Material-UI | 4.7.1 |
| MUI-datatables | 2.12.4 |
| React | 16.12.0 |
-1 for count should work fine. You can see an example of serverside pagination with the serverside-pagination example. Provided you have enough data, you should have enabled pagination buttons. I cannot tell why this wouldn't be the case for you, as I do not have enough information about your code. If you create a minimal codesandbox example to demonstrate the issue, I will take a look. It is also helpful if you can build from a working example like serverside-pagination.
Sure, sorry for not providing an example from the start, but I thought it falls under the feature request/question category because I had not found information about it in the docs.
I upgraded my environment as follows:
| Tech | Version |
|---|---|
| Material-UI | 4.7.1 -> 4.9.3 |
| MUI-datatables | 2.12.4 -> 2.14.0 |
This solved my issue with the buttons being disabled, but now pagination only works from page 0 to 1. I can't change the page to either the third or first page from the second page.
Here is the codesandbox example.
In your example, you're not using the count from your state. Therefore, the hardcoded value is never changed when you request more data.
Also bear in mind that this library uses a lower version of material ui internally, so the count prop may operate differently than it does in v4. If you need to use the v4 material-ui pagination and can't wait until this library is updated, then you can use the custom footer option to build a new pagination (examples/customize-footer).
I don't use it deliberately to show that using -1 as the count option breaks the pagination (because my real server doesn't return the total number of items), but I added a total count in my example to show that when -1 is replaced with the current count from the state, pagination works fine.
Yeah, I know that the internal Material-UI version is lower than v4, but I'm not familiar with how MUI-Datatables use the count option, so I wasn't sure if it's an issue with MUI-Datatables, Material-UI or the interrogation of both of them.
I don't think v3 accepts -1 as a valid value for count. You have need to build your own pagination, but hopefully the example will help you.
Okay, I will build a customer footer with v4, thanks for the help!
I won't have access to a count from the api I am using, does anyone know if it's possible to still use the pagination button without this? Can you just have a next and previous button?