Data Grid - Pagination
Through paging, a segment of data can be viewed from the assigned data source.
By default, the MIT DataGrid
displays the rows with pagination, and up to 100 rows per page.
On the other hand, the commercial XGrid
component displays, by default, all the rows with infinite scrolling (and virtualization) and without the 100 rows per page limitation. You need to set the pagination
prop to enable the pagination feature in such a case.
Basic example
<DataGrid pagination {...data} />
Page size
- The default page size is
100
, you can change this value with thepageSize
prop. - You can configure the possible page size the user can choose from with the
rowsPerPageOptions
prop.
<DataGrid pageSize={5} rowsPerPageOptions={[5, 10, 20]} pagination {...data} />
Controlled pagination
While the previous demos show how the pagination can be uncontrolled, the active page can be controlled with the page
/onPageChange
props.
<DataGrid
page={page}
onPageChange={(params) => {
setPage(params.page);
}}
pageSize={5}
pagination
{...data}
/>
Auto size
The autoPageSize
prop allows to auto-scale the pageSize
to match the container height and the max number of rows that can be displayed without a vertical scroll bar.
By default, this feature is off.
<DataGrid autoPageSize pagination {...data} />
Custom pagination component
Head to the rendering section of the documentation for customizing the pagination component.
Server-side pagination
By default, pagination works on the client-side.
To switch it to server-side, set paginationMode="server"
.
You also need to set the rowCount
prop to so the grid know the total number of pages.
Finally, you need to handle the onPageChange
callback to load the rows for the corresponding page.
<DataGrid
rows={rows}
columns={data.columns}
pagination
pageSize={5}
rowCount={100}
paginationMode="server"
onPageChange={handlePageChange}
loading={loading}
/>
Rendering
You can customize the rendered of the pagination following this section of the documentation.
Paginate > 100 rows
The DataGrid
component can display up to 100 rows per page.
The XGrid
component removes this limitation.
The following demo displays 200 rows per page:
<XGrid pagination pageSize={200} {...data} />
apiRef
The grid exposes a set of methods that enables all of these features using the imperative apiRef.
⚠️ Only use this API when you have no alternative. Always start from the declarative API that the grid exposes.
setPageSize
: Set the number of rows in one page.setPage
: Set the displayed page.onPageChange
: Callback fired after a new page has been displayed.onPageSizeChange
: Callback fired after the page size was changed.
Below is an example of how you can reset the page using the imperative setPage
method.
<XGrid pagination pageSize={5} apiRef={apiRef} {...data} />