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

how to wrap text in cell of DataGrid #1040

Closed
NavizDev opened this issue Feb 13, 2021 · 17 comments
Closed

how to wrap text in cell of DataGrid #1040

NavizDev opened this issue Feb 13, 2021 · 17 comments
Labels
component: data grid This is the name of the generic UI component, not the React module! duplicate This issue or pull request already exists

Comments

@NavizDev
Copy link

NavizDev commented Feb 13, 2021

I'm trying to wrap text in a cell of the data grid. Is there a way to wrap text in a table column? I using <DataGrid />:

image

.MuiDataGrid-root .MuiDataGrid-cell {
  color: tomato; /* work */
  white-space: 'normal' !important; /* don't work */
  word-wrap: 'break-word' !important; /* don't work */
}
@oliviertassinari oliviertassinari transferred this issue from mui/material-ui Feb 13, 2021
@oliviertassinari oliviertassinari added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 15, 2021
@oliviertassinari
Copy link
Member

Do you expect the height of the row to adapt to the number of wrapped lines?

@NavizDev
Copy link
Author

@oliviertassinari yes.

@alessandrocapra
Copy link

Same here, I am considering going back to Table because of this. I google for quite some time and it seems the feature has not been released yet, but if there's any workaround please share :)

@dtassone
Copy link
Member

dtassone commented Feb 17, 2021

Having dynamic row height is on the pipeline but it might take some time before it gets to production.
In the meantime, I have created a demo with a cell expand renderer which allows seeing the full content of the cell when going over it.

Check out https://codesandbox.io/s/expandcelldemo-enbw7

Screenshot 2022-12-25 at 13 36 54

@oliviertassinari
Copy link
Member

Should we close as a duplicate of #417 and post the Popper workaround there?

@jfosterdavis
Copy link

Can anybody help with an ES6 popper?

@oliviertassinari
Copy link
Member

Duplicate of #417

@oliviertassinari oliviertassinari marked this as a duplicate of #417 Feb 21, 2021
@github-actions github-actions bot added duplicate This issue or pull request already exists and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 21, 2021
@chriszertamayo
Copy link

@dtassone not in tsx please but in js?

@tielushko
Copy link

@chriszertamayo
https://codesandbox.io/s/bjupl?file=/demo.js

@rushilzala

This comment was marked as resolved.

@shoaib1985
Copy link

shoaib1985 commented Jul 29, 2022

@NavizDev Please try the following in your CSS:

.MuiDataGrid-root .MuiDataGrid-cell {
  color: tomato;
  white-space: normal !important;
  word-wrap: break-word !important;
}

this should be working.

@sadiki-o
Copy link

sadiki-o commented Dec 21, 2022

here is a working solution using Tooltip:

/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { Tooltip } from '@mui/material';
import { GridRenderCellParams } from '@mui/x-data-grid';
import { useEffect, useRef, useState } from 'react';

const RenderExpandableCell = (props: GridRenderCellParams) => {
  const [isOverflowed, setIsOverflow] = useState(false);
  const { value } = props;

  const textElementRef = useRef<HTMLSpanElement | null>(null);

  const checkOverflow = () => {
    // Using getBoundingClientRect, instead of scrollWidth and clientWidth, to get width with fractional accuracy
    const clientWidth = textElementRef.current!.getBoundingClientRect().width;

    textElementRef.current!.style.overflow = 'visible';
    const contentWidth = textElementRef.current!.getBoundingClientRect().width;
    textElementRef.current!.style.overflow = 'hidden';

    setIsOverflow(contentWidth > clientWidth);
  };

  useEffect(() => {
    checkOverflow();
    window.addEventListener('resize', checkOverflow);
    return () => {
      window.removeEventListener('resize', checkOverflow);
    };
  }, []);

  return (
    <Tooltip title={value} disableHoverListener={!isOverflowed}>
      <span
        ref={textElementRef}
        style={{
          whiteSpace: 'nowrap',
          overflow: 'hidden',
          textOverflow: 'ellipsis',
        }}
      >
        {value}
      </span>
    </Tooltip>
  );
};

export default RenderExpandableCell;

Screenshot 2022-12-25 at 14 07 00

https://codesandbox.io/s/expandcelldemo-forked-6dzrqh?file=/src/App.tsx

@alsiddsarkar
Copy link

This works:

getRowHeight={(params) => "auto"}

@jnewman-phare
Copy link

This works:

getRowHeight={(params) => "auto"}

This is the best and most simple solution, thanks!

@jvallesmasttro
Copy link

Is there a way to do the same with datagrid headers?

@csatish
Copy link

csatish commented Sep 27, 2024

Is there a way to do the same with datagrid headers?
In the DataGrid props :

sx={{ '& .MuiDataGrid-columnHeaderTitleContainer': { whiteSpace: 'normal' }, }}

@rahulchougule
Copy link

This works:
getRowHeight={(params) => "auto"}

This is the best and most simple solution, thanks!

My text in data grid cell was truncated, on mobile device. I added padding to make UI good along with this solution.

getRowHeight={() => 'auto'}
sx={{
'.MuiDataGrid-cell': {
fontSize: '16px',
padding:'10px'
}
}}

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! duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests