-
-
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] autosizeColumns
on columnVisibilityModelChange
fails
#14922
Comments
@KenanYusuf could you have a look into this? This might just be a race condition or a false order of execution. |
autosizeColumns
on columnVisibilityModelChange
fails
Sry, I just had a look and you can just call the autosizing on every render, or when the model changes in state: React.useEffect(() => {
apiRef.current.autosizeColumns({
expand: true,
includeHeaders: true,
includeOutliers: false,
});
}, [columnVisibilityModel]); This will fix it! |
Sry again. It will still throw an error when you hide a column with the column management panel. Showing it through that or hiding from the column menu works though! 🤷🏼 |
I haven’t found a solution for this issue yet except in uncontrolled mode for columnVisibility. Once it’s resolved, do you have any recommendations for the best approach? Should I select the columnVisibilityModel from the state or subscribe to the event? First approach : const columnVisibilityModel = useGridSelector(
apiRef,
gridColumnVisibilityModelSelector
);
useEffect(() => {
apiRef.current.autosizeColumns(autosizeOptions);
}, [columnVisibilityModel]); Second Approach : useGridApiEventHandler(apiRef, "columnVisibilityModelChange", () => {
apiRef.current.autosizeColumns(autosizeOptions);
}); |
It seems to be a state out-of-sync between https://stackblitz.com/edit/react-qsqwfi-hifr2v?file=Demo.tsx A simple internal fix could be to skip rendering the outdated columns: diff --git a/packages/x-data-grid/src/components/GridRow.tsx b/packages/x-data-grid/src/components/GridRow.tsx
index 03c8b20797..e0d360e2dc 100644
--- a/packages/x-data-grid/src/components/GridRow.tsx
+++ b/packages/x-data-grid/src/components/GridRow.tsx
@@ -443,6 +443,9 @@ const GridRow = React.forwardRef<HTMLDivElement, GridRowProps>(function GridRow(
const column = visibleColumns[i];
const indexInSection = i - pinnedColumns.left.length;
+ if (!column) {
+ continue;
+ }
cells.push(getCell(column, indexInSection, i, middleColumnsLength));
}
if (hasVirtualFocusCellRight) { For a more sophisticated fix, we might want to ensure @romgrk I'd be curious to know what you think about the quick fix I suggested. |
If it's being used in a component that is rendered inside the Data Grid, it's better to go with the approach 1 as For the use-cases outside the Data Grid (like the example attached with this issue), approach 2 should be the way to go. |
The quick fix LGTM. The state management needs work to be able to sync parts like |
I encountered this issue on #14393 when switching between list view and normal view. I have made @MBilalShafi's suggested fix there https://github.com/mui/mui-x/pull/14393/files#diff-f3fbb1e13f754215032d5ddeec1713b812667d502e65c180c3ca9eabf048c7cbR447-R449 |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note @ddecrulle 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. |
Steps to reproduce
https://stackblitz.com/edit/react-qsqwfi?file=Demo.tsx
Steps:
Current behavior
I want to resize my grid when column visibility changes. So, I tried to run autosizeColumns when the columnVisibilityModelChange event is fired. However, when a column is removed, this results in the following error:
It appears that the autosizeColumns function attempts to access the computedWidth of a column that has already been removed.
Expected behavior
No error in console
Context
I tried the same behavior without controlling the value of columnVisibilityModel, and it worked as expected without any errors.
Your environment
npx @mui/envinfo
Search keywords: autosizeColumns, columnVisibilityModelChange
The text was updated successfully, but these errors were encountered: