【发布时间】:2020-04-28 13:18:34
【问题描述】:
我在这个 UI 中使用 material-table。
我正在尝试仅在 onTreeExpandChange 为 true 时自定义特定单元格的样式。
所以基本上我只想为单元格 2 和 3 设置不同的背景颜色。我附上了一个屏幕截图以获得理想的结果。这将帮助我仅识别子列标题。
一些代码示例:
{
title: 'WEST',
field: 'west',
cellStyle: { textAlign: 'center' },
headerStyle: { backgroundColor: '#006EB8' }
},
]}
data={[
{ id: 1, group_name: '1', northeast: 'Baran', central: 'ddd', west: '3' },
{ id: 2, parentId: 1, group_name: '2', northeast: 'Baran', central: 'ddd', west: '3' },
{ id: 3, parentId: 1, group_name: '3', northeast: 'Baran', central: 'ddd', west: '3' },
{ id: 4, group_name: '4', northeast: 'Baran', central: 'ddd', west: '3' },
{ id: 5, group_name: '5', northeast: 'Baran', central: 'ddd', west: '3' },
{ id: 6, group_name: '6', northeast: 'Baran', central: 'ddd', west: '3' },
]}
onTreeExpandChange={(row, rows) => {
if (rows) {
// How to changne the backgroundColor of cell 2 & 3 ?????
}
}
}
parentChildData={(row, rows) => rows.find(a => a.id === row.parentId)}
options={{
search: false,
sorting: true,
exportButton: true,
paging: false,
知道如何做到这一点吗?
谢谢
【问题讨论】:
-
您是否尝试过使用
console.log()查看row和rows中的数据?如果 row 具有.style属性,则您将设置为row.style.backgroundColor="#FF0;"。 -
是的,我做到了。 Row 实际上包含 Object 数据,因此没有样式。
标签: css reactjs material-ui material-table