-
-
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
getFilterState
method (#13418)
- Loading branch information
1 parent
722aa31
commit 70c6612
Showing
8 changed files
with
231 additions
and
11 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,88 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { DataGridPro, useGridApiRef } from '@mui/x-data-grid-pro'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
import Button from '@mui/material/Button'; | ||
import Stack from '@mui/material/Stack'; | ||
|
||
const predefinedFilters = [ | ||
{ | ||
label: 'All', | ||
filterModel: { items: [] }, | ||
}, | ||
{ | ||
label: 'Filled', | ||
filterModel: { items: [{ field: 'status', operator: 'is', value: 'Filled' }] }, | ||
}, | ||
{ | ||
label: 'Open', | ||
filterModel: { items: [{ field: 'status', operator: 'is', value: 'Open' }] }, | ||
}, | ||
{ | ||
label: 'Rejected', | ||
filterModel: { items: [{ field: 'status', operator: 'is', value: 'Rejected' }] }, | ||
}, | ||
{ | ||
label: 'Partially Filled', | ||
filterModel: { | ||
items: [{ field: 'status', operator: 'is', value: 'PartiallyFilled' }], | ||
}, | ||
}, | ||
]; | ||
|
||
export default function FilteredRowCount() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 1000, | ||
maxColumns: 10, | ||
}); | ||
|
||
const apiRef = useGridApiRef(); | ||
|
||
const [predefinedFiltersRowCount, setPredefinedFiltersRowCount] = React.useState( | ||
[], | ||
); | ||
|
||
const getFilteredRowsCount = React.useCallback( | ||
(filterModel) => { | ||
const { filteredRowsLookup } = apiRef.current.getFilterState(filterModel); | ||
return Object.keys(filteredRowsLookup).filter( | ||
(rowId) => filteredRowsLookup[rowId] === true, | ||
).length; | ||
}, | ||
[apiRef], | ||
); | ||
|
||
React.useEffect(() => { | ||
// Calculate the row count for predefined filters | ||
if (data.rows.length === 0) { | ||
return; | ||
} | ||
|
||
setPredefinedFiltersRowCount( | ||
predefinedFilters.map(({ filterModel }) => getFilteredRowsCount(filterModel)), | ||
); | ||
}, [apiRef, data.rows, getFilteredRowsCount]); | ||
|
||
return ( | ||
<div style={{ overflow: 'hidden' }}> | ||
<Stack direction="row" gap={1} mb={1} flexWrap="wrap"> | ||
{predefinedFilters.map(({ label, filterModel }, index) => { | ||
const count = predefinedFiltersRowCount[index]; | ||
return ( | ||
<Button | ||
key={label} | ||
onClick={() => apiRef.current.setFilterModel(filterModel)} | ||
variant="outlined" | ||
> | ||
{label} {count !== undefined ? `(${count})` : ''} | ||
</Button> | ||
); | ||
})} | ||
</Stack> | ||
<Box sx={{ height: 520, width: '100%' }}> | ||
<DataGridPro {...data} loading={data.rows.length === 0} apiRef={apiRef} /> | ||
</Box> | ||
</div> | ||
); | ||
} |
88 changes: 88 additions & 0 deletions
88
docs/data/data-grid/filtering-recipes/FilteredRowCount.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,88 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { DataGridPro, useGridApiRef, GridFilterModel } from '@mui/x-data-grid-pro'; | ||
import { useDemoData } from '@mui/x-data-grid-generator'; | ||
import Button from '@mui/material/Button'; | ||
import Stack from '@mui/material/Stack'; | ||
|
||
const predefinedFilters: { label: string; filterModel: GridFilterModel }[] = [ | ||
{ | ||
label: 'All', | ||
filterModel: { items: [] }, | ||
}, | ||
{ | ||
label: 'Filled', | ||
filterModel: { items: [{ field: 'status', operator: 'is', value: 'Filled' }] }, | ||
}, | ||
{ | ||
label: 'Open', | ||
filterModel: { items: [{ field: 'status', operator: 'is', value: 'Open' }] }, | ||
}, | ||
{ | ||
label: 'Rejected', | ||
filterModel: { items: [{ field: 'status', operator: 'is', value: 'Rejected' }] }, | ||
}, | ||
{ | ||
label: 'Partially Filled', | ||
filterModel: { | ||
items: [{ field: 'status', operator: 'is', value: 'PartiallyFilled' }], | ||
}, | ||
}, | ||
]; | ||
|
||
export default function FilteredRowCount() { | ||
const { data } = useDemoData({ | ||
dataSet: 'Commodity', | ||
rowLength: 1000, | ||
maxColumns: 10, | ||
}); | ||
|
||
const apiRef = useGridApiRef(); | ||
|
||
const [predefinedFiltersRowCount, setPredefinedFiltersRowCount] = React.useState< | ||
number[] | ||
>([]); | ||
|
||
const getFilteredRowsCount = React.useCallback( | ||
(filterModel: GridFilterModel) => { | ||
const { filteredRowsLookup } = apiRef.current.getFilterState(filterModel); | ||
return Object.keys(filteredRowsLookup).filter( | ||
(rowId) => filteredRowsLookup[rowId] === true, | ||
).length; | ||
}, | ||
[apiRef], | ||
); | ||
|
||
React.useEffect(() => { | ||
// Calculate the row count for predefined filters | ||
if (data.rows.length === 0) { | ||
return; | ||
} | ||
|
||
setPredefinedFiltersRowCount( | ||
predefinedFilters.map(({ filterModel }) => getFilteredRowsCount(filterModel)), | ||
); | ||
}, [apiRef, data.rows, getFilteredRowsCount]); | ||
|
||
return ( | ||
<div style={{ overflow: 'hidden' }}> | ||
<Stack direction="row" gap={1} mb={1} flexWrap="wrap"> | ||
{predefinedFilters.map(({ label, filterModel }, index) => { | ||
const count = predefinedFiltersRowCount[index]; | ||
return ( | ||
<Button | ||
key={label} | ||
onClick={() => apiRef.current.setFilterModel(filterModel)} | ||
variant="outlined" | ||
> | ||
{label} {count !== undefined ? `(${count})` : ''} | ||
</Button> | ||
); | ||
})} | ||
</Stack> | ||
<Box sx={{ height: 520, width: '100%' }}> | ||
<DataGridPro {...data} loading={data.rows.length === 0} apiRef={apiRef} /> | ||
</Box> | ||
</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
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