How to adjust height of material UI header with styles - javascript

How can you style the header of a MUI table like height and width?
<TableHead >
<TableRow >
<TableCell hover>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>
I tried passing
sx={{ height:'20px' }}
but the height is fixed

You can achieve that by using sx on the TableHead component:
<TableHead sx={{height:"150px"}}>
<TableRow >
<TableCell hover>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>

To customize the styling of the TableHead you need to target the MuiTableCell-head class of the Table.
One important thing to notice, Mui TableHead height is not coming from the height property, It is coming from line-height. Please check the example below:
import { Table, TableHead, TableBody } from '#mui/material'
<Table sx={{ '& .MuiTableCell-head':{ lineHeight: 20, color: 'red' } }}>
<TableHead>
</TableHead>
<TableBody>
</TableBody>
</Table>

Related

row inside a cell

I have the following simple code that generates the location of various records in a table.
return (
<TableRow>
<TableCell >
// some code_1
</TableCell>
<TableCell >
// some code_2
</TableCell>
<TableCell>
<TableRow> // here I get warning
<TableCell>
// some code_3
</TableCell>
</TableRow>
<TableRow>
<TableCell >
// some code_4
</TableCell>
</TableRow>
<TableRow>
<TableCell >
// some code_5
</TableCell>
</TableRow>
</TableCell>
<TableCell
// some code_6
</TableCell>
</TableRow >
);
And as you can guess, when I add more components to the TableCell (in my case I add TableRow ), I get a warning:
Warning: validateDOMNesting(...): tr cannot appear as a child of td.
Tell me how to save this table structure, get rid of warnings
Copied from my comment above:
You can put table rows inside your cells but not directly. They need to be wrapped in their own seperate inner table element. In the rendered html it will look something like ... > td > table > tbody > tr > td > ...
In this examble I have wrapped your table rows in tables with table body elements, but you may want to wrap the top row in a table head element, <TableHead> instead and subsequently the rest in a table body.
Table head and body may not be required so you can try to place the rows directly inside the table element.
Here is the documentation from Material UI https://mui.com/material-ui/react-table/
return (
<TableRow>
<TableCell>
// some code_1
</TableCell>
<TableCell>
// some code_2
</TableCell>
<TableCell>
<Table>
<TableBody>
<TableRow>
<TableCell>
// some code_3
</TableCell>
</TableRow>
<TableRow>
<TableCell>
// some code_4
</TableCell>
</TableRow>
<TableRow>
<TableCell>
// some code_5
</TableCell>
</TableRow>
</TableBody>
</Table>
</TableCell>
<TableCell>
// some code_6
</TableCell>
</TableRow>
);

(MUI) Two table on the same row

I am trying to create two parallel tables that stick to each other on the same row. However, the second table is always stacked under the first table. The only idea I could think of is using Grid Item to determine the column span of each table, but I have no idea how to implement it.
Can you use Grid Container over the TableContainer?
const TableCellStyle = styled(TableCell)(({ theme }) => ({
[`&.${tableCellClasses.head}`]: {
color: theme.palette.text.dark,
backgroundColor: theme.palette.success.light,
},
[`&.${tableCellClasses.body}`]: {
fontSize: 12,
}
}))
const TableRowStyle = styled(TableRow)(({ theme }) => ({
'&:nth-of-type(odd)': {
backgroundColor: theme.palette.action.hover,
},
'&:last-child td, &:last-child th': {
border: 0,
}
}))
function DataTable() {
return (
//first table
<TableContainer
component={Paper}
sx={{
width:'max-content',
marginLeft: 'auto',
marginRight: 'auto'
}}
>
<Table sx={{ minWidth: 400 }}>
<TableHead>
<TableRow>
<TableCellStyle>Position</TableCellStyle>
<TableCellStyle align="right">Defect Type by AI</TableCellStyle>
<TableCellStyle align="right">Tool Decision by AI</TableCellStyle>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRowStyle key={row.name}>
<TableCellStyle component="th" scope="row">
{row.name}
</TableCellStyle>
<TableCellStyle align="right">{row.Column1}</TableCellStyle>
<TableCellStyle align="right">{row.Column2}</TableCellStyle>
</TableRowStyle>
))}
</TableBody>
//second table
</Table>
<Table sx={{ minWidth: 400 }}>
<TableHead>
<TableRow>
<TableCellStyle>Position</TableCellStyle>
<TableCellStyle align="right">Defect Type by AI</TableCellStyle>
<TableCellStyle align="right">Tool Decision by AI</TableCellStyle>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRowStyle key={row.name}>
<TableCellStyle component="th" scope="row">
{row.name}
</TableCellStyle>
<TableCellStyle align="right">{row.Column1}</TableCellStyle>
<TableCellStyle align="right">{row.Column2}</TableCellStyle>
</TableRowStyle>
))}
</TableBody>
</Table>
</TableContainer>
);
}

display an full-width row in material-ui table react

