【问题标题】:How to sort values as numbers in NatTable?如何在 NatTable 中将值排序为数字?
【发布时间】:2021-09-02 07:16:04
【问题描述】:

我的问题是,尽管我使用了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, composite, 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(), DisplayMode.NORMAL,
            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);
    
    ...    
  }
}

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

【问题讨论】:

    标签: java nattable


    【解决方案1】:

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

    这在我们的文档中有解释:https://www.eclipse.org/nattable/documentation.php?page=sorting

    下面的例子展示了用法:

    【讨论】:

    • 嗨@Dirk,我想使用默认比较器,它也应该比较双数据类型。不过,在那种情况下,我不需要注册它。在上面的示例中使用了自定义比较器,所以我假设,我需要自定义比较器
    • 那么您需要为 SortConfigAttributes.SORT_COMPARATOR 注册您的自定义默认比较器,而无需指定区域和标签。然后你得到对象并需要在那里进行类型检查。我不知道如何从比较器中访问单元格标签。
    • 我在NatTableLayerConfigurations注册了自定义比较器,但没有成功。我的代码:` // 注册自定义比较器 configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, getCustomComparator(), DisplayMode.NORMAL, NatTableFactory.DataTypeNumberLabel);` 也许我还需要在列标题层注册cellLabelAccumulator
    • 如果要将其注册为默认比较器,那么为什么要将其注册为标签?该标签是否设置在发生点击的列标题中?如果它应该替换 NatTable 的默认比较器,请删除 DisplayMode 和 label 参数。
    • 每列只能有一个比较器。所以你必须处理它。
    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 2016-07-23
    • 2020-01-20
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多