How to get rid of Table Cells in TableBody?

Viewed 38

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&nbsp;(g)</TableCell>
            <TableCell align="right">Carbs&nbsp;(g)</TableCell>
            <TableCell align="right">Protein&nbsp;(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?

1 Answers

Instead of using div and p tags inside your table, you could add a TableRow and a single TableCell. You can add colspan={5} tou your TableCell to indicate for how many columns the cell extends, like this:

<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&nbsp;(g)</TableCell>
        <TableCell align="right">Carbs&nbsp;(g)</TableCell>
        <TableCell align="right">Protein&nbsp;(g)</TableCell>
      </TableRow>
    </TableHead>
    <TableBody>
      {rows ? (
        <TableRow>
          <TableCell colspan={5} align="center">
            No items found
          </TableCell>
        </TableRow>
      ) : (
        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>
Related