Reputation: 532
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:
ListDataProvider.getItems()
or grid.getDataProvider().fetch(new Query<>()).collect(Collectors.toList())
(here)grid.getDataCommunicator().fetchItemsWithRange(0,grid.getDataCommunicator().getDataProviderSize())
(here and here)grid.getDataCommunicator().fetchFromProvider(..)
(here)Drawbacks:
How to actually get all items from the grid with applied filters and sorting?
Upvotes: 4
Views: 1629
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
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
getItems().size()
) to get the range for the actual query.getFilter()
method of the DataCommunicator
is protected
and cannot be called to retrieve the filter (therefore null
(= no filter) is used here).Upvotes: 0