Material-UI Table Collapsible Rows in-line with regular rows - css

I'm using material-ui v4 and I'm trying to get my collapsible row columns to be in-line with my regular columns.
I posted a photo below along with the code. You can see that when I expand the Frozen Yogurt row, the columns aren't inline with the headings (dessert, calories, fat, carbs, protein). Is it possible to get the columns in the collapsible row to line up with the table headings? I also want them to stay in-line when the screen is re-sized.
image of issue
import React from "react";
import PropTypes from "prop-types";
import { makeStyles } from "#material-ui/core/styles";
import Box from "#material-ui/core/Box";
import Collapse from "#material-ui/core/Collapse";
import IconButton from "#material-ui/core/IconButton";
import Table from "#material-ui/core/Table";
import TableBody from "#material-ui/core/TableBody";
import TableCell from "#material-ui/core/TableCell";
import TableContainer from "#material-ui/core/TableContainer";
import TableHead from "#material-ui/core/TableHead";
import TableRow from "#material-ui/core/TableRow";
import Typography from "#material-ui/core/Typography";
import Paper from "#material-ui/core/Paper";
import KeyboardArrowDownIcon from "#material-ui/icons/KeyboardArrowDown";
import KeyboardArrowUpIcon from "#material-ui/icons/KeyboardArrowUp";
const useRowStyles = makeStyles({
root: {
"& > *": {
borderBottom: "unset"
}
}
});
function createData(name, calories, fat, carbs, protein, price) {
return {
name,
calories,
fat,
carbs,
protein,
price,
history: [
{ date: "2020-01-05", customerId: "11091700", amount: 3 },
{ date: "2020-01-02", customerId: "Anonymous", amount: 1 }
]
};
}
function Row(props) {
const { row } = props;
const [open, setOpen] = React.useState(false);
const classes = useRowStyles();
return (
<React.Fragment>
<TableRow className={classes.root}>
<TableCell>
<IconButton
aria-label="expand row"
onClick={() => setOpen(!open)}
>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
</IconButton>
</TableCell>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
<TableRow>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
<Collapse in={open} timeout="auto" unmountOnExit>
<Box margin={1}>
<Table aria-label="purchases">
<TableBody>
{row.history.map((historyRow) => (
<TableRow key={historyRow.date}>
<TableCell component="th" scope="row">
<IconButton
aria-label="expand row"
onClick={() => setOpen(!open)}
>
{open ? (
<KeyboardArrowUpIcon />
) : (
<KeyboardArrowDownIcon />
)}
</IconButton>
</TableCell>
<TableCell component="th" scope="row">
Inline with dessert column
</TableCell>
<TableCell align = 'right'>Inline with calories</TableCell>
<TableCell align="right">Inline with fat</TableCell>
<TableCell align="right">Inline with carbs</TableCell>
<TableCell align="right">Inline with protein</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Box>
</Collapse>
</TableCell>
</TableRow>
</React.Fragment>
);
}
Row.propTypes = {
row: PropTypes.shape({
calories: PropTypes.number.isRequired,
carbs: PropTypes.number.isRequired,
fat: PropTypes.number.isRequired,
history: PropTypes.arrayOf(
PropTypes.shape({
amount: PropTypes.number.isRequired,
customerId: PropTypes.string.isRequired,
date: PropTypes.string.isRequired
})
).isRequired,
name: PropTypes.string.isRequired,
price: PropTypes.number.isRequired,
protein: PropTypes.number.isRequired
}).isRequired
};
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0, 3.99),
createData("Ice cream sandwich", 237, 9.0, 37, 4.3, 4.99),
createData("Eclair", 262, 16.0, 24, 6.0, 3.79),
createData("Cupcake", 305, 3.7, 67, 4.3, 2.5),
createData("Gingerbread", 356, 16.0, 49, 3.9, 1.5)
];
export default function CollapsibleTable() {
return (
<TableContainer component={Paper}>
<Table aria-label="collapsible table">
<TableHead>
<TableRow>
<TableCell />
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<Row key={row.name} row={row} />
))}
</TableBody>
</Table>
</TableContainer>
);
}

You can specify negative margin as I think that should solve your issue. I have added it as inline style, but you can use it by adding an object inside makeStyles
<IconButton
style={{ marginLeft: "-24px" }}
aria-label="expand row"
onClick={() => setOpen(!open)}>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
</IconButton>;

