Skip to content

Commit 4fe7d53

Browse files
committed
account for client side filtering on total row count for grid
1 parent 91adeb0 commit 4fe7d53

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/components/Grid/Toolbar.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import { Stack, Typography } from '@mui/material';
2-
import { useGridRootProps } from '@mui/x-data-grid-pro';
2+
import {
3+
gridFilteredSortedRowIdsSelector,
4+
useGridApiContext,
5+
useGridRootProps,
6+
useGridSelector,
7+
} from '@mui/x-data-grid-pro';
8+
import { useMemo } from 'react';
39
import { ChildrenProp, extendSx, StyleProps } from '~/common';
410
import { FormattedNumber } from '../Formatters';
511

612
export const Toolbar = (props: ChildrenProp & StyleProps) => {
713
const rootProps = useGridRootProps();
14+
const apiRef = useGridApiContext();
15+
const filteredRowIds = useGridSelector(
16+
apiRef,
17+
gridFilteredSortedRowIdsSelector
18+
);
19+
20+
const displayCount = useMemo(() => {
21+
const { items } = rootProps.filterModel ?? {};
22+
const hasFilters = items && items.length > 0;
23+
const count = hasFilters ? filteredRowIds.length : rootProps.rowCount;
24+
25+
return count;
26+
}, [rootProps.filterModel, filteredRowIds.length, rootProps.rowCount]);
827

928
return (
1029
<Stack
@@ -24,11 +43,9 @@ export const Toolbar = (props: ChildrenProp & StyleProps) => {
2443
]}
2544
>
2645
{props.children}
27-
{rootProps.rowCount && (
28-
<Typography>
29-
Total Rows: <FormattedNumber value={rootProps.rowCount} />
30-
</Typography>
31-
)}
46+
<Typography minWidth={100}>
47+
Total Rows: {displayCount && <FormattedNumber value={displayCount} />}
48+
</Typography>
3249
</Stack>
3350
);
3451
};

0 commit comments

Comments
 (0)