In the Grid
widget of Vaadin 14, backed by a ListDataProvider
, after applying a filter(s) via a call such as setFilter
or addFilter
…
➥ How does one detect how many of the items in the data provider are currently displayed to the user?
I have noticed the ListDataProvider::size
method. But that method takes a Query
with a SerializablePredicate
. My multiple filters have already been successfully applied, and my Grid
is showing a subset of the data provider's items. So I do not want to run another query. I want to know the results of the filters already applied.
new Query<>( myDataProvider.getFilter() )
to ListDataProvider::size
ListDataProvider
has the method size(Query<T, SerializablePredicate<T>> query)
.
Because the dataprovider only keeps the original list of items, a simple size
method without parameters would not work as you wanted. You can build the required query using the current filters of your dataprovider.
I tested the following code and it works:
// my own method, defined as value change listeners of any filter field
private void onFilterChange(){
ListDataProvider<Foo> dataProvider = (ListDataProvider<Foo>) grid.getDataProvider();
dataProvider.setFilter((item) -> {......}
// everytime after resetting the filter, check how many items now are displayed
int filteredSize = dataProvider.size(new Query<>(dataProvider.getFilter()));
int fullSize = dataProvider.getItems().size(); // this is how you get the size of all items
Notification.show(String.format("Now showing %d of %d items", filteredSize, fullSize));
}
Perhaps in the future this work might be a little easier: See feature-request ticket Add filteredSize
method to DataProvider
or ListDataProvider
#7539.