dosenfant
dosenfant

Reputation: 532

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

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?

Upvotes: 4

Views: 1629

Answers (2)

kscherrer
kscherrer

Reputation: 5766

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

Upvotes: 5

dosenfant
dosenfant

Reputation: 532

One solution I found works for me. This does not obey to filtering, but sorting, which is currently sufficient for my case. And this seems very hacky:

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

The main problems I see here are

  1. All items have to be extracted before (getItems().size()) to get the range for the actual query.
  2. The getFilter() method of the DataCommunicator is protected and cannot be called to retrieve the filter (therefore null (= no filter) is used here).

Upvotes: 0

Related Questions