-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[data grid] Did MUI X v7 break tree data children selection? #13391
Comments
Hey @genepaul ... it does indeed not rerender on a selection model change anymore. Here is the adjusted piece of code: const checkboxColumn = (apiRef: MutableRefObject<GridApiPremium>): GridColDef => {
return {
...GRID_CHECKBOX_SELECTION_COL_DEF,
renderHeader: (params) => {
const children = gridFilteredSortedRowIdsSelector(
apiRef.current.state,
apiRef.current.instanceId,
).filter((id) => !id.toString().includes('auto-generated'));
const selectionLookup = selectedIdsLookupSelector(
apiRef.current.state,
apiRef.current.instanceId,
);
const indeterminate =
children?.some((child) => selectionLookup[child] === undefined) &&
children?.some((child) => selectionLookup[child] !== undefined);
const checked =
Object.keys(selectionLookup).length > 0 &&
children?.every((child) => selectionLookup[child] !== undefined);
const data: GridColumnHeaderParams & {
indeterminate?: boolean;
checked?: boolean;
disabled?: boolean;
onClick?: (e: MouseEvent) => void;
} = {
...params,
onClick: (e) => {
apiRef.current.selectRows(children, indeterminate || !checked);
e.preventDefault();
},
indeterminate,
checked,
};
return (
<>
<GridHeaderCheckbox {...data} />
</>
);
},
renderCell: (params) => {
const { rowNode } = params;
if (rowNode.type !== 'group') return <GridCellCheckboxRenderer {...params} />;
const selectionLookup = useGridSelector(apiRef, gridRowSelectionStateSelector);
const children = apiRef.current.getRowGroupChildren({
groupId: rowNode.id,
applyFiltering: true,
applySorting: true,
});
const indeterminate =
children?.some((child) => selectionLookup.includes(child)) &&
children?.some((child) => !selectionLookup.includes(child));
const checked = children?.every((child) => selectionLookup.includes(child));
const extraData: GridRenderCellParams &
GridRenderCellParamsPremium & {
indeterminate?: boolean;
checked?: boolean;
disabled?: boolean;
onClick?: (e: MouseEvent) => void;
} = {
...params,
disabled: false,
onClick: (e) => {
if (rowNode.type === 'group') {
if (children) {
apiRef.current.selectRows(children, indeterminate || !checked);
}
e.preventDefault();
}
},
indeterminate,
checked,
};
return <GridCellCheckboxRenderer {...extraData} />;
},
};
}; Hope this helps! |
This really helped! Thank you! |
@genepaul: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
Side note: the header checkbox in the example code above has the same issue as the cell checkbox did - need to use |
Hey, I've been trying many tree data children selection solutions since this morning, and this one seems the most promising. I'm using
as I'm being prevented from using a hook in the renderCell function. I'm desperately looking for a working codesandbox ! Thanks ! |
@jonathanasquier - what is preventing you from using a hook? My understanding is that the hooks are the way to go, as they are kept up to date. I've had similar issues with trying to use apiRef.current.state as well, and the hooks have been the answer in those scenarios. |
Solved it by moving the hook above the I reimplemented the grid in isolation, and then dropped it in my project, worked like a charm 🤷♂️ Updated version:
Thank you ! |
The problem in depth
I posted this as a comment on #4248 but haven't gotten much response, and I'd like some help troubleshooting this. I'm using latest of the DataGrid Premium, and it seems the code posted in #4248 no longer works correctly. It seems some improvements were made where only affected cells are re-rendering when a checkbox is clicked, which does not allow a parent row to catch that a child was selected and it should render as indeterminate. Here's an example:
https://react-kckdew.stackblitz.io/
If you select a child row, notice the parent row doesn't render indeterminate until you collapse or expand that row (thus causing it to rerender).
Your environment
`npx @mui/envinfo`
Search keywords: treeData children selection indeterminate
Order ID: 0332927583
The text was updated successfully, but these errors were encountered: