如何为 nattable 中的某些列设置自定义样式?

How to set custom style to some columns in a nattable?

我需要为 nattable 中的某些列设置自定义样式。我不能这样设置配置:

natTable.addConfiguration(new DefaultNatTableStyleConfiguration());

因为这会将配置设置为整个 table。我必须只覆盖特定列的配置。在我的例子中,列应该像这样设置水平对齐:

setHAlign(HorizontalAlignmentEnum.RIGHT);

我怎样才能做到这一点? 谢谢!

来自NatTable styling docs

To enable conditional styling, the custom style needs to be registered in the IConfigRegistry against the label defined before.

Style style = new Style();
// You can set other attributes here 
style.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR, GUIHelper.COLOR_RED);

configRegistry.registerConfigAttribute(
    CellConfigAttributes.CELL_STYLE,    // attribute to apply
    style,                  // value of the attribute
    DisplayMode.NORMAL,         // apply during normal rendering
    CELL_LABEL);    

        // apply for all cells with this label

要将 CELL_LABEL 标签应用于您的列,请按照 NatTable configuration docs

中的说明进行操作

Attaching a label to a cell

Following the overall design convention, Layers can add labels to cells. In order to attach a label to a cell(s) you need to implement the IConfigLabelAccumulator interface. The IConfigLabelAccumulator.accumulateConfigLabels() is called on each layer. Every layer can add its labels to the LabelStack.

The most common use cases are available out of the box, including but not limited to:

CellOverrideLabelAccumulator - applies labels to cell(s) containing a specified data value ColumnOverrideLabelAccumulator - applies labels to all cells in a column You can make custom implementations for your own rules