Set Material UI react TableCell width dynamically - reactjs

How can I set the width of a TableCell in Material UI react? I tried this, but it doesn't work:
return(
<Paper className={classes.root}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell style={{width: '30%'}}>Vorname</TableCell>
<TableCell style={{width: '20%'}}>Nachname</TableCell>
<TableCell style={{width: '20%'}}>Soc. Vers. Nr.</TableCell>
<TableCell style={{width: '20%'}}>Geburtsdatum</TableCell>
<TableCell style={{width: '10%'}} colSpan={2}>Aktionen</TableCell>
</TableRow>
</TableHead>
<TableBody>
{delegates.map((delegateItem) => {
return(
<TableRow key={delegateItem.userData.id}>
<TableCell style={{width: '30%'}}>{delegateItem.userData.firstName}</TableCell>
<TableCell style={{width: '20%'}}>{delegateItem.userData.lastName}</TableCell>
<TableCell style={{width: '20%'}}>{delegateItem.userData.socSecNr}</TableCell>
<TableCell style={{width: '20%'}}>{delegateItem.userData.birthDateStr}</TableCell>
<TableCell style={{width: '5%'}}>
<Link to={`/delegateperms/${delegateItem.userData.id}`}>
<SettingsIcon/>
</Link>
</TableCell>
<TableCell style={{width: '5%'}}>
<Link to={`/delegatedelete/:${delegateItem.userData.id}`}>
<DeleteForeverIcon/>
</Link>
</TableCell>
</TableRow>
)
})}
</TableBody>
</Table>
</Paper>
)
All columns have the same width. I would like to have the width dynamically. How can I make this?

This works for me with the version "#material-ui/core": "4.9.0"
<TableContainer className={classes.container}>
<Table className={classes.table} stickyHeader size="small">
<TableHead>
<TableRow>
<TableCell width="30%">User Name</TableCell>
<TableCell width="20%">Designation</TableCell>
<TableCell width="10%">Nid</TableCell>
<TableCell width="20%">Email</TableCell>
<TableCell width="10%">Mobile</TableCell>
<TableCell width="10%">Gender</TableCell>
</TableRow>
</TableHead>
<TableBody>
{props.isGridLoading && (
<TableRow>
<TableCell colSpan={6}>
<LoadingGridSpinner open={props.isGridLoading} />
</TableCell>
</TableRow>
)}
{props.profileObj.lists &&
props.profileObj.lists.map(row => (
<TableRow key={row.userProfileId} hover={true}>
<TableCell width="30%">
{row.userName}
</TableCell>
<TableCell width="20%">{row.designation}</TableCell>
<TableCell width="10%">{row.nid}</TableCell>
<TableCell width="20%">{row.email}</TableCell>
<TableCell width="10%">{row.mobile}</TableCell>
<TableCell width="10%">{row.gender}</TableCell>
</TableRow>
))}
</TableBody>
</Table>

There's a Github thread here where they talk about this. If you have a look at the last comment at the bottom there's a code sample of making a table where you assign widths to certain cells using a class and the rest span equally. So I assume you could assign percents to all like you want, and the comment says percentages work as well as fixed pixel amounts.

Set width to each TableCell like this:
<TableCell style={{width: '20%'}}>Geburtsdatum</TableCell>

This works for me with "#material-ui/core": "4.8.3":
<Table style={{ width: "auto", tableLayout: "auto" }}>
<TableBody>
<TableRow>
<TableCell component="th" scope="row" width={150}>lorem 1</TableCell>
<TableCell component="th" scope="row" width={100}>lorem 2</TableCell>
<TableCell component="th" scope="row">lorem 3</TableCell>
</TableRow>
</TableBody>
</Table>

Related

How to create dynamic material ui table in ReactJs

