-
-
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
[datagrid] How to autosize columns on rows fetch #10578
Comments
I think I'd favor adding a recipe for 3. |
Thought the same while reading ... it provides the most value, since there will be a "blank" state instead of no grid being rendered at all |
In my opinion this should be handled by the library. When reading the docs it says that "Autosizing" is the default behaviour in pro- and premium-plans, so I would expect it to try to autosize on "rows" change and maybe give me an option to control this myself. |
Like @Madsim said, this seems like counter-intuitive behavior to me as well. Especially since there is a For example, if you're doing something like this: const { data, isLoading } = useQuery(myQueryOptions); then you need to pass in an empty array to To me it would make sense to have a prop |
I noticed that Solution 3 suggests using ReactDOM.flushSync, but the official React documentation states that using flushSync is uncommon and can hurt the performance of your app. For this reason, I opted for Solution 2, which seems to work fine. Here's the code I used:
This version better handles cases where the component gets unmounted, avoiding potential problems or errors caused by running timeouts after the component has been unmounted. The timeout solution seems to work, but I've noticed that the outliersFactor factor doesn't seem to function correctly when rows.length is greater than pageSize. When I set pageSize to be greater than or equal to rows.length, the autosize seems to fix itself. |
I was struggling with the same issue for the past couple of days. Are there any potential issues with this solution, that I might be overlooking? |
The problem in depth 🔍
The
autosizeOnMount
prop that works great when the rows data is available on grid mount.But it's not obvious how to autosize columns when the rows are fetched asynchronously after the grid is mounted.
We need to document a recommended solution for this use case.
Here is what I tried:
1. Call
apiRef.current.autosizeColumns
when rows are fetchedhttps://codesandbox.io/s/lucid-roentgen-ynchfy?file=/Demo.tsx
Pros:
autosizeColumns
does not work #10510)Cons:
autosizeColumns
can only measure column headers.2. Same as 1, but wrap
apiRef.current.autosizeColumns
withsetTimeout
https://codesandbox.io/s/sad-pasteur-cdyscq?file=/Demo.tsx
Pros:
Cons:
3. Use
ReactDOM.flushSync
+apiRef.current.updateRows
(instead ofrows
prop) andapiRef.current.autosizeColumns
https://codesandbox.io/s/interesting-ben-vh9ndk?file=/Demo.tsx
Pros:
Cons:
4. Render the data grid after the data is fetched and use the
autosizeOnMount
prop.Pros:
Cons:
key
will reset the grid state.Your environment 🌎
`npx @mui/envinfo`
Search keywords: datagrid autosize columns
The text was updated successfully, but these errors were encountered: