如何在 NatTable 中将值排序为数字?

问题描述

我的问题是,尽管我使用了 DefaultDoubledisplayConverter,但数据值按字符串排序。我将转换器注册到单元格标签,而不是列标题标签。我的代码

public class NatTableFactory {
  private NatTable createTable(Composite parent,List<TableLine> tLines,String[][] propertyNames,PropertyToLabels[] propToLabels,TableParams params,TextMatcherEditor<TableLine>editor,boolean openableParts) {
    
    BodyLayerStack bodyLayerStack =
        new BodyLayerStack(
                tLines,tLines.get(0).getLength(),params.getColumnIndicesForRowHeaders());
    
    ...

    SortHeaderLayer<TableLine> sortHeaderLayer =
        new SortHeaderLayer<TableLine>(
            columnHeaderLayer,new GlazedListsSortModel<TableLine>(
                bodyLayerStack.getSortedList(),getSortingColumnPropAccessor(propertyNames[0]),configRegistry,columnHeaderDataLayer));
 
    ...
    
    composite.addConfiguration(NatTableLayerConfigurations.getCompoositeLayerConfiguration());
    
    NatTable natTable = new NatTable(parent,composite,false);
    
    if( params.getAutoFitColWidthindices().size() > 0 )
      registerautoResizeColCmdHandler(natTable,bodyLayerStack,params.getAutoFitColWidthindices());
    
    setNatTableContentTooltip(natTable);
    
    natTable.setConfigRegistry(configRegistry);
   
    natTable.addConfiguration(new SingleClickSortConfiguration());
    
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());

    setNatTableContextMenu(natTable,openableParts);
    
    natTable.addConfiguration(NatTableLayerConfigurations.getNatTableConfiguration());        
    
    //natTable.addConfiguration(NatTableLayerConfigurations.getCustomConvertConfiguration(bodyDataLayer));
    
    natTable.configure();

    ...

    NatTableContentProvider.addNatTableData(natTable,bodyLayerStack.getSelectionLayer(),bodyLayerStack.getBodyDataProvider());
    
    return natTable;
  }
}

然后:

public class NatTableLayerConfigurations
{
  ...

  public static AbstractRegistryConfiguration getNatTableConfiguration()
  {
    return new AbstractRegistryConfiguration()
    {

      @Override
      public void configureRegistry(IConfigRegistry configRegistry)
      {
        ...    
        Style cellAlignStyle = new Style();
        cellAlignStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT,HorizontalAlignmentEnum.RIGHT);
        configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,cellAlignStyle,displayMode.norMAL,NatTableFactory.DataTypeNumberLabel);
        //
        configRegistry.registerConfigAttribute(
            CellConfigAttributes.disPLAY_CONVERTER,new DefaultDoubledisplayConverter(),NatTableFactory.DataTypeNumberLabel);
        System.out.println("configRegistry.registerConfigAttribute CellConfigAttributes.disPLAY_CONVERTER");
        
        ...
      }
    };
  }
}

和:

public class BodyLayerStack extends AbstractLayerTransform
{
  ...    
  public BodyLayerStack(List<TableLine> values,int columnCount,Integer[] columnIndicesForRowHeaders)
  {
    EventList<TableLine> eventList = GlazedLists.eventList(values);
    TransformedList<TableLine,TableLine> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);

    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 {
          configLabels.addLabel(filterList.get(rowIndex).getobjectTypeByColumn(columnIndex));
          
          // NatTableLayerConfigurations.getNatTableConfiguration();
        }
      }
    };
    bodyDataLayer.setConfigLabelAccumulator(cellLabelAccumulator);
    
    GlazedListsEventLayer<TableLine> glazedListsEventLayer = new GlazedListsEventLayer<>(bodyDataLayer,filterList);
    
    ...    
  }
}

我检查了各种示例,但我看不到,我错过了什么。以及如何检查单元格中的数据是否正确转换?谢谢你的提示

解决方法

我想您缺少排序比较器的配置。

我们的文档对此进行了说明:https://www.eclipse.org/nattable/documentation.php?page=sorting

下面的例子展示了用法: