How can I make all rows expand/collapse programmatically in MUI ReactJS DataGrid
?
What have I tried?
I used prop defaultGroupingExpansionDepth
based on MUI documentation:
export const EXPAND_ALL = -1;
export const COLLAPSE_ALL = 0;
...
const [expandedState, setExpandedState] = useState(COLLAPSE_ALL);
...
return <Stack>
<Stack spacing={2} direction="row" m={1}>
<Button variant={"contained"} onClick={() => setExpandedState(EXPAND_ALL)}>Expand All</Button>
<Button variant={"contained"} onClick={() => setExpandedState(COLLAPSE_ALL)}>Collapse All</Button>
</Stack>
<DataGridPro
treeData
...
apiRef={dataGridApi}
defaultGroupingExpansionDepth={expandedState}
... />
</Stack>;
But the problem is that these buttons only work when tree is not already partially expanded.
As soon as I partially expand the tree grid, these buttons don't work any more. How can I make these buttons work irrespective of the current expansion/collapse state of the tree grid?