Material UI 的 TableRow
组件可以通过自定义样式来添加边框。以下是几种常见的方法:
withStyles
或 makeStyles
如果你使用的是 Material UI v4,可以使用 withStyles
高阶组件来创建自定义样式:
import React from 'react';
import { withStyles } from '@material-ui/core/styles';
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';
const useStyles = (theme) => ({
tableRow: {
'&:nth-of-type(odd)': {
backgroundColor: theme.palette.background.default,
},
border: '1px solid black',
},
});
function MyTable() {
const classes = useStyles();
return (
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow className={classes.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} className={classes.tableRow}>
<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>
);
}
export default MyTable;
如果你使用的是 Material UI v5,可以使用 makeStyles
钩子:
import React from 'react';
import { makeStyles } from '@mui/styles';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Paper from '@mui/material/Paper';
const useStyles = makeStyles({
tableRow: {
'&:nth-of-type(odd)': {
backgroundColor: '#f5f5f5',
},
border: '1px solid black',
},
});
function MyTable() {
const classes = useStyles();
return (
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow className={classes.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} className={classes.tableRow}>
<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.prosterol}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
}
export default MyTable;
你也可以直接在 TableRow
组件上使用内联样式:
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';
function MyTable() {
return (
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow style={{ border: '1px solid black' }}>
<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} style={{ border: '1px solid black' }}>
<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>
);
}
export default MyTable;
你也可以创建一个 CSS 文件,并在 TableRow
组件上应用该类:
/* styles.css */
.table-row {
border: 1px solid black;
}
然后在你的组件中引入并使用该类:
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 './styles.css';
function MyTable() {
return (
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow className="table-row">
<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} className="table-row">
<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>
);
}
export default MyTable;
以上三种方法都可以为 TableRow
组件添加边框样式。选择哪种方法取决于你的具体需求和个人偏好。通常情况下,使用 withStyles
或 makeStyles
是推荐的做法,因为它们提供了更好的样式封装和主题支持。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云