如何使用材料UI并排显示2个表格?

发布于 2025-02-09 10:22:26 字数 2155 浏览 1 评论 0原文

当前看起来像

我想使用材料UI并排有2个表格。

这是桌子。

function BasicTable() {
return (
  <TableContainer component={Paper}>
    <Table sx={{ minWidth: 300 }} 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.map((row) => (
          <TableRow
            key={row.name}
            sx={{ '&:last-child td, &:last-child th': { border: 0} }}
          >
            <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>
);

}

这是我两次并排显示表的代码。

  <Box
    sx={{ 
    
    float:'left',
    clear: 'both',
    p:10,
    m:5, 
    bgcolor: 'background.paper', 
    borderRadius: 1,
   display: 'inline-flex', 
   direction:'row' }}>
   <BasicTable/>
    </Box>


    <Box
    sx={{ 
    float: 'right',
    clear: 'both',
    width:'200',
    //display: table,
    p:10,
    m:5, 
    bgcolor: 'background.paper', 
    borderRadius: 1,
   display: 'inline-block',
   direction:'row'
  // width:'50%' 
}}
    
    >
    <BasicTable/>
    </Box>
   

我只能实现这一目标

。而且它很难在不使用CSS文件的情况下格式化。任何帮助将不胜感激。谢谢。

currently looks like

I want to have 2 tables side by side in React using Material UI.

This is the table.

function BasicTable() {
return (
  <TableContainer component={Paper}>
    <Table sx={{ minWidth: 300 }} 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.map((row) => (
          <TableRow
            key={row.name}
            sx={{ '&:last-child td, &:last-child th': { border: 0} }}
          >
            <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>
);

}

This is my code for displaying the table twice side by side.

  <Box
    sx={{ 
    
    float:'left',
    clear: 'both',
    p:10,
    m:5, 
    bgcolor: 'background.paper', 
    borderRadius: 1,
   display: 'inline-flex', 
   direction:'row' }}>
   <BasicTable/>
    </Box>


    <Box
    sx={{ 
    float: 'right',
    clear: 'both',
    width:'200',
    //display: table,
    p:10,
    m:5, 
    bgcolor: 'background.paper', 
    borderRadius: 1,
   display: 'inline-block',
   direction:'row'
  // width:'50%' 
}}
    
    >
    <BasicTable/>
    </Box>
   

I'm only quite able to achieve this.currently looks like

I'm new to Material UI and its so hard to format without using a CSS file. Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初熏 2025-02-16 10:22:27

您可以使用网格而不是框。

<Grid container spacing={2}>
  <Grid item sm={6}>
   <BasicTable/>
  </Grid>
 <Grid item sm={6}>
   <BasicTable/>
 </Grid>
</Grid>

You can use Grid rather than Box.

<Grid container spacing={2}>
  <Grid item sm={6}>
   <BasicTable/>
  </Grid>
 <Grid item sm={6}>
   <BasicTable/>
 </Grid>
</Grid>
夏末 2025-02-16 10:22:27

您也可以使用stack

<Stack direction='row'>
   <BasicTable />
   <BasicTable />
</Stack>

You can use Stack too:

<Stack direction='row'>
   <BasicTable />
   <BasicTable />
</Stack>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文