Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI DataGrid dynamic row height based on text content

When using DataGrid, I cannot figure out how to make row heights variable, so that the row's height is dynamically based on the length of the text content in the cell.

I was thinking I would need to add renderCell on the column with the longer text, and use the <Typography> component, but I don't know what params to use to style it this way.

There is a lot of documentation on how to handle truncation, ellipsis, etc, but I cannot seem to figure out what I need to apply for variable row heights based on content.

like image 937
DaveH Avatar asked Mar 02 '21 15:03

DaveH


Video Answer


3 Answers

const StyledDataGrid = withStyles({
    root: {
        '& .MuiDataGrid-renderingZone': {
            maxHeight: 'none !important',
        },
        '& .MuiDataGrid-cell': {
            lineHeight: 'unset !important',
            maxHeight: 'none !important',
            whiteSpace: 'normal',
        },
        '& .MuiDataGrid-row': {
            maxHeight: 'none !important',
        },
    },
})(DataGrid);

See this demo for an example.

like image 164
Brandon Chutuk Avatar answered Oct 17 '22 00:10

Brandon Chutuk


Based on this thread it's not possible.

@ronnyroeller thanks for the feature request. It's definitely something we were expecting. We are happy to see it surface :). For the virtualization to work, the grid needs to know the size of all the rows.

like image 38
cortisol Avatar answered Oct 17 '22 00:10

cortisol


As of Data Grid version 5.12.0 there is support for dynamic row height.

To use it:

<DataGrid getRowHeight={() => 'auto'} />

See documentation here for more details.

like image 7
M Polak Avatar answered Oct 17 '22 01:10

M Polak