【问题标题】:material ui: reactjs: tablecell: width fit to content材料ui:reactjs:tablecell:宽度适合内容
【发布时间】:2021-03-27 20:30:53
【问题描述】:

我有以下代码。它与 Material ui 中的 basic table 相同。只有我修改了第一行,第二列的内容。我在里面使用了一个 Grid 元素。并且还添加了更多列,以便我可以看到水平滚动场景

我不想让它坏。

import React from 'react';
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';
import Grid from '@material-ui/core/Grid';
import { createMuiTheme, makeStyles, ThemeProvider } from '@material-ui/core/styles';

const hare = makeStyles({
  table: {
    minWidth: 650,
  },
});

function createData(name, calories, fat, carbs, protein) {
  return { name, calories, fat, carbs, protein };
}


const rows = [
  createData('Frozen yoghurt',
        159,
        <Grid container alignItems="center" justify="space-between" spacing={10}>
          <Grid item>
             Dont
          </Grid>
          <Grid item>
            Break
          </Grid>
        </Grid>,
        24,
        4.0
  ),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Gingerbread', 356, 16.0, 49, 3.9),
];



export default function BasicTable() {
  const classes = hare();

  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>
              <TableCell align="right">Protein&nbsp;(g)</TableCell>
              <TableCell align="right">Protein&nbsp;(g)</TableCell>
              <TableCell align="right">Protein&nbsp;(g)</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>
              <TableCell align="right">Protein&nbsp;(g)</TableCell>
              <TableCell align="right">Protein&nbsp;(g)</TableCell>
              <TableCell align="right">Protein&nbsp;(g)</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {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>
  );
}

看到的是

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    您可以在Grid 上使用wrap="nowrap" 属性。来自docs

    wrap - 定义 flex-wrap 样式属性。它适用于所有屏幕尺寸。

    只是我更改的摘录,以及codesandbox

    <Grid container wrap="nowrap" alignItems="center" justify="space-between" spacing={10}>
      <Grid item>Dont</Grid>
      <Grid item>Break</Grid>
    </Grid>
    

    【讨论】:

    • 另外,如果我试图不为第一列标题换行。我试试&lt;TableCell wrap="nowrap"&gt;Dessert (100g serving)&lt;/TableCell&gt;。文本仍然被包装。我怎么也可以为此使用 nowrap
    • TableCellth/td 在引擎盖下我猜。如果您愿意,可以使用添加white-space: nowrap;
    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2012-07-13
    • 2012-07-01
    • 1970-01-01
    相关资源
    最近更新 更多