I'm using React to display client information. Currently I have hardcoded the table header and table cell's values. My indented result is
I have an array like ClientAddress =[{addressLine1Desc : "1 Apple Park Way", addressLine2Desc: null, ....}]. I want the first two be in a row and next two items are in second row and so on...
I have harcoded like this
<Card sx={{ minWidth: 275 }}>
<CardContent>
<Typography variant="h5" component="div" className="cardTitle">
<MailOutlineIcon /> Primary Address
</Typography>
<TableContainer component={Paper} sx={{ width: 400, margin: 'auto', display: 'Block', boxShadow: 'none' }}>
<Table sx={{ minWidth: 400 }} aria-label="customized table">
<TableBody>
<TableRow className='tableRow'>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">Address Line1</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].addressLine1Desc}</span> : <span className='cellValue'> </span>}
</TableCell>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">Address Line2</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].addressLine2Desc}</span> : <span className='cellValue'> </span>}
</TableCell>
</TableRow>
<TableRow className='tableRow'>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">Address Line3</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].addressLine3Desc}</span> : <span className='cellValue'> </span>}
</TableCell>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">Address Line4</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].addressLine4Desc}</span> : <span className='cellValue'> </span>}
</TableCell>
</TableRow>
<TableRow className='tableRow'>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">Country Code</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].countryCode}</span> : <span className='cellValue'> </span>}
</TableCell>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">Country Name</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].countryCode}</span> : <span className='cellValue'> </span>}
</TableCell>
</TableRow>
<TableRow className='tableRow'>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">CityName</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].cityName}</span> : <span className='cellValue'> </span>}
</TableCell>
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">PostalCode</span><br />{clientAddressDetails.length ? <span className='cellValue'>{clientAddressDetails[0].postalCode}</span> : <span className='cellValue'> </span>}
</TableCell>
</TableRow>
</TableBody>
</Table>
</TableContainer>
</CardContent>
</Card>
I want to display like the above image dynamically instead of having repeated code. I need to do this for displaying other information in this format
Try this in your code for map function
<Card sx={{ minWidth: 275 }}>
<CardContent>
<Typography variant="h5" component="div" className="cardTitle">
<MailOutlineIcon /> Primary Address
</Typography>
<TableContainer component={Paper} sx={{ width: 400, margin: 'auto', display: 'Block', boxShadow: 'none' }}>
<Table sx={{ minWidth: 400 }} aria-label="customized table">
<TableBody>
<TableRow className='tableRow'>
{clientAddressDetails.length>0 && Object.keys(clientAddressDetails[0]).map(key=>(
<TableCell component="th" scope="row" className="tableCell">
<span className="cellHeader">Address Line1</span><br />{clientAddressDetails[0][key] && <span className='cellValue'>{clientAddressDetails[0][key]}</span>}
</TableCell>
))}
</TableRow>
</TableBody>
</Table>
</TableContainer>
</CardContent>
</Card>
Loop through the above DOM with help for your array data.

how can I add row and columns in table heading in react material UI

I am new to material UI and I am struggling to add columns in the table heading, I have provided an image I am trying to do like this, under the Economics there will be 3 columns also each column will take two more columns.
Here is my code:
<Table sx={{ minWidth: 700 }} aria-label="customized table">
<TableHead>
<TableRow>
<StyledTableCell align="left" scope="col">
Economics
<StyledTableCell align="left" scope="col">
1st
<StyledTableCell align="left">CQ</StyledTableCell>
<StyledTableCell align="left">MCQ</StyledTableCell>
</StyledTableCell>
<StyledTableCell align="left">
2nd
<StyledTableCell align="left">CQ</StyledTableCell>
<StyledTableCell align="left">MCQ</StyledTableCell>
</StyledTableCell>
<StyledTableCell align="left">
AVG
<StyledTableCell align="left">%</StyledTableCell>
<StyledTableCell align="left">GP</StyledTableCell>
</StyledTableCell>
</StyledTableCell>
</TableRow>
</TableHead>
The above code UI is like the below screenshot, it's too big and not aligned
I have used the scope="col" attribute inside the StyledTableCell but it's not working, is there anything that I am missing?
Thank you.
try adding this colSpan attribute
<StyledTableCell colSpan={6}>
you can check it here. in the spanning section
https://mui.com/material-ui/react-table/
your code will look like this
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
<TableCell align="center" colSpan={6}>
Economics
</TableCell>
</TableRow>
<TableRow>
<TableCell align="center" colSpan={2}>
1st
</TableCell>
<TableCell align="center" colSpan={2}>
2nd
</TableCell>
<TableCell align="center" colSpan={2}>
AVG
</TableCell>
</TableRow>
<TableRow>
<TableCell align="center">CQ</TableCell>
<TableCell align="center">MCQ</TableCell>
<TableCell align="center">CQ</TableCell>
<TableCell align="center">MCQ</TableCell>
<TableCell align="center">%</TableCell>
<TableCell align="center">GP</TableCell>
</TableRow>
</TableHead>
</Table>

How to stick the last column and add scrolling effect?

I want to stick last column and add scrolling effect in react mui table.
I want last column to stick and previous 2 3 column will be scrollable.
I have used material ui table and react js for that.
This is my mui table code:
<TableContainer
sx={{
mt: "32px",
height: "383px",
overflowX: "scroll",
}}
component={Paper}
>
<Table stickyHeader aria-label="caption table">
<TableHead>
<TableRow>
<TableCell className={classes.tableHead}>Merchant name</TableCell>
<TableCell className={classes.tableHead}>Merchant ID</TableCell>
<TableCell className={classes.tableHead}>
Transaction ID
</TableCell>
<TableCell className={classes.tableHead}>PG ID</TableCell>
<TableCell className={classes.tableHead}>Order No</TableCell>
<TableCell className={classes.tableHead}>Amount</TableCell>
<TableCell className={classes.tableHead}>
Transaction Fee
</TableCell>
<TableCell className={classes.tableHead}>
Aggregator Fee
</TableCell>
<TableCell className={classes.tableHead}>Reseller fee </TableCell>
<TableCell className={classes.tableHead}>Paymode </TableCell>
<TableCell className={classes.tableHead}>
<Grid
container
direction={"row"}
justifyContent={"space-between"}
>
Transaction date
<Image
className={classes.filter}
src={Filer}
alt={"filterIcon"}
/>
</Grid>
</TableCell>
<TableCell
sx={{ position: "sticky", right: 0, background: "white" }}
className={classes.tableHead}
>
Status
</TableCell>
</TableRow>{" "}
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow key={row.Merchantname} onClick={toggleModalOpen}>
<TableCell component="th" scope="row">
{row.Merchantname}
</TableCell>
<TableCell component="th" scope="row">
{row.MerchantID}
</TableCell>
<TableCell>{row.TransactionID}</TableCell>
<TableCell>{row.PGID}</TableCell>
<TableCell>{row.OrderNo}</TableCell>
<TableCell>{row.Amount}</TableCell>
<TableCell>{row.TransactionFee}</TableCell>
<TableCell>{row.AggregatorFee}</TableCell>
<TableCell>{row.ResellerFee}</TableCell>
<TableCell>{row.Paymode}</TableCell>
<TableCell>{row.Transactiondate}</TableCell>
<TableCell
sx={{ position: "sticky", right: "0", background: "white" }}
>
{row.Status}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>

