css 如何在Material-ui表中添加垂直列分隔符

fcg9iug3  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(131)

我已经创建了一个给定高度的表格(比如70vh)。我希望表格的整个高度都有一个垂直的列分隔符。我可以使用CSS为TableCell添加它。但是我希望即使我没有任何TableCell,垂直的列分隔符也应该在那里。

import React from "react";

//MUI Stuffs
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";

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";

const useStyles = makeStyles((theme) => ({
  tableContainer: {
    maxWidth: "150vh",
    margin: "auto",
    marginTop: "15vh",
    height: "70vh",
    background: "#ccffff",
    borderWidth: 2,
    borderColor: "black",
    borderStyle: "solid",
  },
  tableCell: {
    borderRightStyle: "solid",
    borderRightColor: "black",
  },
  tableHead: {
    borderBottomStyle: "solid",
    borderBottomColor: "blue",
    borderBottomWidth: 3,
  },
}));

function Canvas() {
  const classes = useStyles();
  const cols = ["Col1", "Col2", "Col3", "Col4", "Col5"];
  return (
    <TableContainer component={Paper} className={classes.tableContainer}>
      <Table className={classes.table} aria-label="simple table">
        <TableHead className={classes.tableHead}>
          <TableRow>
            {cols.map((col) => (
              <TableCell align="center" className={classes.tableCell}>
                {col}
              </TableCell>
            ))}
          </TableRow>
        </TableHead>
        <TableBody>
          <TableRow></TableRow>
        </TableBody>
      </Table>
    </TableContainer>
  );
}

export default Canvas;

字符串


的数据

chy5wohz

chy5wohz1#

我通过不使用TableHeader

成功地做到了这一点
我在70vh的表中添加了一个height,使其与容器的高度相同。然后我将tableCell的显示类型更改为tableRowGroup,这使得列可以灵活地填充可用空间。我完全删除了TableHead,因为MUI强制CSS显示table-header-group,而table-header-group不会扩展以填充表空间。

import React from "react";

//MUI Stuffs
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";

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 TableRow from "@material-ui/core/TableRow";

const useStyles = makeStyles((theme) => ({
  tableContainer: {
    maxWidth: "150vh",
    margin: "auto",
    marginTop: "15vh",
    height: "70vh",
    background: "#ccffff",
    borderWidth: 2,
    borderColor: "black",
    borderStyle: "solid"
  },
  table: {
    height: "70vh"
  },
  tableCell: {
    borderRightStyle: "solid",
    borderRightColor: "black",
    display: "tableRowGroup"
  }
}));

function Canvas() {
  const classes = useStyles();
  const cols = ["Col1", "Col2", "Col3", "Col4", "Col5"];
  return (
    <TableContainer component={Paper} className={classes.tableContainer}>
      <Table className={classes.table} aria-label="simple table">
        <TableRow>
          {cols.map((col) => (
            <TableCell align="center" className={classes.tableCell}>
              {col}
            </TableCell>
          ))}
        </TableRow>
        <TableBody>
          <TableRow>
            {cols.map((col) => (
              <TableCell align="center" className={classes.tableCell}>
                {col}
              </TableCell>
            ))}
          </TableRow>
        </TableBody>
      </Table>
    </TableContainer>
  );
}

export default Canvas;

字符串
您可以删除TableBody中的TableRow,以查看标题行实际上是否填充了所有可用空间。

相关问题