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;
̀
Related
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>
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>
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.
How do I reduce the height of the TableFooter component? I am trying to reduce the height of the TableFooter component in materials-UI but below 56px, the height of the footer does not get any smaller. There's no problem in increasing/making it bigger, however.
I've tried using the MUI Theme override based on other articles I've read here but they also do not work.
const theme = createMuiTheme({
overrides:{
MuiTableRow: {
root: { //for the body
height: "100%"
},
head: { //for the head
height: "100%"
},
footer: {
height: '30px',
minHeight: '20px',
backgroundColor: 'grey'
},
}
}
})
The code for my table based mostly on the Custom Pagination demo from the material-UI website aside from the codes that try to reduce the footer size.
<Paper className={classes.root}>
<div className={classes.tableWrapper}>
<Table className={classes.table} padding={"none"}>
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map(row => (
<TableRow key={row.id}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
</TableRow>
))}
{emptyRows > 0 && (
<TableRow style={{ height: 48 * emptyRows }}>
<TableCell colSpan={4} />
</TableRow>
)}
</TableBody>
<TableFooter className={classes.footer}>
<TableRow className={classes.footer}>
<TablePagination
rowsPerPageOptions={[]}
colSpan={3}
count={rows.length}
rowsPerPage={rowsPerPage}
page={page}
SelectProps={{
native: true,
}}
onChangePage={this.handleChangePage}
ActionsComponent={TablePaginationActionsWrapped}
style={{ padding: 0, margin: 0 }}
/>
</TableRow>
</TableFooter>
</Table>
</div>
</Paper>
https://codesandbox.io/s/moj46v62oy?fontsize=14
The current output where the size doesn't get any smaller than that. I was hoping to lessen the space between the top and bottom of the arrow.
You can use:
footer: {
"& > td > div": {
height: 30,
minHeight: 30
},
backgroundColor: "grey",
height: 30
}
This should become easier once v4 is out and global styles are being used.
Example: https://codesandbox.io/s/pmokwxxmvj
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>