How can I change a color in a material ui sticky header table?

Without stickyHeader I can change its color, but when I put stickyHeader the color becomes its default as white.
<Table aria-label="customized table" >
<TableHead stickyHeader style={{backgroundColor: "black" }}>
Make sure you actually have a row with at least one cell. Otherwise the TableHead has nothing to render
// ...
<TableHead stickyHeader style={{ backgroundColor: 'blue' }}>
<TableRow>
<TableCell>Cell Content</TableCell>
// More cells...
</TableRow>
</TableHead>
// ...
As shubham Baranwall said,specify the backgroundColor style at not but as follows,
<TableHead stickyHeader={true} >
<TableCell style={{backgroundColor: "black"}}> HeadColumn1 <TableCell>
<TableCell style={{backgroundColor: "black"}}> HeadColumn2 <TableCell>
<TableCell style={{backgroundColor: "black"}}> HeadColumn3 <TableCell>
</TableHead>

Scroll Material UI table body vertically

I am using Material UI table for my React.js application. I want to scroll its body vertically if table is too long. Here is the table.
<Table style={TableBorder}>
<colgroup>
<col style={{width: '10%'}}/>
<col style={{width: '30%'}}/>
<col style={{width: '10%'}}/>
<col style={{width: '10%'}}/>
<col style={{width: '10%'}}/>
<col style={{width: '10%'}}/>
<col style={{width: '10%'}}/>
<col style={{width: '10%'}}/>
</colgroup>
<TableHead>
<TableRow>
<TableCell align="center"
style={styles.table.tableHead.tableCell}>PRODUCT</TableCell>
<TableCell align="center"
style={styles.table.tableHead.tableCell}>BUILDS</TableCell>
<TableCell align="right"
style={styles.table.tableHead.tableCell}>INSTRUCTIONS(%)</TableCell>
<TableCell align="right"
style={styles.table.tableHead.tableCell}>BRANCHES(%)</TableCell>
<TableCell align="right"
style={styles.table.tableHead.tableCell}>COMPLEXITY(%)</TableCell>
<TableCell align="right"
style={styles.table.tableHead.tableCell}>LINES(%)</TableCell>
<TableCell align="right"
style={styles.table.tableHead.tableCell}>METHODS(%)</TableCell>
<TableCell align="right"
style={styles.table.tableHead.tableCell}>CLASSES(%)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{this.state.tableSummary.map((row, index) => (
<TableRow key={index}
style={((row[1] > 0) ? styles.table.tableBody.tableCell.cursorPointer : styles.table.tableBody.tableCell.cursorText)}
hover>
<TableCell component="th" scope="row"
align="left">{row.name}</TableCell>
<TableCell style={builds}
align="left">{formatBuildString(row.build_no)}</TableCell>
<TableCell
align="right">{getPercentage(row.data.totalIns, row.data.missIns)}</TableCell>
<TableCell
align="right">{getPercentage(row.data.totalBranches, row.data.missBranches)}</TableCell>
<TableCell
align="right">{getPercentage(row.data.totalCxty, row.data.missCxty)}</TableCell>
<TableCell
align="right">{getPercentage(row.data.totalLines, row.data.missLines)}</TableCell>
<TableCell
align="right">{getPercentage(row.data.totalMethods, row.data.missMethods)}</TableCell>
<TableCell
align="right">{getPercentage(row.data.totalClasses, row.data.missClasses)}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
I used below styles to scroll table body vertically.
thead, tbody { display: block; }
tbody {
height: 100px; /* Just for the demo */
overflow-y: auto; /* Trigger vertical scroll */
overflow-x: hidden; /* Hide the horizontal scroll */
}
But when I use it my table is not aligned well.
You should give style to table in this way.
<Table className="scroll">
....
</Table>
*.css file
.scroll {
height: 100px;
overflow-y: auto;
overflow-x: hidden;
}
Or if you customize the table using styled components you can do in this way:
const ScrollYTable = styled(Table)̀
height: 100px;
overflow-y: auto;
overflow-x: hidden;
̀

Resources