-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DataGrid] Add
resetPageOnSortFilter
prop that resets the page afte…
…r sorting/filtering (@arminmeh) (#16580) Signed-off-by: Armin Mehinovic <[email protected]> Co-authored-by: Armin Mehinovic <[email protected]> Co-authored-by: Bilal Shafi <[email protected]> Co-authored-by: Sam Sycamore <[email protected]> Co-authored-by: Armin Mehinovic <[email protected]>
- Loading branch information
1 parent
7564ded
commit 73ded3e
Showing
26 changed files
with
497 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as React from 'react'; | ||
import { DataGrid, GridToolbar } from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin']; | ||
|
||
export default function FilteringWithPageReset() { | ||
const { data, loading } = useDemoData({ | ||
dataSet: 'Employee', | ||
visibleFields: VISIBLE_FIELDS, | ||
rowLength: 100, | ||
}); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
{...data} | ||
loading={loading} | ||
pagination | ||
initialState={{ | ||
...data.initialState, | ||
pagination: { paginationModel: { page: 0, pageSize: 10 } }, | ||
}} | ||
pageSizeOptions={[10]} | ||
resetPageOnSortFilter | ||
slots={{ toolbar: GridToolbar }} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as React from 'react'; | ||
import { DataGrid, GridToolbar } from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin']; | ||
|
||
export default function FilteringWithPageReset() { | ||
const { data, loading } = useDemoData({ | ||
dataSet: 'Employee', | ||
visibleFields: VISIBLE_FIELDS, | ||
rowLength: 100, | ||
}); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
{...data} | ||
loading={loading} | ||
pagination | ||
initialState={{ | ||
...data.initialState, | ||
pagination: { paginationModel: { page: 0, pageSize: 10 } }, | ||
}} | ||
pageSizeOptions={[10]} | ||
resetPageOnSortFilter | ||
slots={{ toolbar: GridToolbar }} | ||
/> | ||
</div> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
docs/data/data-grid/filtering/FilteringWithPageReset.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<DataGrid | ||
{...data} | ||
loading={loading} | ||
pagination | ||
initialState={{ | ||
...data.initialState, | ||
pagination: { paginationModel: { page: 0, pageSize: 10 } }, | ||
}} | ||
pageSizeOptions={[10]} | ||
resetPageOnSortFilter | ||
slots={{ toolbar: GridToolbar }} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
docs/data/data-grid/pagination/ServerPaginationFilterSortGrid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as React from 'react'; | ||
import { DataGrid } from '@mui/x-data-grid'; | ||
import { createFakeServer } from '@mui/x-data-grid-generator'; | ||
|
||
const SERVER_OPTIONS = { | ||
useCursorPagination: false, | ||
}; | ||
|
||
const { useQuery, ...data } = createFakeServer({}, SERVER_OPTIONS); | ||
|
||
export default function ServerPaginationFilterSortGrid() { | ||
const [paginationModel, setPaginationModel] = React.useState({ | ||
page: 0, | ||
pageSize: 5, | ||
}); | ||
const [sortModel, setSortModel] = React.useState([]); | ||
const [filterModel, setFilterModel] = React.useState({ | ||
items: [], | ||
}); | ||
const queryOptions = React.useMemo( | ||
() => ({ ...paginationModel, sortModel, filterModel }), | ||
[paginationModel, sortModel, filterModel], | ||
); | ||
const { isLoading, rows, pageInfo } = useQuery(queryOptions); | ||
|
||
// Some API clients return undefined while loading | ||
// Following lines are here to prevent `rowCount` from being undefined during the loading | ||
const rowCountRef = React.useRef(pageInfo?.totalRowCount || 0); | ||
|
||
const rowCount = React.useMemo(() => { | ||
if (pageInfo?.totalRowCount !== undefined) { | ||
rowCountRef.current = pageInfo.totalRowCount; | ||
} | ||
return rowCountRef.current; | ||
}, [pageInfo?.totalRowCount]); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
rows={rows} | ||
{...data} | ||
rowCount={rowCount} | ||
loading={isLoading} | ||
pageSizeOptions={[5]} | ||
paginationModel={paginationModel} | ||
sortModel={sortModel} | ||
filterModel={filterModel} | ||
paginationMode="server" | ||
sortingMode="server" | ||
filterMode="server" | ||
onPaginationModelChange={setPaginationModel} | ||
onSortModelChange={setSortModel} | ||
onFilterModelChange={setFilterModel} | ||
/> | ||
</div> | ||
); | ||
} |
57 changes: 57 additions & 0 deletions
57
docs/data/data-grid/pagination/ServerPaginationFilterSortGrid.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as React from 'react'; | ||
import { DataGrid, GridSortModel, GridFilterModel } from '@mui/x-data-grid'; | ||
import { createFakeServer } from '@mui/x-data-grid-generator'; | ||
|
||
const SERVER_OPTIONS = { | ||
useCursorPagination: false, | ||
}; | ||
|
||
const { useQuery, ...data } = createFakeServer({}, SERVER_OPTIONS); | ||
|
||
export default function ServerPaginationFilterSortGrid() { | ||
const [paginationModel, setPaginationModel] = React.useState({ | ||
page: 0, | ||
pageSize: 5, | ||
}); | ||
const [sortModel, setSortModel] = React.useState<GridSortModel>([]); | ||
const [filterModel, setFilterModel] = React.useState<GridFilterModel>({ | ||
items: [], | ||
}); | ||
const queryOptions = React.useMemo( | ||
() => ({ ...paginationModel, sortModel, filterModel }), | ||
[paginationModel, sortModel, filterModel], | ||
); | ||
const { isLoading, rows, pageInfo } = useQuery(queryOptions); | ||
|
||
// Some API clients return undefined while loading | ||
// Following lines are here to prevent `rowCount` from being undefined during the loading | ||
const rowCountRef = React.useRef(pageInfo?.totalRowCount || 0); | ||
|
||
const rowCount = React.useMemo(() => { | ||
if (pageInfo?.totalRowCount !== undefined) { | ||
rowCountRef.current = pageInfo.totalRowCount; | ||
} | ||
return rowCountRef.current; | ||
}, [pageInfo?.totalRowCount]); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
rows={rows} | ||
{...data} | ||
rowCount={rowCount} | ||
loading={isLoading} | ||
pageSizeOptions={[5]} | ||
paginationModel={paginationModel} | ||
sortModel={sortModel} | ||
filterModel={filterModel} | ||
paginationMode="server" | ||
sortingMode="server" | ||
filterMode="server" | ||
onPaginationModelChange={setPaginationModel} | ||
onSortModelChange={setSortModel} | ||
onFilterModelChange={setFilterModel} | ||
/> | ||
</div> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
docs/data/data-grid/pagination/ServerPaginationFilterSortGrid.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<DataGrid | ||
rows={rows} | ||
{...data} | ||
rowCount={rowCount} | ||
loading={isLoading} | ||
pageSizeOptions={[5]} | ||
paginationModel={paginationModel} | ||
sortModel={sortModel} | ||
filterModel={filterModel} | ||
paginationMode="server" | ||
sortingMode="server" | ||
filterMode="server" | ||
onPaginationModelChange={setPaginationModel} | ||
onSortModelChange={setSortModel} | ||
onFilterModelChange={setFilterModel} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as React from 'react'; | ||
import { DataGrid } from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin']; | ||
|
||
export default function SortingWithPageReset() { | ||
const { data, loading } = useDemoData({ | ||
dataSet: 'Employee', | ||
visibleFields: VISIBLE_FIELDS, | ||
rowLength: 100, | ||
}); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
{...data} | ||
loading={loading} | ||
pagination | ||
pageSizeOptions={[10]} | ||
initialState={{ pagination: { paginationModel: { page: 0, pageSize: 10 } } }} | ||
resetPageOnSortFilter | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as React from 'react'; | ||
import { DataGrid } from '@mui/x-data-grid'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
|
||
const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin']; | ||
|
||
export default function SortingWithPageReset() { | ||
const { data, loading } = useDemoData({ | ||
dataSet: 'Employee', | ||
visibleFields: VISIBLE_FIELDS, | ||
rowLength: 100, | ||
}); | ||
|
||
return ( | ||
<div style={{ height: 400, width: '100%' }}> | ||
<DataGrid | ||
{...data} | ||
loading={loading} | ||
pagination | ||
pageSizeOptions={[10]} | ||
initialState={{ pagination: { paginationModel: { page: 0, pageSize: 10 } } }} | ||
resetPageOnSortFilter | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<DataGrid | ||
{...data} | ||
loading={loading} | ||
pagination | ||
pageSizeOptions={[10]} | ||
initialState={{ pagination: { paginationModel: { page: 0, pageSize: 10 } } }} | ||
resetPageOnSortFilter | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.