How do I get the list of filtered items in a grid to calculate the footer based on the filtered item

For example I have Grid and I have a searchTextField which for which I do:

GridListDataView gridListDataView = super.items(cars)
gridListDataView.addFilter(car → car.matches(searchTextField.getValue()));

I then have a footer on the table that shows the total number of cars (could be the dollar amount totalled, etc.) of the displayed cars (those that match the search criteria). Meaning I can’t just use the example in the documentation grid.addColumn(…).setFooter(cars) because I need to filter out the selected cars.

So instead I’m using:

public void recalculateFooterTotals() {
FooterRow footer = getFooterRows().get(0);
getDataProvider().fetch(new Query<>).forEach(car → myTotalObject.add(car))
footer.cell(getColumnByKey(“Cost”).setText(myTotalObject.totalCostOfAllFilteredCars());
}

I do it this way because I have to do more than one calculation and rather than loop through the list of cars once for each footer total I loop through it only once.

I then have:

searchTextField.addValueChangeListener(change → recalulateFooterTotals());

However this always shows the total for all the cars in the grid and not just the filtered ones. I will admit I’m not familiar with the fetch() method and some of this is derived from a number of sources. Ideally I’d like to keep it as simple as possible. The key is that it’s always showing all the items.

Is there a way to just get the list of filtered (searched) cars when calculating the footer totals? I suspect it has to do with the query or how I’m getting the list of cars but I’m not quite sure what the missing piece is.

I just figured it out. The key is that you need to do:

ListDataProvider listDataProvider = (ListDataProvider)getDataProvider();
int totalSize = listDataProvider.getItems().size();
DataCommunicator dataCommunicator = getDataCommunicator();

listDataProvider.fetch(new Query<>(
0,
totalSize,
dataCommunicator.getBackEndSorting(),
dataCommunicator.getInMemorySorting(),
listDataProvider.getFilter()
)).forEach(car →
myTotalObject.add(car));

I found the answer at: https://stackoverflow.com/questions/60203253/get-all-items-from-a-grid-with-current-filters-and-sorting-applied-in-vaadin-13 in case you want more details and information.