Skip to content
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

Autoresize does not properly resize columns that are scrolled far off screen when the resize is requested #15048

Closed
Blargel opened this issue Oct 21, 2024 · 3 comments · Fixed by #15116
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/

Comments

@Blargel
Copy link

Blargel commented Oct 21, 2024

Steps to reproduce

Link to live example: (required)
https://codesandbox.io/p/sandbox/condescending-paper-lfpgkq?from-embed=&workspaceId=ff0972be-f965-4d9b-9731-28487014ad71

Steps:

  1. Make sure your viewport is narrow enough that a large portion of the columns cannot be displayed and must be scrolled
  2. Click the autosize columns button
  3. Scroll the columns all the way to the right and observe that the last few columns have not been resized properly

Current behavior

Columns that are far outside of the viewport at the time the resize is requested are not resized properly. The narrower your viewport, the more columns will be improperly resized. If your viewport happens to be wide enough, the resizing will work fine.

Expected behavior

All columns should be resized correctly regardless of viewport

Context

I am displaying a data grid with a large amount of columns. The rows are fetched from a server and then the columns are resized automatically once fetching is complete. I would like for the columns to properly resize themselves instead of hardcoding some minimum widths to the columns which I am currently doing to work around the issue.

Your environment

npx @mui/envinfo
  System:
    OS: macOS 14.6.1
  Binaries:
    Node: Not Found
    npm: 10.9.0 - ~/projects/platform-web/node_modules/.bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: 129.0.6668.101
    Edge: Not Found
    Safari: 17.6
  npmPackages:
    @emotion/react: ^11.11.4 => 11.13.3 
    @emotion/styled: ^11.11.0 => 11.13.0 
    @mui/core-downloads-tracker:  6.1.3 
    @mui/icons-material: ^6.0.0 => 6.1.3 
    @mui/material: ^6.0.0 => 6.1.3 
    @mui/private-theming:  6.1.3 
    @mui/styled-engine:  6.1.3 
    @mui/system: ^6.0.0 => 6.1.3 
    @mui/types:  7.2.18 
    @mui/utils:  6.1.3 
    @mui/x-data-grid: ^7.3.0 => 7.20.0 
    @mui/x-data-grid-pro: ^7.4.0 => 7.20.0 
    @mui/x-date-pickers: ^7.1.1 => 7.20.0 
    @mui/x-date-pickers-pro: ^7.3.1 => 7.20.0 
    @mui/x-internals:  7.20.0 
    @mui/x-license: ^7.2.0 => 7.20.0 
    @types/react: ^18.3.1 => 18.3.11 
    react: ^18.2.0 => 18.3.1 
    react-dom: ^18.2.0 => 18.3.1 
    typescript: ^5.5.0 => 5.6.3 

This was tested on Firefox and Chrome.

Search keywords: datagrid autoresize resize

Order ID or Support Key

89478
(sorry for the edit, the person with the receipt wasn't available to give it to me until just now)

@Blargel Blargel added bug 🐛 Something doesn't work status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Oct 21, 2024
@github-actions github-actions bot added the component: data grid This is the name of the generic UI component, not the React module! label Oct 21, 2024
@KenanYusuf KenanYusuf added the support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/ label Oct 22, 2024
@KenanYusuf
Copy link
Member

This indeed looks like a bug - I will add it to the board for prioritisation.

@github-project-automation github-project-automation bot moved this to 🆕 Needs refinement in MUI X Data Grid Oct 23, 2024
@KenanYusuf KenanYusuf moved this from 🆕 Needs refinement to 🔖 Ready in MUI X Data Grid Oct 23, 2024
@KenanYusuf KenanYusuf removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Oct 23, 2024
@ppaskaris-plooto
Copy link

For anyone looking for a workaround in the interim, here's what I'm using.

  const autosizeColumns = useCallback(() => {
    if (apiRef.current == null) {
      return;
    }
    // DataGrid currently shrinks off-screen columns despite it turning off virtualization to
    // compute cell widths. To work around this, we will peek into the virtualization layer and only
    // request it autosize columns that are actually being rendered.
    //
    // See: https://github.com/mui/mui-x/issues/15048
    let columns: Array<string>;
    const gridVirtualizationColumnEnabled = gridVirtualizationColumnEnabledSelector(
      apiRef.current.state,
      apiRef.current.instanceId
    );
    if (gridVirtualizationColumnEnabled) {
      const gridRenderContextColumns = gridRenderContextColumnsSelector(
        apiRef.current.state,
        apiRef.current.instanceId
      );
      const visibleColumns = gridVisibleColumnDefinitionsSelector(
        apiRef.current.state,
        apiRef.current.instanceId
      );
      columns = visibleColumns
        .filter(
          (_, index) =>
            index >= gridRenderContextColumns.firstColumnIndex &&
            index <= gridRenderContextColumns.lastColumnIndex
        )
        .map((column) => column.field);
    }
    apiRef.current.autosizeColumns({ ...AutosizeOptions, columns });
  }, [apiRef]);

Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@Blargel 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/
Projects
Status: 🔖 Ready
Development

Successfully merging a pull request may close this issue.

3 participants