问题
I need to set a custom style not for all, but some columns in a nattable. I can't set the configuration like this:
natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
because this sets the configuration to the whole table. I have to override the configuration only to specific columns. In my case the columns should have the horizontal align set like this:
setHAlign(HorizontalAlignmentEnum.RIGHT);
How can I achieve this? Thanks!
回答1:
From 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
To apply the CELL_LABEL label to your column follow the instructions from 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
来源:https://stackoverflow.com/questions/28799492/how-to-set-custom-style-to-some-columns-in-a-nattable