Get all items from a Grid with current filters and sorting applied in Vaadin 13

dosenfant :

I'm trying to get all items from a grid to be exported. The retrieved list should obey all applied filters and sorting. I'm using a ListDataProvider if it matters.

Suggested solutions include:

  1. using ListDataProvider.getItems() or grid.getDataProvider().fetch(new Query<>()).collect(Collectors.toList()) (here)
  2. using grid.getDataCommunicator().fetchItemsWithRange(0,grid.getDataCommunicator().getDataProviderSize()) (here and here)
  3. using grid.getDataCommunicator().fetchFromProvider(..) (here)

Drawbacks:

  1. The items are not sorted/filtered.
  2. Solution for Vaadin 8, method not present in Vaadin 13.
  3. Provided method is protected, so cannot be called.

How to actually get all items from the grid with applied filters and sorting?

kscherrer :

Since you cast grid.getDataProvider to a ListDataProvider<Type>, you can get the current filters from the ListDataProvider to use for the fetch Query.

But only using the Filter for the query will disregard the sort order. To take all that information into account you need to use information from both the dataProvider (filter information) and the dataCommunicator (sorting information)

ListDataProvider<Type> dataProvider = (ListDataProvider<Type>) grid.getDataProvider();
int totalSize = dataProvider.getItems().size();
DataCommunicator<Type> dataCommunicator = grid.getDataCommunicator();
Stream<Type> stream = dataProvider.fetch(new Query<>(
        0,
        totalSize,
        dataCommunicator.getBackEndSorting(),
        dataCommunicator.getInMemorySorting(),
        dataProvider.getFilter()));
List<Type> list = stream.collect(Collectors.toList());

Edit: you say in your answer that this feels "hacky". I see what you mean, but this is the way to do it. I think this behaviour could be made available as a public api on the grid itself: List<Type> list = grid.getCurrentItems();. The grid would then do this internally so you wouldn't see the "hacky" part yourself. I'm sure they know ways to do the same when the dataprovider is not an instance of ListDataProvider. You could open a feature request for this in the github repo

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=420559&siteId=1