I am using one of materialUI's built-in components to display data on one of my sites. Currently, the code that was implemented is very closely based off of the examples on the MaterialUI API site. With that said, I have made a few adjustments for my own personal use case. This is how each of the columns that I need are labeled (5 total). I've also included my code below.
import React from 'react';
import {
withStyles,
Theme,
createStyles,
makeStyles,
} from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
const StyledTableCell = withStyles((theme: Theme) =>
createStyles({
head: {
backgroundColor: '#533cf8',
color: theme.palette.common.white,
fontSize: 11,
},
body: {
fontSize: 10,
},
}),
)(TableCell);
const StyledTableRow = withStyles((theme: Theme) =>
createStyles({
root: {
'&:nth-of-type(odd)': {
backgroundColor: theme.palette.action.hover,
},
},
}),
)(TableRow);
function createData(
parameters: string,
required: string,
description: string,
defaults: string,
type: string,
) {
return { parameters, required, description, defaults, type };
}
const rows = [
createData('id', 'yes', 'id to update', 'n/a', 'string'),
// eslint-disable-next-line prettier/prettier
createData('start', 'no', 'datetime in YYYY-MM-DD HH:mm:ss format that specifies the start', 'n/a', 'string',),
// eslint-disable-next-line prettier/prettier
createData('end', 'no', 'datetime string in YYYY-MM-DD HH:mm:ss format that specifies the end', 'n/a', 'string',),
// eslint-disable-next-line prettier/prettier
createData('provider', 'no', 'subdomain of the provider this affects', 'n/a', 'string',),
createData('resources', 'no', 'list of ids for resources', 'n/a', 'string[]'),
];
const useStyles = makeStyles({
main: {
maxWidth: '90%',
marginRight: 'auto',
marginBottom: '75px',
marginLeft: '25px',
borderRadius: '8px',
},
});
export default function CustomTable(): JSX.Element {
const classes = useStyles();
return (
<TableContainer
component={Paper}
className={classes.main}
elevation={0}
style={{
borderRight: '0.3px solid lightgray',
borderLeft: '0.3px solid lightgray',
}}
>
<Table aria-label="customized table">
<TableHead>
<TableRow>
<StyledTableCell>Parameters</StyledTableCell>
<StyledTableCell align="right">Required</StyledTableCell>
<StyledTableCell align="right">Description </StyledTableCell>
<StyledTableCell align="right">Defaults </StyledTableCell>
<StyledTableCell align="right">Type </StyledTableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<StyledTableRow key={row.parameters}>
<StyledTableCell component="th" scope="row">
{row.parameters}
</StyledTableCell>
<StyledTableCell align="right">{row.required}</StyledTableCell>
<StyledTableCell align="right">{row.description}</StyledTableCell>
<StyledTableCell align="right">{row.defaults}</StyledTableCell>
<StyledTableCell align="right">{row.type}</StyledTableCell>
</StyledTableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
For my project, I am going to be using this table many, many times. The column headings won't be changing, but the number of rows/parameters will definitely be changing. For example, there might be a table with only 2 rows, while another could have up to 8.
In my current code, the table is being populated using the 'const rows' variable, but since I am using this component throughout my site, I want to pass something similar using props/variables, instead of having to create a new component each time I want to fill a table with data. I am relatively new to ReactJS, so any help is much appreciated. Thank you.
