Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
75 views
in Technique[技术] by (71.8m points)

glazedlists - 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;
  }  
}
question from:https://stackoverflow.com/questions/65881864/sorting-in-nattables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...