我确实将Nattable中的所有值都作为字符串。我使用了IConfigLabelAccumulator
,并在单元格中添加了标签DataTypeStringLabel = "String"
和DataTypeNumberLabel = "Number"
在一列中可以是具有两种类型标签的值:
public BodyLayerStack(List<TableLine> values, int columnCount, Integer[] columnIndicesForRowHeaders)
{
EventList<TableLine> eventList = GlazedLists.eventList(values);
TransformedList<TableLine, TableLine> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);
// this.filterList = new FilterList<>(rowObjectsGlazedList); //changed to:
// use the SortedList constructor with 'null' for the Comparator
// because the Comparator will be set by configuration
sortedList = new SortedList<>(rowObjectsGlazedList, null);
// wrap the SortedList with the FilterList
filterList = new FilterList<>(sortedList);
bodyDataProvider = new ListDataProvider<TableLine>(filterList, getColumnAccessor(columnCount));
bodyDataLayer = new DataLayer(bodyDataProvider);
IConfigLabelAccumulator cellLabelAccumulator = new IConfigLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
int columnIndex = bodyDataLayer.getColumnIndexByPosition(columnPosition);
int rowIndex = bodyDataLayer.getRowIndexByPosition(rowPosition);
if( isRowHeader(columnIndicesForRowHeaders, columnIndex) ) {
configLabels.addLabel(NatTableFactory.RowHeaderLabel);
} else {
// get dataTypes at actual positions and add/refresh labels
configLabels.addLabel(filterList.get(rowIndex).getObjectTypeByColumn(columnIndex));
if(configLabels.getLabels().get(0) != null && configLabels.getLabels().get(0)=="Number") {
System.out.println("Number");
}
}
}
};
bodyDataLayer.setConfigLabelAccumulator(cellLabelAccumulator);
GlazedListsEventLayer<TableLine> glazedListsEventLayer = new GlazedListsEventLayer<>(bodyDataLayer, filterList);
selectionLayer = new SelectionLayer(glazedListsEventLayer, false);
selectionLayer.setSelectionModel(getSelectionModel());
selectionLayer.addConfiguration(getSelectionConfiguration());
setUnderlyingLayer(new ViewportLayer(selectionLayer));
}
目前,所有值都按字符串排序(如预期(。但我需要将标签为Number
的值排序为数字。
什么更好:将数据转换为数字,或者使用自定义比较器?
- 如果我使用数据转换,数据转换是否仅用于排序?排序后,我需要将数据保存为字符串
- 如果我使用自定义比较器,我不确定如何注册标签。我应该使用已经创建的
IConfigLabelAccumulator cellLabelAccumulator
吗
我正在尝试:
public static IConfiguration getCustomComparatorConfiguration(final AbstractLayer columnHeaderDataLayer) {
return new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
// Register labels
BodyLayerStack.super.getConfigLabelAccumulator().registerOverride( //ERROR
4, //here should come index of sorted column?
NatTableFactory.DataTypeNumberLabel);
// Register custom comparator
configRegistry.registerConfigAttribute(
SortConfigAttributes.SORT_COMPARATOR,
NatTableLayerConfigurations.getCustomComparator(),
DisplayMode.NORMAL,
NatTableFactory.DataTypeNumberLabel);
}
};
}
但我得了The method registerOverride(int, String) is undefined for the type IConfigLabelAccumulator
。
您得到一个不提供registerOverride(int, String)
方法的IConfigLabelAccumulator
。不确定是哪个子类定义了那个方法。ColumnOverrideLabelAccumulator
提供了方法registerColumnOverride(int, String)
如果基于字符串表示的排序不够,我建议使用自定义Comparator
。
https://www.eclipse.org/nattable/documentation.php?page=sorting