So, I'm creating a table for the data I retrieve from my backend server. I am using the Table component from Material UI. From the data I retrieve, it could be empty or include an object. The problem I am having is that if it is empty, I want to display that there are no logs in the middle of Table Component for the user to see. However, because I have defined TableCell in the TableHead, the number of cells is copied into the TableBody (or so I believe) and this is stopping my message from being centered.
Here is my component:
export default function BasicTable() {
const classes = useStyles();
return (
<TableContainer component={Paper}>
<Table className={classes.table} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
<TableCell align="right">Carbs (g)</TableCell>
<TableCell align="right">Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows ? (
<div className={classes.noLogs}>
<p>No logs</p>
</div>
) : (
rows.map((row) => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="right">{row.calories}</TableCell>
<TableCell align="right">{row.fat}</TableCell>
<TableCell align="right">{row.carbs}</TableCell>
<TableCell align="right">{row.protein}</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>
</TableContainer>
);
}
Full code is available on CodeSandbox.
As you can see, the message is only centered in the first cell of TableBody. Is there a way in which I can either delete these cells ONLY when the data is empty or to center the message by some other method?