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
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 theRowSelectionModel
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 aPaintListener
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.