Adding colors to row in nattable based on condition

南楼画角 提交于 2020-01-16 08:55:33

问题


I have a column 'Lifecycle' in my NAT Table based on which i have to set respective color to each row. Adding color to row works fine. The problem is when i use the scroll bar to scroll either left or right, the color disappears. I am not aware what i am missing. Kindly help me if you have any idea of how it can be resolved

My code looks like:

IConfigLabelAccumulator cellLabelAccumulator = new IConfigLabelAccumulator() {

  @Override
  public void accumulateConfigLabels(final LabelStack configLabels, final int columnPosition,
      final int rowPosition) {

    Object dataValueByPosition = PhysicalDimensionNatTable.this.bodyLayer.getDataValueByPosition(10, rowPosition);

    if ((dataValueByPosition != null) && dataValueByPosition.equals("Valid")) {
      configLabels.addLabel("VALID");
    }
    if ((dataValueByPosition != null) && dataValueByPosition.equals("Invalid")) {
      configLabels.addLabel("INVALID");
    }
    if ((dataValueByPosition != null) && dataValueByPosition.equals("Obsolete")) {
      configLabels.addLabel("OBSOLETE");
    }
  }
};
this.bodyLayer.setConfigLabelAccumulator(cellLabelAccumulator);

this.natTable.addConfiguration(new AbstractRegistryConfiguration() {

  @Override
  public void configureRegistry(final IConfigRegistry configRegistry) {
    Style cellStyle = new Style();
    cellStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_GREEN);
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL, "VALID");

    cellStyle = new Style();
    cellStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_RED);
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL,
        "INVALID");

    cellStyle = new Style();
    cellStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_YELLOW);
    configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL,
        "OBSOLETE");
  }
});

回答1:


The issue that you are facing is that the IConfigLabelAccumulator is registered on the bodyLayer, and I assume that is a stack where the top most layer is the ViewportLayer. bodyLayer.getDataValueByPosition(10, rowPosition); is returning the data value of the column position 10. And the underlying cell changes on scrolling as for example the column with index 11 becomes the column at position 10 if the first column moves out of the visible range. That is the index-position-transformation which is a basic concept in NatTable.

Either you need to perform a transformation calculation e.g. via LayerUtil to get the index, or operate on the DataLayer of the body directly instead of the bodylayer stack. Then you don't need to consider the index-position-transformation. I typically suggest to use the later.

As the index-position-transformation handling is too abstract for several people, another option is to operate on the object on the DataLayer. For this the IConfigLabelAccumulator needs to know the IRowDataProvider to be able to access the row object. And then register it on the DataLayer.

An example would look like the following snippet. Of course it should be transferred to your solution with better class separation.

    IRowDataProvider<PersonWithAddress> bodyDataProvider = new ListDataProvider<>(data, accessor);
    DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);

    bodyDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator() {

        @Override
        public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
            PersonWithAddress person = bodyDataProvider.getRowObject(rowPosition);
            if ("Simpson".equals(person.getLastName())) {
                configLabels.addLabel("YELLOW");
            }
        }
    });


来源:https://stackoverflow.com/questions/48293631/adding-colors-to-row-in-nattable-based-on-condition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!