【问题标题】:Adding colors to row in nattable based on condition根据条件向 nattable 中的行添加颜色
【发布时间】:2018-06-25 21:25:26
【问题描述】:

我的 NAT 表中有一个“生命周期”列,我必须根据该列为每一行设置相应的颜色。 为行添加颜色效果很好。问题是当我使用滚动条向左或向右滚动时,颜色消失了。我不知道我错过了什么。如果您知道如何解决,请帮助我

我的代码如下:

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");
  }
});

【问题讨论】:

    标签: java nattable


    【解决方案1】:

    您面临的问题是IConfigLabelAccumulator 在bodyLayer 上注册,我假设这是一个堆栈,其中最顶层是ViewportLayerbodyLayer.getDataValueByPosition(10, rowPosition); 正在返回列位置 10 的数据值。并且底层单元格在滚动时发生变化,例如,如果第一列移出可见范围,则索引为 11 的列将变为位置 10 处的列。那就是索引位置变换,这是NatTable中的一个基本概念。

    您需要执行转换计算,例如通过LayerUtil获取索引,或者直接对body的DataLayer进行操作,而不是bodylayer栈。那么你就不需要考虑索引位置变换了。我通常建议使用后者。

    由于 index-position-transformation 处理对几个人来说过于抽象,另一种选择是对 DataLayer 上的对象进行操作。为此,IConfigLabelAccumulator 需要知道IRowDataProvider 才能访问行对象。然后在DataLayer注册。

    一个示例类似于以下 sn-p。当然,它应该以更好的类分离转移到您的解决方案中。

        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");
                }
            }
        });
    

    【讨论】:

    • 我按照你的指示做了。我在数据层而不是 bodylayer 堆栈上操作。数据层数据层=新数据层(bodyDataProvider);并将配置标签累加器设置为数据层。在这种情况下,颜色不会应用于任何行/列。另一方面,我尝试了 Object dataValueByPosition = PhysicalDimensionNatTable.this.bodyLayer.getDataValueByPosition(columnPosition, rowPosition);而不是指定仅突出显示相关列值而不是整行的确切列位置。
    • 如果您想检查整行的一列的值,那么 DataLayer 上的方法应该可以工作。不知道为什么它不适合你。也许您需要调试您的累加器以查看标签未应用的原因。
    • 现在知道了。感谢您的即时帮助。
    • 嗨,德克。现在我在使用垂直滚动条时遇到了另一个问题。假设第 13 行满足条件以黄色突出显示。当我使用垂直滚动条查看下面的行时,第 14 行被突出显示,即使它不满足条件。我知道这是因为索引位置转换。您能帮我处理索引位置转换并解决它吗?
    • 我添加了一个简单的 sn-p,它展示了如何使用 IRowDataProvider 基于行对象解决您的问题。并且将 IConfigLabelAccumulator 设置为 DataLayer 应该不需要索引位置转换。
    猜你喜欢
    • 2015-04-05
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多