-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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] autoheight + maxHeight to limit the displayed rows doesn't work #3524
Comments
Setting a The number of rows will exceed the available space: <div style={{ height: 300, overflow: "auto" }}>
<DataGrid autoHeight={false} />
</div> The number of rows will not exceed the available space: <div style={{ height: 'auto', overflow: "auto" }}>
<DataGrid autoHeight={true} />
</div> Does it solve your case? |
Thank you for your answer @m4theushw , but no, unfortunately it doesn't cover my case. Let me provide some examples which looks bad in my project (when |
This comment was marked as outdated.
This comment was marked as outdated.
Try this: rows per page and on parent container and Datagrid prop |
Not so good if there are less than rpp rows in your dataset. Try this... |
Mine was like this |
=> afterstruggling for a while finally this works for me like a magic:
now I have complete control over the width and height through the top level box #DataGridContainer
|
I managed to get this working, though I had to use some pretty hacky css selectors in the SX. Height is controlled by the parent Box
|
am i the only one who finds these "styling" solutions abysmal? |
Hi! Is there a solution that works with tree data as well? In this case the number of visible rows can change, so it's not enough to simply count all rows. Is there a way to get the number of visible (parent / child) rows before they are rendered? It would be great if MUI DataGrid had a EDIT: Veeery early version of an experiment to make it work with tree data and fixed row height: const useAutoMaxHeight = ({
api,
rowHeight,
}: {
api: GridApiPro
rowHeight: number
}) => {
const [isAutoHeightEnabled, setIsAutoHeightEnabled] = React.useState(true)
useEffect(() => {
if (api.subscribeEvent && api.state && api.instanceId) {
return api.subscribeEvent('stateChange', () => {
const visibleRowIds = gridExpandedSortedRowIdsSelector(
api.state,
api.instanceId,
)
// Table height without header and pinned rows
const tableHeight = rowHeight * visibleRowIds.length
const viewportHeight = window.innerHeight
setIsAutoHeightEnabled(tableHeight < viewportHeight)
})
}
}, [api, rowHeight])
return isAutoHeightEnabled
}
export const MyDataGrid = (props) => {
const apiRef = useGridApiRef()
const isAutoHeightEnabled = useAutoMaxHeight({
api: apiRef.current,
rowHeight: props.rowHeight,
})
return (
<div
style={{
height: isAutoHeightEnabled ? 'auto' : '100vh',
overflow: 'auto',
}}
>
<DataGridPro
apiRef={apiRef}
autoHeight={isAutoHeightEnabled}
...
/>
</div>
)
} |
Other workaround can be: <DataGrid
autoHeight={true}
sx={{
'.MuiDataGrid-virtualScroller': {
overflowY: 'auto !important',
// example of maxHeight
maxHeight: 'calc(100vh - 100px) !important',
},
}}
...otherProps
/> |
I have a similar use-case to this issue, where I have a page containing several DataGrids that I want to have a maximum height, but also shrink to a smaller height if needed. Previously, in x-data-grid 6.x, I was able to solve this with a flexbox container with a max-height. For example: <Box
sx={{
display: 'flex',
flexDirection: 'column',
maxHeight: '400px',
}}
>
<DataGrid rows={rows} columns={columns} />
</Box> However, upon upgrading to x-data-grid 7.x, this stopped working (the DataGrid sizes correctly, but scrolling doesn't work).
Based on the age of this issue, the discussion in comments around programmatic sizing or hard-coded style workarounds, and also the callout in these docs: https://mui.com/x/react-data-grid/layout/
...I'm starting to think that the CSS-only flexbox solution I was using wasn't officially supported. Would someone be able to confirm this? I thought about opening a new issue, but it seemed like it was going to be a duplicate of this one. |
This comment was marked as off-topic.
This comment was marked as off-topic.
@greglittlefield-wf Yeah, this looks like a regression.
diff --git a/packages/x-data-grid/src/components/virtualization/GridMainContainer.tsx b/packages/x-data-grid/src/components/virtualization/GridMainContainer.tsx
index 8fcbd0a41..c8b4ca5e3 100644
--- a/packages/x-data-grid/src/components/virtualization/GridMainContainer.tsx
+++ b/packages/x-data-grid/src/components/virtualization/GridMainContainer.tsx
@@ -20,6 +20,8 @@ const Element = styled('div', {
flexGrow: 1,
position: 'relative',
overflow: 'hidden',
+ display: 'flex',
+ flexDirection: 'column',
});
export const GridMainContainer = React.forwardRef<
<div style={{ maxHeight: 300, display: "flex", flexDirection: "column" }}>
<DataGrid {...data} rows={data.rows.slice(0, nbRows)} />
</div> Screen.Recording.2024-08-28.at.01.01.28.mov
|
@oliviertassinari When should we expect item 1 from solution above to be fixed and released? Also this doesn't work for virtualized data grid (i.e. DataGridPro) - when you remove a row, the data grid doesn't shrink and it stays the same height. I tried adding |
@dimon82 I don't know, I guess when someone opens a PR to apply this fix. But I would imagine that we don't do 1. without 3. We might be waiting on more upvotes.
I can't reproduce this issue with the above diff applied. |
@oliviertassinari actually doesn't even have to be virtualized, regular paginated data grid is not working either. Try to decrease the number of columns so there is no horizontal scrollbar and it won't shrink when rows are removed: then try adding also since diff is a part of mui package, how would I apply it in my code? just override styles for |
@dimon82 Right, I should have updated the codesandbox linked in #3524 (comment) to demonstrate the diff. This is now done, it should work. |
@oliviertassinari this only fixes vertical scroll, but still doesn't fix the problem of height being adjusted when you remove rows when there is no horizontal scrollbar (less columns), see below, try to remove rows https://codesandbox.io/p/sandbox/autoheightgrid-material-demo-forked-m2vnq3 |
@dimon82 Interesting, so #3524 (comment) is only fixing v7 regression, but not this issue. It looks like that when there is no horizontal scrollbar (not enough columns), then the |
Interesting, I was not aware this was possible in v6: https://stackblitz.com/edit/react-v9w8tw-55hdpw?file=Demo.tsx |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note We value your feedback @venik2007! How was your experience with our support team? |
Summary 💡
I need to have a data grid embedded into some Summary Page content.
So before and after the
<DataGrid>
component I have some data. The<DataGrid>
should be flexible (emptyDataGrid
looks not good enough in my case) and for 1 row should be small (like it already happens when autosize is switched on). But for more than 5 rows (for ex.) I need to prevent further<DataGrid>
stretching to keep the rest content visible.Examples 🌈
Here is an example https://codesandbox.io/s/autoheightgrid-material-demo-forked-r48po?file=/demo.tsx.
Screen.Recording.2024-08-17.at.18.29.47.mov
In that example, I've met really close implementation to what I want, but unfortunately, datagrid header and footer stay unpinned and disappear on scrolling. But I need behavior like
DataGrid
without autoheight with specified height.Is it possible to solve my problem somehow with the current functionality?
Motivation 🔦
I'm totally ok with:
DataGrid
's header/footer (throughMuiThemes
, if it will not break anything inDataGrid
virtualization)Order ID 💳 (optional)
No response
The text was updated successfully, but these errors were encountered: