在 NatTables 中排序

Sorting in NatTables

我已经在 nattables 中实现了过滤。如何添加排序?我必须创建新的 columnHeaderLayer 吗?这是我的代码:

1 class BodyLayerStack

class BodyLayerStack extends AbstractLayerTransform {

  private final FilterList<TableLine> filterList;
  private final SelectionLayer selectionLayer;
  private final DataLayer bodyDataLayer;
  private final IRowDataProvider<TableLine> bodyDataProvider;
  private final SortedList<TableLine> sortedList;
  public DataLayer getBodyDataLayer() {
    return bodyDataLayer;
  }
  public SelectionLayer getSelectionLayer() {
    return selectionLayer;
  }
  public IRowDataProvider<TableLine> getBodyDataProvider() {
    return bodyDataProvider;
  }

从表中获取值:

  public BodyLayerStack(List<TableLine> values, IColumnAccessor<TableLine> columnAccessor, Integer[] columnIndicesForRowHeaders) {
      EventList<TableLine> eventList = GlazedLists.eventList(values);
      TransformedList<TableLine, TableLine> rowObjectsGlazedList = GlazedLists.threadSafeList(eventList);

正在创建过滤器和排序列表:

      this.filterList = new FilterList<>(rowObjectsGlazedList);
      this.sortedList = new SortedList<>(filterList, null);
      this.bodyDataProvider = new ListDataProvider<TableLine>(this.sortedList, columnAccessor);
      bodyDataLayer = new DataLayer(this.bodyDataProvider);
      ColumnOverrideLabelAccumulator bodyLabelAccumulator = new ColumnOverrideLabelAccumulator(bodyDataLayer);
      bodyDataLayer.setConfigLabelAccumulator(bodyLabelAccumulator);

      if( columnIndicesForRowHeaders != null ) {
        for ( int i = 0; i < columnIndicesForRowHeaders.length; i++ ) {
          bodyLabelAccumulator.registerColumnOverrides(
            columnIndicesForRowHeaders[i],
            RowHeaderLabel);
        }
      }

事件层:

      GlazedListsEventLayer<TableLine> glazedListsEventLayer =
            new GlazedListsEventLayer<>(bodyDataLayer, this.filterList);

      this.selectionLayer = new SelectionLayer(glazedListsEventLayer, false);
      selectionLayer.setSelectionModel(new RowSelectionModel<TableLine>(selectionLayer, this.bodyDataProvider, new IRowIdAccessor<TableLine>()
      {
        @Override
        public Serializable getRowId(TableLine line)
        {
          return line.getId();
        }
      }));

向选择层添加配置

      selectionLayer.addConfiguration(new DefaultSelectionLayerConfiguration()
      {
        @Override
        protected void addSelectionUIBindings()
        {
          addConfiguration(new SelectionBindings());
        }

        @SuppressWarnings("rawtypes")
        @Override
        protected void addMoveSelectionConfig()
        {
          addConfiguration(new RowOnlySelectionConfiguration());
        }
      });

视口图层 + return

      ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
      setUnderlyingLayer(viewportLayer);
  }

  public FilterList<TableLine> getFilterList() {
      return this.filterList;
  }  
}

如果您想通过点击列 header 单元格来添加排序,您必须将 SortHeaderLayer 添加到列 header 层堆栈。

请检查NatTable examples, like for example the SortableFilterableColumnGroupExample