【发布时间】:2020-05-11 20:51:47
【问题描述】:
我的目标是根据单元格值设置行的背景颜色。
着色适用于两者,但第一行失去了悬停效果。
有没有办法让行着色表现得像单元格一样?
这样做的原因是保持交替的行颜色,同时在其上放置背景,而不会丢失悬停。
这也是这里的代码......
let data = [
{ string: "aaaaaaaaaa", number: 0 },
{ string: "bbbbbbbbbb", number: 0 },
{ string: "cccccccccc", number: 1 },
{ string: "dddddddddd", number: 1 },
{ string: "eeeeeeeeee", number: 0 },
{ string: "ffffffffff", number: 1 },
{ string: "gggggggggg", number: 0 },
{ string: "hhhhhhhhhh", number: 0 }
];
var table = new Tabulator("#example-table", {
data: data,
rowFormatter: row => {
let data = row.getData();
if (data.number === 1) {
row.getElement().style.cssText += "background: rgba(255, 0, 0, 0.5);";
}
},
columns: [
{
title: "string",
field: "string"
},
{
title: "number",
field: "number",
formatter: cell => {
if (cell.getValue() === 0) {
cell.getElement().style.cssText += "background: rgba(255, 255, 0, 0.5);";
}
return cell.getValue();
}
}
]
});
【问题讨论】:
-
当使用 .style.cssText 时,它会替换元素上的所有 css。要仅替换背景颜色,请执行 .style.backgroundColor=red。如果这不能解决问题,请告诉我。
-
该死,我在代码中犯了一个错误。我实际上使用了 `style.cssText += "background...",但它仍然不起作用。为什么它适用于单元格,而不是行...
标签: javascript css tabulator