How to create dynamic material ui table in ReactJs - 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.

Related

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>

How to align table pagination internal components (captions,Actions)

I was able to center the 'entire' table pagination but I couldn't align the internal components of tablePagination from MaterialUI separately with the code below.I would like to center the Pagination caption and 'right' action components.
Pagination: {
display:'flex',
justifyContent: 'center',
width: "100%",
alignItems: 'left',
padding:'0px',
},
PaginationCaption: {
color: 'white',
fontSize: '1.8rem',
flexShrink: 0,
// width: '50%',
textAlign: 'center',
},
PaginationSelectIcon: {
color: 'white',
fontSize: '1.8rem'
},
PaginationSelect: {
color: 'white',
fontSize: '1.8rem'
},
PaginationActions: {
color: 'white',
fontSize: '1.8rem',
// width: '50%',
textAlign: 'right',
}
<Grid container
alignItems="center"
justify="center"
>
<Grid item xs={12} style={{height:'38vh'}}>
<div className={classes.tableWrapper}>
<Table className={classes.table}>
<TableHead style={{backgroundColor:'#4e4e4e'}}>
<TableRow >
<TableCell component="th" scope="row" className=
{classes.tablecell} style={{width: '20%'}}> License Plate</TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> Status</TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> IN </TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> OUT </TableCell>
<TableCell align="right" className={classes.tablecell}
style={{width: '20%'}}> Time on-site</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.slice(page * rowsPerPage, page * rowsPerPage +
rowsPerPage).map(row => (
<TableRow key={row.name} >
<TableCell component="th" scope="row" className=
{classes.tablecell}>
{row.name}
</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.calories}</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.fat}</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.calories}</TableCell>
<TableCell align="right" className={classes.tablecell}>
{row.fat}</TableCell>
</TableRow>
))}
{emptyRows > 0 && (
<TableRow style={{ height: 48 * emptyRows }}>
<TableCell colSpan={6} />
</TableRow>
)}
</TableBody>
</Table>
</div>
<TablePagination
component="div"
className={classes.Pagination}
classes={{
// root: classes.Pagination,
caption: classes.PaginationCaption,
selectIcon: classes.PaginationSelectIcon,
select: classes.PaginationSelect,
actions: classes.PaginationActions,
}}
// rowsPerPageOptions={[5, 7, 10]}
colSpan={5} // Important!!!!
count={rows.length}
rowsPerPageOptions={[]} //added
rowsPerPage={rowsPerPage}
page={page}
SelectProps={{
inputProps: { 'aria-label': 'Rows per page' },
native: true,
}}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
ActionsComponent={TablePaginationActions}
/>
</Grid>
</Grid>
I've also have looked into the source code of tablePagination and tried the css styles there as well. But the results are the same.

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;
̀

Set Material UI react TableCell width dynamically

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>

Resources