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

[data grid] Is there a way to determine if a table can horizontally scroll? #12234

Closed
MartinTiangco opened this issue Feb 27, 2024 · 8 comments
Closed
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability support: question Community support but can be turned into an improvement

Comments

@MartinTiangco
Copy link

MartinTiangco commented Feb 27, 2024

Summary

Hey, is there a way (from the API?) to determine if a horizontal scrollbar is in the table (ie if there are columns that can't fit in the table and must be scrolled to).

I've searched through the issues and the doc and found nothing related to this. It would be a great convenience to have this.

Examples

No response

Motivation

The motivation is this interaction: https://www.behance.net/gallery/90059503/Complex-data-tables-on-mobile

If the user can horizontally scroll, then it would be good to have a pinned column (like shown in the attached link) that expands/shows all the columns of a row item in the detail view.

If all the table's columns can fit without horizontally scrolling, then there wouldn't be a need to have the expand pinned column.

Search keywords: horizontal scrollbar, scroll

@MartinTiangco MartinTiangco added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 27, 2024
@michelengelen
Copy link
Member

@MartinTiangco I am almost 100% sure that we do not have a public API for this, however you could still do a manual calculation by targeting .MuiDataGrid-virtualScroller, which is the element that restricts the view, and .MuiDataGrid-topContainer, which will overflow that container. This seems rather hack though.

@romgrk you have worked on the scrolling recently ... do you have a better idea, or maybe a method readily available for this?

@michelengelen michelengelen added support: question Community support but can be turned into an improvement component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability labels Feb 28, 2024
@michelengelen michelengelen changed the title [question] Is there a way to determine if a table can horizontally scroll? [data grid] Is there a way to determine if a table can horizontally scroll? Feb 28, 2024
@romgrk
Copy link
Contributor

romgrk commented Feb 28, 2024

For v7:

const dimensions = apiRef.current.getRootDimensions()
const hasScroll = dimensions.columnsTotalWidth > dimensions.viewportOuterSize.width

Needs to run in an effect, after the dimensions have been calculated.

For v6, probably checking the .clientWidth and .offsetWidth of the virtual scroller element could work.

@romgrk romgrk added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 28, 2024
@michelengelen
Copy link
Member

@MartinTiangco would that be a sufficient solution for you?

@MartinTiangco
Copy link
Author

MartinTiangco commented Feb 28, 2024

Hey thanks for the solutions. I used VirtualScroller's scrollWidth and offsetWidth

Just a followup: If grid resizing is a possibility, I attached these checks in the onResize eventHandler of the grid. I wouldn't suppose there's a better alternative?

const [isGridOverflowing, setIsGridOverflowing] = useState(false);

<DataGrid
   ...
    onResize={() => {
        const { offsetWidth } = ref.current.querySelector(
          '.MuiDataGrid-virtualScroller',
        );
        const { scrollWidth } = ref.current.querySelector(
          '.MuiDataGrid-virtualScroller',
        );
        const isOverflow = scrollWidth > offsetWidth;
      
        if (isOverflow === isGridOverflowing) {
            // Do nothing as it is the same;
            return;
        }
        setIsGridOverflowing(isOverflow);
    }}
/>

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Feb 28, 2024
@romgrk
Copy link
Contributor

romgrk commented Feb 29, 2024

No, that solution seems good.

@romgrk romgrk added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 29, 2024
@MartinTiangco
Copy link
Author

MartinTiangco commented Feb 29, 2024

ok cool, thanks for the help!

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Feb 29, 2024
@romgrk
Copy link
Contributor

romgrk commented Feb 29, 2024

Sure, let us know if you need anything else.

@romgrk romgrk closed this as completed Feb 29, 2024
Copy link

⚠️ This issue has been closed.
If you have a similar problem, please open a new issue and provide details about your specific problem.
If you can provide additional information related to this topic that could help future readers, please feel free to leave a comment.

How did we do @MartinTiangco?
Your experience with our support team matters to us. If you have a moment, please share your thoughts through our brief survey.

@romgrk romgrk removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

3 participants