【发布时间】:2021-02-17 06:25:03
【问题描述】:
我正在为我的客户评估 React-Material 网格。关键反馈之一是没有交替的行阴影,这会影响用户体验。
https://material-ui.com/components/data-grid/#mit-version
这可能吗?
实际
希望
谢谢,
修
【问题讨论】:
我正在为我的客户评估 React-Material 网格。关键反馈之一是没有交替的行阴影,这会影响用户体验。
https://material-ui.com/components/data-grid/#mit-version
这可能吗?
谢谢,
修
【问题讨论】:
通过 CSS 选择器添加非常简单。
如果为奇数行添加选择器.Mui-odd,则可以设置背景颜色并将其设为条纹。您可以使用.Mui-even 代替另一半。
.MuiDataGrid-row.Mui-odd {
background-color: aliceblue;
}
如果您想使用:nth-child,那么您需要:nth-child(even) 用于与.Mui-odd 相同的行集,尽管.Mui-odd 保持它在页面之间的排序,而伪选择器没有t.
.MuiDataGrid-row:nth-child(even){
background-color: aliceblue;
}
【讨论】:
nth-child 选择器。您还可以向 github 存储库提交问题或 PR 以将其记录在案。
使用此解决方案,您可以通过亮/暗模式使其动态化,并保留鼠标悬停效果。
const useStyles = makeStyles((theme) =>
({
root: {
'& .MuiDataGrid-row.Mui-even:not(:hover)': {
backgroundColor: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.04)' : 'rgba(255, 255, 255, 0.04)',
},
},
}),
);
【讨论】:
<div className={classes.root}>之类的东西包裹你的DataGrid吗?
theme.palette.secondary[300] 的颜色来坚持主题可能会更好。
这在 MUI v5 - TypeScript 中对我有用。
const useStyles = makeStyles((theme: Theme) =>
({
root: {
'& .MuiDataGrid-row:nth-child(odd)': {
backgroundColor: theme.palette.mode === 'light' ? theme.palette.secondary[300] : theme.palette.secondary[900]
},
},
}),
);
【讨论】: