[英]Dynamically change row color in NatTable
I am trying to change the background row color based on the rows criteria. 我试图根据行条件更改背景行颜色。 I am very close but there's something off I can't quite put my finger on. 我离得很近,但是有些事我不能完全按一下。 (I believe it's because I'm pulling an Object from an underlying list vs getting the data dynamically. I marked this section of the code below) (我相信这是因为我要从基础列表中拉一个对象,而不是动态获取数据。我在下面的代码部分做了标记)
In the example below, each row color is based off of an Object (MyObj) which has a success or failure value. 在下面的示例中,每行颜色均基于具有成功或失败值的对象(MyObj)。 If myObj has a success value the row should be green. 如果myObj具有成功值,则该行应为绿色。 If myObj has a failure value the row should be red. 如果myObj具有失败值,则该行应为红色。 If myObj has no value, the default row color should be used. 如果myObj没有值,则应使用默认行颜色。
When I run the code, the row colors display as expected. 当我运行代码时,行颜色将按预期显示。 However, if I sort the columns the original rows index maintains that color while the data moves to a new row index. 但是,如果我对列进行排序,则当数据移动到新的行索引时,原始行索引将保持该颜色。 I would expect that the row color would move with the object instead of always being fixed at that row index. 我希望行颜色将随对象一起移动,而不是始终固定在该行索引处。
Example:
Row 1 - "SUCCESS" - Shows Green
Row 2 - "FAIL" - Shows Red
If I sort on that column alphabetically I get: 如果按字母顺序对该列进行排序,则会得到:
Row 1 - "FAIL - Shows Green
Row 2 - "SUCCESS" - Shows Red
Below is the code snippet I use to generate the example: 下面是我用来生成示例的代码片段:
void example() {
getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
Style cellStyleSuccess = new Style();
cellStyleSuccess.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
COLOR_SUCCESS);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
cellStyleSuccess,
DisplayMode.NORMAL, "SUCCESS");
Style cellStyleFail = new Style();
cellStyleFail.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR,
COLOR_FAILURE);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE,
cellStyleFail,
DisplayMode.NORMAL, "FAIL");
}
});
DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
IConfigLabelAccumulator cellLabelAccumulator =
new IConfigLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels,
int columnPosition, int rowPosition) {
configLabels.getLabels().clear();
// TODO Is this the issue? Is there a better way to
// pull MyObj here?
MyObj myObj = getEventList().get(rowPosition);
if (myObj.getFoo().equals("SUCCESS")) {
configLabels.addLabel("SUCCESS");
} else if (myObj.getFoo().equals("FAIL"))) {
configLabels.addLabel("FAIL");
} else {
// default color
}
}
};
dl.setConfigLabelAccumulator(cellLabelAccumulator);
getNatTable().configure();
}
The important part that probably causes the issue is missing. 可能导致问题的重要部分丢失了。 Which list is returned by getEventList()
? getEventList()
返回哪个列表? If it is the basic EventList
you always get the object at the original index. 如果它是基本的EventList
,则始终在原始索引处获取该对象。 When you sort a transformation is applied via SortedList
. 排序时,将通过SortedList
应用SortedList
。 So your issue should be solved if getEventList()
returns the top most GlazedLists collection ( SortedList
or FilterList
dependent on which features you are using). 因此,如果getEventList()
返回最顶级的GlazedLists集合(取决于所使用的功能是SortedList
还是FilterList
getEventList()
则应该解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.