html - 如何在 Material-ui 表中添加垂直列分隔符
问题描述
我创建了一个给定高度的表(比如 70vh)。我想要整个表格高度的垂直列分隔符。我可以使用 CSS for 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;
解决方案
我添加了一个height
表,70vh
因此它与容器的高度相同。然后我将显示类型更改为tableCell
允许tableRowGroup
列灵活填充可用空间。我已经TableHead
完全删除了,因为 MUI 强制其 CSS 显示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;
您可以删除TableRow
中的TableBody
以查看标题行实际上填充了所有可用空间。
推荐阅读
- ios - 如果一个节点被缩放,包含的 scntext 边界框不是太正常,这是否正常?
- amazon-web-services - 如何使用 AWS Rekognition 服务通过 javascript 检测图像中的文本
- python - 登录内部目录与外部目录不同
- ip - 如何检查我的树莓派的 ip 是否是静态的?
- jquery - Jquery datatable - 如何使表无响应
- powershell - Powershell Start-Process 无法正确启动 .exe
- php - Doctrine 联合继承结合单表继承
- java - 视频breakIn、breakOut 使用SIP
- python - 根据另一个列表中的项目数创建多个新列表
- apache - RewriteCond 范围