NatTable preserve row selection on clear data and reload

616 Views Asked by At

I use the below snippet to clear the NatTable data and do a complete reload on refresh action.

natTable.getBodyDataProvider().getList().clear();
natTable.getBodyDataProvider().getList().addAll(inputList);
natTable.refresh();

Since, the data is cleared and reset, I am unable to set the row selection back to previously selected row before refresh.

However, I am using the RowSelectionModel successfully to restore selection on Sort

final RowSelectionModel<T> rowSelectionModel = new RowSelectionModel<T>(bodyLayer.getSelectionLayer(),bodyDataProvider, rowIdAccessor);
bodyLayer.getSelectionLayer().setSelectionModel(rowSelectionModel);

Is it possible to restore row selection when data is cleared and reloaded??

I did see existing question on PreserveModel : How to use Nebula NatTable's PreserveSelectionModel? But it did not answer my question.

Appreciate if anyone could give more pointers regarding this.

Regards, SDS

1

There are 1 best solutions below

4
On BEST ANSWER

IIRC when you clear the underlying list (and I suppose you are using GlazedLists so there is an event fired when you do so), the RowSelectionModel clears the internally stored selections. Which is consistent, as it does not make sense to keep a selection for an object that has been removed.

So you need to implement a workaround that deals with that fact. That can be for example to implement a custom ISelectionModel that extends the RowSelectionModel and ensures consistency with the underlying list in a different way. Or you remember the selection before you clear the underlying list, and apply the selection afterwards again. But to do this you need to register a PaintListener on NatTable to apply the selection late, otherwise the internal events will clear the selection with a delay.

The following snippet for example will always select the 5th row in the body region of a NatTable, which is the 6th row in the NatTable because of the column header row.

natTable.addPaintListener(new PaintListener() {

    @Override
    public void paintControl(PaintEvent e) {
        // use column 1 as column 0 in NatTable is the row header
        // insert the rows to select that you previously cached
        natTable.doCommand(
                new SelectRowsCommand(natTable, 1, 5, false, false));
    }
});