I want to display some data in a table this is my JSX code:
const StudentsTable = (props) => {
const classes = useStyles();
return (
<TableContainer component={Paper}>
<Table className={classes.table} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>NR.</TableCell>
<TableCell align="right">NUME</TableCell>
<TableCell align="right">E-MAIL</TableCell>
<TableCell align="right">NR.TELEFON</TableCell>
<TableCell align="right">CLASA</TableCell>
<TableCell align="right">ACTIUNI</TableCell>
</TableRow>
</TableHead>
<TableBody>
{props.students.length > 0 ? (
props.students.map((student, i) => (
<TableRow key={student.studentName}>
<TableCell component="th" scope="row">
{i + 1}
</TableCell>
<TableCell align="right">{student.studentName}</TableCell>
<TableCell align="right">{student.studentEmail}</TableCell>
<TableCell align="right">{student.studentPhone}</TableCell>
<TableCell align="right">{student.clasa}</TableCell>
<TableCell align="center" className={classes.actionsCell}>
<BootstrapButton variant="primary" size="sm">
EDITARE
</BootstrapButton>
<BootstrapButton
variant="danger"
size="sm"
onClick={() => props.deleteStudent(student.studentName)}
>
STERGERE
</BootstrapButton>
</TableCell>
</TableRow>
))
) : (
<Typography variant="h5">
NU EXISTA ELEVI ADAUGATI.{"\n"} PUTETI ADAUGA APASAND PE BUTONUL
"ADAUGA ELEVI"
</Typography>
)}
</TableBody>
</Table>
</TableContainer>
);
};
when the props.students it's an empty array I want to return a single full-width row with this message: NU EXISTA ELEVI ADAUGATI.{"\n"} PUTETI ADAUGA APASAND PE BUTONUL "ADAUGA ELEVI" but in my app, the table looks something like this:
how to fix it?
In the place where you want to render a full row
use
<TableRow><TableCell colSpan={6}>"your message"</TableCell></TableRow>

Customize which TableRows appear on which page using TablePagination (Material-ui)

I want to be able to set whatever table row I want to a specific page. For example, If I have three TableRows and want the first two on page one in my DataTable and the third one on page two. I'm new to programming so the more thorough the explanation the better. Here is a quick peek at my code.
I have the rows in the TableBody inside the TableContainer:
<TableRow className="property_taxes" hover>
<TableCell>Property Taxes</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
<TableRow className="maintenance" hover>
<TableCell>Maintenance</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
<TableRow className="total_costs" hover>
<TableCell>Total</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
And then I have the TablePagination right after that:
<TablePagination
component="div"
count={7}
rowsPerPage={rowsPerPage}
rowsPerPageOptions={[]}
page={page}
onChangePage={handleChangePage}
/>
Thank you in advance!
export default function DataTable() {
const classes = useStyles();
const [page, setPage] = React.useState(0);
const handleChangePage = (event, newPage) => {
setPage(newPage)
}
return (
<div>
<TableContainer component={Paper} className={classes.container}>
<Table stickyHeader className={classes.table} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Expenses</TableCell>
<TableCell align="right">Monthly ($)</TableCell>
<TableCell align="right">Yearly ($)</TableCell>
<TableCell align="right">Total ($)</TableCell>
</TableRow>
</TableHead>
<TableBody className={classes.tableBody}>
<TableRow className="principle" hover>
<TableCell id="name">Principle</TableCell>
<TableCell id="monthly"align="right">0</TableCell>
<TableCell id="yearly"align="right">0</TableCell>
<TableCell id="total"align="right">0</TableCell>
</TableRow>
<TableRow className="private_mortgage_insurance" hover>
<TableCell>Private Mortgage Insurance</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
<TableRow className="interest" hover>
<TableCell>Interest</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
<TableRow className="property_taxes" hover>
<TableCell>Property Taxes</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
<TableRow className="maintenance" hover>
<TableCell>Maintenance</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
<TableRow className="total_costs" hover>
<TableCell>Total</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
<TableRow className="closing_costs" hover>
<TableCell>Closing Costs</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
<TableCell align="right">0</TableCell>
</TableRow>
</TableBody>
</Table>
</TableContainer>
<TablePagination
component="div"
count={7}
rowsPerPage={6}
rowsPerPageOptions={[]}
page={page}
onChangePage={handleChangePage}
/>
</div>
);
}

How to merge React TableCells inside a map function?

So I am trying to create a table with material-ui where rows of w1 and w2 are merged vertically, but I can't seem to find a solution. Using rowSpan when hardcoding table manually works just fine, but when I use it inside map function it creates 4 columns with merged rows of the same element. I need to map data because there is going to be lots of rows in that table and hardcoding all rows is not reasonable.
Any type of suggestion to resolve this?
<TableBody>
{data.data.map((row, i) => (
<TableRow key={row.i}>
<TableCell className={classes.fontEditable} component="th" scope="row">
{row.x1}
</TableCell>
<TableCell className={classes.fontEditable} align="right">
{row.x2}
</TableCell>
<TableCell rowSpan={4} className={classes.fontEditable} align="right">
{row.w1}
</TableCell>
<TableCell rowSpan={4} className={classes.fontEditable} align="right">
{row.w2}
</TableCell>
<TableCell className={classes.font} align="right">
{row.t}
</TableCell>
</TableRow>
))}
</TableBody>
Expected outcome
Actual outcome
problem:
it creates the w1 and w2 cell in each row because of the map function
solution:
you can do an if condition for the w1 and w2 print for the first only as the following
<TableBody>
{data.data.map((row, i) => (
<TableRow key={row.i}>
<TableCell className={classes.fontEditable} component="th" scope="row">
{row.x1}
</TableCell>
<TableCell className={classes.fontEditable} align="right">
{row.x2}
</TableCell>
{i === 0?
<TableCell rowSpan={4} className={classes.fontEditable} align="right">
{row.w1}
</TableCell>
<TableCell rowSpan={4} className={classes.fontEditable} align="right">
{row.w2}
</TableCell>
:null}
<TableCell className={classes.font} align="right">
{row.t}
</TableCell>
</TableRow>
))}

Categories