nattableglazedlists

Sorting in NatTables


I have got implemented filtering in nattables. How to add sorting? do I have to create new columnHeaderLayer? Here is my code:

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;
  }

getting values from tables:

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

creating filter and sorted list:

      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);
        }
      }

event layer:

      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();
        }
      }));

adding configuration to selection layer

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

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

viewport layer + return

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

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

Solution

  • If you want to add sorting by click on column header cells, you have to add the SortHeaderLayer to your column header layer stack.

    Please check the NatTable examples, like for example the SortableFilterableColumnGroupExample