Related

How to style a selected cell from the first column in the table Material UI

I'm working on the material ui table from this codesandbox
and I'm trying to change the style of the cell from the first column in the table if the cell is selected to look like this image below.
Apparently, I'm doing something wrong, but I don't know what. How can I style only the selected cell in the table instead of styling the whole row? Thanks in advance.
You're applying the className={classes.tableCell} to all the <TableCell />. You have to only apply that on the first one.
import React, { useState } from "react";
import PropTypes from "prop-types";
import { withStyles } from "#material-ui/core";
import Table from "#material-ui/core/Table";
import TableBody from "#material-ui/core/TableBody";
import TableCell from "#material-ui/core/TableCell";
import TableHead from "#material-ui/core/TableHead";
import TableRow from "#material-ui/core/TableRow";
import Paper from "#material-ui/core/Paper";
const styles = (theme) => ({
root: {
width: "100%",
marginTop: theme.spacing.unit * 3,
overflowX: "auto"
},
table: {
minWidth: 700
},
tableRow: {
"&$selected, &$selected:hover": {
backgroundColor: "white"
}
},
tableCell: {
"$selected &": {
color: "red",
backgroundColor: "yellow"
}
},
hover: {},
selected: {}
});
let id = 0;
function createData(name, calories, fat, carbs, protein) {
id += 1;
return { id, name, calories, fat, carbs, protein };
}
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0),
createData("Ice cream sandwich", 237, 9.0, 37, 4.3),
createData("Eclair", 262, 16.0, 24, 6.0),
createData("Cupcake", 305, 3.7, 67, 4.3),
createData("Gingerbread", 356, 16.0, 49, 3.9)
];
function SimpleTable(props) {
const { classes } = props;
const [selectedID, setSelectedID] = useState([]);
const clickHandler = (id) => {
const array = [...selectedID];
const index = array.indexOf(id);
if (index !== -1) {
array.splice(index, 1);
setSelectedID(array);
} else {
setSelectedID([...array, id]);
}
};
return (
<Paper className={classes.root}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow
hover
key={row.id}
onClick={() => {
clickHandler(row.id);
}}
selected={selectedID.includes(row.id)}
classes={{ hover: classes.hover, selected: classes.selected }}
className={classes.tableRow}
>
<TableCell
className={classes.tableCell}
component="th"
scope="row"
>
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Paper>
);
}
SimpleTable.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(SimpleTable);

How to align all the table cells inside the collapsed row with the cells in expandable row?

I'm trying to create a table with expandable/collapsible rows. The head row with a taskID
and first taskAction is rendered as Expandable row with <IconButton>. The other taskActions of the same taskID (collapsible rows) are rendered inside the <Collapse> container. While running the component with mock data, the cells inside the collapse are not properly aligned with the head row.
Screenshot of the table body with head rows:
Screenshot of misaligned collapsible rows
Code for the table:
function Task(props: { task: taskProps }) {
const { task } = props;
console.log(task);
const [open, setOpen] = React.useState(false);
return (
<>
{task.tasksActions.map((taskAction, index) => {
if(index == 0){
return(
<TableRow key={index}>
<TableCell>
<IconButton
aria-label="expand row"
size="small"
onClick={() => setOpen(!open)}
>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
</IconButton>
</TableCell>
<TableCell>
{task.taskId}
</TableCell>
<TableCell>
{taskAction.actionId}
</TableCell>
<TableCell>{taskAction.templateStepId}</TableCell>
<TableCell>{taskAction.templateStepName}</TableCell>
<TableCell>100</TableCell>
<TableCell>{taskAction.startedAt}</TableCell>
<TableCell>{taskAction.endedAt}</TableCell>
<TableCell>{taskAction.status}</TableCell>
</TableRow>
);
}
else {
return(
<TableRow key={index}>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={9}>
<Collapse in={open} timeout="auto" unmountOnExit>
<TableCell>{' '}</TableCell>
<TableCell>
{task.taskId}
</TableCell>
<TableCell>
{taskAction.actionId}
</TableCell>
<TableCell>{taskAction.templateStepId}</TableCell>
<TableCell>{taskAction.templateStepName}</TableCell>
<TableCell>100</TableCell>
<TableCell>{taskAction.startedAt}</TableCell>
<TableCell>{taskAction.endedAt}</TableCell>
<TableCell>{taskAction.status}</TableCell>
</Collapse>
</TableCell>
</TableRow>
);
}
})}
</>
);
}
export const MetricsTable = () => {
return (
<TableContainer component={Card}>
<Table aria-label="collapsible table">
<TableHead>
<TableRow>
<TableCell key={'headCol0'}>{" "}</TableCell>
<TableCell key={'headCol1'}>ID</TableCell>
<TableCell key={'headCol2'}>ACTION</TableCell>
<TableCell key={'headCol3'}>TITLE</TableCell>
<TableCell key={'headCol4'}>DESCRIPTION</TableCell>
<TableCell key={'headCol5'}>DURATION</TableCell>
<TableCell key={'headCol6'}>START</TableCell>
<TableCell key={'headCol7'}>END</TableCell>
<TableCell key={'headCol8'}>STATUS</TableCell>
</TableRow>
</TableHead>
<TableBody>
{jsonData.map((task) => (
<Task key={task.taskId} task={task} />
))}
</TableBody>
</Table>
</TableContainer>
);
};
I would appreciate the help with aligning the cells uniformly.

