如何解决我的React表格单元格问题(使用React表格和MUI)

huangapple go评论50阅读模式
英文:

How to fix my React Table cell problem (using React Table and MUI)

问题

I am new to React and I created a table component using React Table-MUI. Then I tried to put my table row into <Box></Box> like the code below. Then I saw my whole table body is only taking space of the first column, and other columns are empty.

How do I correct this problem? The <Box></Box> is mandatory to wrap every row.

<TableContainer>
    <TableHeader>
    .
    .
    .
    </TableHeader>

    <TableBody>
        {data.map((item, index) => {
            return (
                <Box>
                   <TableCell></TableCell>
                   <TableCell></TableCell>
                   <TableCell></TableCell>
                </Box>
            )
        })}
    </TableBody>
</TableContainer>

I need to get every column under its header. Right now, the whole row is under the first column. How can I do this?

英文:

I am new to React and I created table component using React Table-MUI. Then I try to put my table row in to &lt;Box&gt;&lt;/Box&gt; like below code. Then I saw my whole table body is only take space of y first column and other columns are empty.

How do I correct this problem? The &lt;Box&gt;&lt;/Box&gt; is mandatory to wrap every row.

&lt;TableContainer&gt;
    &lt;TableHeader&gt;
    .
    .
    .
    &lt;/TableHeader&gt;

    &lt;TableBody&gt;
        {data.map((item,index) =&gt; {
            return(
                &lt;Box&gt;
                   &lt;TableCell&gt;&lt;/TableCell&gt;
                   &lt;TableCell&gt;&lt;/TableCell&gt;
                   &lt;TableCell&gt;&lt;/TableCell&gt;
                &lt;/Box&gt;
            )
        })}
    &lt;/TableBody&gt;
&lt;/TableContainer&gt;

I need to get every column under to it header. Right now whole row is in under first column. How can I do this?

答案1

得分: 1

看表格语法: https://www.w3schools.com/html/html_tables.asp

以及 MUI 表格文档: https://mui.com/material-ui/react-table/

<table>
  <thead>
    <tr>
      <th>Month</th>
      <th>Savings</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Sum</td>
      <td>$180</td>
    </tr>
  </tfoot>
</table>

MUI 表格在底层是一个具有样式的简单表格。在tbody中不应使用div,而应该使用TableRow来包装单元格。

英文:

Look at table syntax: https://www.w3schools.com/html/html_tables.asp

And MUI table docs: https://mui.com/material-ui/react-table/

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Month&lt;/th&gt;
      &lt;th&gt;Savings&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;January&lt;/td&gt;
      &lt;td&gt;$100&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;February&lt;/td&gt;
      &lt;td&gt;$80&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tfoot&gt;
    &lt;tr&gt;
      &lt;td&gt;Sum&lt;/td&gt;
      &lt;td&gt;$180&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tfoot&gt;
&lt;/table&gt;

Under hood MUI table simple table with styles. You should not use div in tbody. Wrap cells with TableRow.

huangapple
  • 本文由 发表于 2023年2月19日 23:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定