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.
Related
import * as React from "react";
import {
Select,
MenuItem,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Paper
} from "#mui/material";
import KeyboardArrowDownIcon from "#mui/icons-material/KeyboardArrowDown";
function createData(name, mobile, access) {
return { name, mobile, access };
}
const rows = [
createData("Sam", 9865745159),
createData("Amily", 8723879237),
createData("Eva", 9432671262),
createData("Jack", 7898083305),
createData("Diana", 8973667356)
];
export default function DenseTable() {
const [access, setAccess] = React.useState(1);
const handleChange = (event, index, data) => {
setAccess(event.target.value);
};
return (
<TableContainer component={Paper}>
<Table sx={{ minWidth: 650 }} size="small" aria-label="a dense table">
<TableHead>
<TableRow>
<TableCell align="center">Name</TableCell>
<TableCell align="center">Mobile</TableCell>
<TableCell align="center">Access</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" align="center">
{row.name}
</TableCell>
<TableCell align="center">{row.mobile}</TableCell>
<TableCell align="center">
<Select
value={access}
onChange={handleChange}
MenuProps={{
MenuListProps: { disablePadding: true }
}}
fullWidth
size="small"
IconComponent={() => (
<KeyboardArrowDownIcon
sx={{
position: "absolute",
right: 10,
width: "20px",
pointerEvents: "none"
}}
/>
)}
sx={{
fontSize: "14px",
width: "100px",
height: "28px"
}}
>
<MenuItem
value={1}
sx={{
fontSize: "14px",
height: "25px",
width: "100%"
}}
>
Allow
</MenuItem>
<MenuItem
value={2}
sx={{
fontSize: "14px",
height: "30px",
width: "100%"
}}
>
Decline
</MenuItem>
</Select>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
I am using ReactJs with Material UI table with dropdown in each row of the table cell. Whenever I am changing the dropdown option of one row then automatically changes to same option in dropdown of all rows.I have to handle each row separately. How to change dropdown in each row of mui table cell in a loop? file.
Move the dropdown to a new jsx component and manage the state there
function dropdownComponent (){
const [access, setAccess] = React.useState(1);
const handleChange = (event, index, data) => {
setAccess(event.target.value);
};
return
(<Select
value={access}
onChange={handleChange}
MenuProps={{
MenuListProps: { disablePadding: true }
}}
fullWidth
size="small"
IconComponent={() => (
<KeyboardArrowDownIcon
sx={{
position: "absolute",
right: 10,
width: "20px",
pointerEvents: "none"
}}
/>
)}
sx={{
fontSize: "14px",
width: "100px",
height: "28px"
}}
>
<MenuItem
value={1}
sx={{
fontSize: "14px",
height: "25px",
width: "100%"
}}
>
Allow
</MenuItem>
<MenuItem
value={2}
sx={{
fontSize: "14px",
height: "30px",
width: "100%"
}}
>
Decline
</MenuItem>
</Select>
)
}
Call it like this
<TableCell align="center">
<dropdownComponent />
I am using mui table. It is not responsive. I need to set the width in %.
The output is
I attach the screenshort from two different screen
Create a div container with overflow: auto, then another container
with: width: '100%', display: 'table', tableLayout: 'fixed'
And that's all.
<div className="App">
<Box sx={{ overflow: "auto" }}>
<Box sx={{ width: "100%", display: "table", tableLayout: "fixed" }}>
<Table>
<TableHead>
<TableRow>
<TableCell>
<TableSortLabel>Name</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel>User</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel>Title</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel>Role</TableSortLabel>
</TableCell>
<TableCell align={"right"}></TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow hover>
<TableCell>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Avatar
sx={{
height: 42,
width: 42,
backgroundColor: "primary.main"
}}
>
MD
</Avatar>
<Box sx={{ ml: 1 }}>
<Link
color={"inherit"}
variant={"subtitle2"}
component={"a"}
sx={{ cursor: "pointer" }}
>
Darwling Jackson
</Link>
</Box>
</Box>
</TableCell>
<TableCell>DJackson</TableCell>
<TableCell>Tester</TableCell>
<TableCell>Editor</TableCell>
</TableRow>
<TableRow hover>
<TableCell>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Avatar
sx={{
height: 42,
width: 42,
backgroundColor: "primary.main"
}}
>
MD
</Avatar>
<Box sx={{ ml: 1 }}>
<Link
color={"inherit"}
variant={"subtitle2"}
sx={{ cursor: "pointer" }}
>
Jhon Phoneix
</Link>
</Box>
</Box>
</TableCell>
<TableCell>JP</TableCell>
<TableCell>Database Administrator</TableCell>
<TableCell>Editor</TableCell>
</TableRow>
<TableRow hover>
<TableCell>
<Box sx={{ display: "flex", alignItems: "center" }}>
<Avatar
sx={{
height: 42,
width: 42,
backgroundColor: "primary.main"
}}
>
MD
</Avatar>
<Box sx={{ ml: 1 }}>
<Link
color={"inherit"}
variant={"subtitle2"}
sx={{ cursor: "pointer" }}
>
Maria Dominguez
</Link>
</Box>
</Box>
</TableCell>
<TableCell>MDominguez</TableCell>
<TableCell>IT Manager</TableCell>
<TableCell>Editor</TableCell>
</TableRow>
</TableBody>
</Table>
</Box>
</Box>
</div>
See my example: Mui responsive code sandbox
Can't really demonstrate without code, but if you don't want to let Material UI to determine the width, a common strategy is to wrap the component in a Box, and give the width (in percent) to the Box instead. Reference.
I am trying to change the divider color in my TableBody, and also the background color of the Rows per page dropdown menu. Any help is appreciated, thank you!
I am using styled-components ThemeProvider to toggle the theme in my app, so I am passing this hook as a prop for MUI components and adjusting their colors with this method:
props.theme === 'light' ? '#fff' : '#2a2a2a'
<Box sx={{ width: '99.5%' }}>
<Paper sx={{ width: '100%', mb: 2 }}>
<EnhancedTableToolbar theme={props.theme} numSelected={selected.length} />
<TableContainer>
<Table
sx={{ minWidth: 750, background: props.theme === 'light' ? '#fff' : '#2a2a2a' }}
aria-labelledby="tableTitle"
size={dense ? 'small' : 'medium'}
>
<EnhancedTableHead
sx={{ background: props.theme === 'light' ? '#fff' : '#2a2a2a' }}
numSelected={selected.length}
order={order}
orderBy={orderBy}
onSelectAllClick={handleSelectAllClick}
onRequestSort={handleRequestSort}
rowCount={rows.length}
/>
<TableBody>
{/* if you don't need to support IE11, you can replace the `stableSort` call with:
rows.slice().sort(getComparator(order, orderBy)) */}
{stableSort(rows, getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => {
const isItemSelected = isSelected(row.macid);
const labelId = `enhanced-table-checkbox-${index}`;
return (
<TableRow
hover
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={row.macid}
selected={isItemSelected}
>
<TableCell
component="th"
id={labelId}
scope="row"
>
{row.macid}
</TableCell>
<TableCell align="right">{row.cpu}</TableCell>
<TableCell align="right">{row.ram}</TableCell>
<TableCell align="right">{row.disk}</TableCell>
<TableCell align="right">{row.temperature}</TableCell>
</TableRow>
);
})}
{emptyRows > 0 && (
<TableRow
style={{
height: (dense ? 33 : 53) * emptyRows,
}}
>
<TableCell colSpan={6} />
</TableRow>
)}
</TableBody>
</Table>
</TableContainer>
<TablePagination
sx={{background: props.theme === 'light' ? '#fff' : '#2a2a2a'}}
rowsPerPageOptions={[5, 10, 25]}
component="div"
count={rows.length}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</Paper>
<FormControlLabel
control={<Switch checked={dense} onChange={handleChangeDense} />}
label="Dense padding"
/>
</Box>
Have you tried props.theme.background?
The problem I have is I try to center out the fixed-sized table in the middle of the page (in a Paper component) and fail miserably. I am also quite new in Material-ui, so I am not sure this is a proper way for structuring this kind of items. Can you please help me centering it out in the middle of the page?
import rows from "./mockdata/mock_dashboard";
const rowsData = rows;
const useStyles = makeStyles(theme => ({
root: {
width: "100%"
},
paper: {
marginTop: theme.spacing(3),
width: "100%",
overflowX: "auto",
marginBottom: theme.spacing(2),
margin: "auto"
},
table: {
minWidth: 650,
maxWidth: 1200
}
}));
export default function MainDashboard() {
const classes = useStyles();
return (
<div className={classes.root}>
<Paper className={classes.paper}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<TableCell>Nz.</TableCell>
<TableCell>Data Przyjęcia</TableCell>
<TableCell>Koordynator</TableCell>
<TableCell>Link do Pliku</TableCell>
<TableCell>Dod. opis</TableCell>
<TableCell>Data Wykonania</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rowsData.map(row => (
<TableRow key={row.orderNo}>
<TableCell component="th" scope="row">
{row.orderNo}
</TableCell>
<TableCell align="center">{row.orderDate}</TableCell>
<TableCell align="center">{row.coordinator}</TableCell>
<TableCell align="center">{row.link}</TableCell>
<TableCell align="center" className={classes.descriptionFont}>
{row.description}
</TableCell>
<TableCell align="center">{row.dueDate}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Paper>
</div>
);
What about wrapping table in fixed Container and get rid of fixed table width? It is basic element for Material-ui layout.
import Container from '#material-ui/core/Container';
import rows from "./mockdata/mock_dashboard";
const rowsData = rows;
const useStyles = makeStyles(theme => ({
root: {
width: "100%"
},
paper: {
marginTop: theme.spacing(3),
width: "100%",
overflowX: "auto",
marginBottom: theme.spacing(2),
margin: "auto"
},
table: {
width: '100%',
}
}));
export default function MainDashboard() {
const classes = useStyles();
return (
<Container fixed>
<Paper className={classes.paper}>
<Table className={classes.table} size="small">
<TableHead>
<TableRow>
<TableCell>Nz.</TableCell>
<TableCell>Data Przyjęcia</TableCell>
<TableCell>Koordynator</TableCell>
<TableCell>Link do Pliku</TableCell>
<TableCell>Dod. opis</TableCell>
<TableCell>Data Wykonania</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rowsData.map(row => (
<TableRow key={row.orderNo}>
<TableCell component="th" scope="row">
{row.orderNo}
</TableCell>
<TableCell align="center">{row.orderDate}</TableCell>
<TableCell align="center">{row.coordinator}</TableCell>
<TableCell align="center">{row.link}</TableCell>
<TableCell align="center" className={classes.descriptionFont}>
{row.description}
</TableCell>
<TableCell align="center">{row.dueDate}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</Paper>
</Container>
);
}
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