Cannot completely override top and bottom padding in MUI TableCell to make entire cell contents clickable

I am trying to make the entire contents of my table cells clickable (for routing to elsewhere in my app; the routing works fine).
I have succeeded in removing the unclickable horizontal space within the row between by explicitly declaring padding: 0 in my tableRow styling overrides (which already seems excessive).
And I have succeeded in removing all of the unclickable space within a cell in the case that the child styling is directly accessed and overridden, eg using '& .MuiTableCell-root:first-child'
But I still have unclickable space on the top and bottom of some of the cells when I do not directly access the child.
Why?
I have tried inspecting element to narrow down the root cause; it seemed that I was close and just had to add some "& .MuiTableCell-sizeSmall" specific styling. But even when I set padding: 0 in tableRow using "& .MuiTableCell-sizeSmall" there is no effect and the vertical space between rows remains unclickable.
//Styling
const useStyles = makeStyles(theme => ({
table: {
minWidth: 650,
position: 'relative',
fontSize: 10
},
largeIcon: {
width: 60,
height: 60
},
tableContainer: {
minHeight: 320
},
tableBodyContainer: {
minHeight: 265
},
tableHeadRow: {
'& .MuiTableCell-root': {
borderRight: `1px solid ${COLORS.WHITE}`,
borderBottom: `none`,
padding: '8px 5px 8px',
fontSize: 10,
cursor: 'pointer'
}
},
arrow: {
color: theme.palette.grey[500]
},
arrowActive: {
transform: 'rotate(-180deg)',
color: theme.palette.primary.main,
display: 'inline-block'
},
tableRow: {
'& .MuiTableCell-root': {
borderRight: `1px solid ${theme.palette.grey[200]}`,
borderBottom: 'none',
minWidth: 25,
padding: 0
},
'& .MuiTableCell-root:first-child': {
border: 'none',
padding: 0
},
'& .MuiTableCell-sizeSmall': {
padding: 0
}
},
selectedRow: {
backgroundColor: `${COLORS.SECONDARY} !important`,
'& .MuiTableCell-root': {
color: COLORS.WHITE
}
}
}));
//table code
return (
<div className={classes.tableContainer}>
<TableContainer className={classes.tableBodyContainer}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow className={classes.tableHeadRow}>
<TableCell />
{tableHeadElements.map(e => (
<TableCell key={e.key} align="center">
{e.label}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{folders?.items.map((folder: IFolderDTO, index: number) => {
const { id, name, updatedAt } = folder;
return (
<TableRow
className={classes.tableRow}
classes={{ selected: classes.selectedRow }}
selected={selectedRow === id}
onClick={() => setSelectedRow(id)}
key={index}
>
<TableCell align="center">
<Link to={APP_DASHBOARD_CHILD_FOLDER_CONTENTS_PATH(id)}>
<Box>
<IconButton color="default" size={'medium'}>
<FolderIcon fontSize="default" />
</IconButton>
</Box>
</Link>
</TableCell>
{[name, new Date(updatedAt)].map(cell => (
<TableCell key={index} align="center">
<Link to={APP_DASHBOARD_CHILD_FOLDER_CONTENTS_PATH(id)}>
<Box>{getCorrectFormat(cell)}</Box>
</Link>
</TableCell>
))}
<FolderActionsMenu
folderId={id}
onDeleteFolder={onDeleteFolder}
openDialogWithId={openDialogWithId}
/>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
<FolderFormDialog />
</div>
);
};
may you need to use A simple example of a dense table with no frills?
import * as React from 'react';
import Table from '#mui/material/Table';
import TableBody from '#mui/material/TableBody';
import TableCell from '#mui/material/TableCell';
import TableContainer from '#mui/material/TableContainer';
import TableHead from '#mui/material/TableHead';
import TableRow from '#mui/material/TableRow';
import Paper from '#mui/material/Paper';
function createData(name, calories, fat, carbs, protein) {
return { name, calories, fat, carbs, protein };
}
const rows = [
createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
createData('Eclair', 262, 16.0, 24, 6.0),
createData('Cupcake', 305, 3.7, 67, 4.3),
createData('Gingerbread', 356, 16.0, 49, 3.9),
];
export default function DenseTable() {
return (
<TableContainer component={Paper}>
<Table sx={{ minWidth: 650 }} size="small" aria-label="a dense table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow
key={row.name}
sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}

the drawer covers the page

my drawer is covering the page i want it to be flex to the left without covering any content. this is my first time using material-ui so if anyone can explain i would appreciate that
const drawerWidth = 240;
const SideBar = (props) => {
const useStyles = makeStyles((theme) => ({
root: { display: "flex" },
}));
const classes = useStyles();
return (
<div>
<Drawer
sx={{
width: drawerWidth,
flexShrink: 0,
"& .MuiDrawer-paper": {
width: drawerWidth,
boxSizing: "border-box",
},
}}
variant="permanent"
anchor="left"
>
<Toolbar />
<List>
<Divider />
{props.jobs.map((job, i) => (
<ListItem
alignItems="flex-start"
key={i}
onClick={() => {
props.onClick(job);
}}
>
<ListItemText primary={job.title} secondary={job.career_level} />
</ListItem>
))}
</List>
</Drawer>
</div>````
What you're probably looking for is the variant='persistent' drawer
import * as React from 'react';
import { styled, useTheme } from '#mui/material/styles';
import Box from '#mui/material/Box';
import Drawer from '#mui/material/Drawer';
import CssBaseline from '#mui/material/CssBaseline';
import MuiAppBar from '#mui/material/AppBar';
import Toolbar from '#mui/material/Toolbar';
import List from '#mui/material/List';
import Typography from '#mui/material/Typography';
import Divider from '#mui/material/Divider';
import IconButton from '#mui/material/IconButton';
import MenuIcon from '#mui/icons-material/Menu';
import ChevronLeftIcon from '#mui/icons-material/ChevronLeft';
import ChevronRightIcon from '#mui/icons-material/ChevronRight';
import ListItem from '#mui/material/ListItem';
import ListItemIcon from '#mui/material/ListItemIcon';
import ListItemText from '#mui/material/ListItemText';
import InboxIcon from '#mui/icons-material/MoveToInbox';
import MailIcon from '#mui/icons-material/Mail';
const drawerWidth = 240;
const Main = styled('main', { shouldForwardProp: (prop) => prop !== 'open' })(
({ theme, open }) => ({
flexGrow: 1,
padding: theme.spacing(3),
transition: theme.transitions.create('margin', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
marginLeft: `-${drawerWidth}px`,
...(open && {
transition: theme.transitions.create('margin', {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
marginLeft: 0,
}),
}),
);
const AppBar = styled(MuiAppBar, {
shouldForwardProp: (prop) => prop !== 'open',
})(({ theme, open }) => ({
transition: theme.transitions.create(['margin', 'width'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
...(open && {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: `${drawerWidth}px`,
transition: theme.transitions.create(['margin', 'width'], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen,
}),
}),
}));
const DrawerHeader = styled('div')(({ theme }) => ({
display: 'flex',
alignItems: 'center',
padding: theme.spacing(0, 1),
// necessary for content to be below app bar
...theme.mixins.toolbar,
justifyContent: 'flex-end',
}));
export default function PersistentDrawerLeft() {
const theme = useTheme();
const [open, setOpen] = React.useState(false);
const handleDrawerOpen = () => {
setOpen(true);
};
const handleDrawerClose = () => {
setOpen(false);
};
return (
<Box sx={{ display: 'flex' }}>
<CssBaseline />
<AppBar position="fixed" open={open}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={handleDrawerOpen}
edge="start"
sx={{ mr: 2, ...(open && { display: 'none' }) }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
Persistent drawer
</Typography>
</Toolbar>
</AppBar>
<Drawer
sx={{
width: drawerWidth,
flexShrink: 0,
'& .MuiDrawer-paper': {
width: drawerWidth,
boxSizing: 'border-box',
},
}}
variant="persistent"
anchor="left"
open={open}
>
<DrawerHeader>
<IconButton onClick={handleDrawerClose}>
{theme.direction === 'ltr' ? <ChevronLeftIcon /> : <ChevronRightIcon />}
</IconButton>
</DrawerHeader>
<Divider />
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</Drawer>
<Main open={open}>
<DrawerHeader />
<Typography paragraph>
Content
</Typography>
<Typography paragraph>
More content
</Typography>
</Main>
</Box>
);
}

How to hide the overflow content in material ui table cell instead of wrapping

I am using the material UI Table component to render a table. I want to show ... ellipsis when the content in a cell over flows. The current behavior is that it wraps text inside the cell. I tried adding CSS styles but to no avail. How do I achieve this?
import React from "react";
// import { makeStyles } from "#material-ui/core/styles";
import Table from "#material-ui/core/Table";
import TableBody from "#material-ui/core/TableBody";
import TableCell from "#material-ui/core/TableCell";
import TableContainer from "#material-ui/core/TableContainer";
import TableHead from "#material-ui/core/TableHead";
import TableRow from "#material-ui/core/TableRow";
import Paper from "#material-ui/core/Paper";
function createData(name, calories, fat, carbs, protein) {
return { name, calories, fat, carbs, protein };
}
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0),
createData(
"Ice cream sandwich dsdadsads dsadsadsadsa dsadasdsadsa dsadsadsa dsadsads asdsadsadsadsa",
237,
9.0,
37,
4.3
),
createData("Eclair", 262, 16.0, 24, 6.0),
createData("Cupcake", 305, 3.7, 67, 4.3),
createData("Gingerbread", 356, 16.0, 49, 3.9)
];
export default function SimpleTable() {
return (
<TableContainer component={Paper}>
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map(row => (
<TableRow key={row.name}>
<TableCell style={{root:{overflowX: 'hidden', textOverflow: "ellipsis"}}} className="overflow-test" component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
.overflow-test {
overflow-x: hidden !important;
text-overflow: ellipsis !important;
}
Here you go with a solution
import React from "react";
// import { makeStyles } from "#material-ui/core/styles";
import Table from "#material-ui/core/Table";
import TableBody from "#material-ui/core/TableBody";
import TableCell from "#material-ui/core/TableCell";
import TableContainer from "#material-ui/core/TableContainer";
import TableHead from "#material-ui/core/TableHead";
import TableRow from "#material-ui/core/TableRow";
import Paper from "#material-ui/core/Paper";
import Css from "./styles.css";
function createData(name, calories, fat, carbs, protein) {
return { name, calories, fat, carbs, protein };
}
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0),
createData(
"Ice cream sandwich dsdadsads dsadsadsadsa dsadasdsadsa dsadsadsa dsadsads asdsadsadsadsa",
237,
9.0,
37,
4.3
),
createData("Eclair", 262, 16.0, 24, 6.0),
createData("Cupcake", 305, 3.7, 67, 4.3),
createData("Gingerbread", 356, 16.0, 49, 3.9)
];
export default function SimpleTable() {
return (
<TableContainer component={Paper}>
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map(row => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
<div className={Css.textContainer}>
{row.name}
</div>
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
.textContainer {
display: block;
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
You need to specify a width to a container and then apply ellisis style to it.
Two notice points
You don't need root inside inline-styles, remove it
CSS for ...
style={{
whiteSpace: "nowrap",
textOverflow: "ellipsis",
width: "300px",
display: "block",
overflow: "hidden"
}}
Try it online:
Related QA: text-overflow: ellipsis not working
To keep the overflowing cell responsive, add table-layout: fixed; to the Table component. If needed TableCell can be given width: auto or a fixed width.

Resources