How to calculate column footer total in filtered grid

I understand that I have to use grid.getDataProvider() to get the ListDataProvider (assuming I sent a List to grid.setItems()). In other to calculate the footer total I do:

Collection myItems = ((ListDataProvider)grid.getDataProvider()).getItems();
for(MyItem myItem : myItems)
   total += myItem.getValue();
footer.getCell(footerCell).setText(format(total));

But what happens if I did:

((ListDataProvider)grid.getDataProvider()).addFilter(myFilter);

If I do the initial loop way above it includes even the filtered items. In other words how can I get a total for only those items that are visible in the grid, the filtered items?

Instead of getItems() you should use fetch(…) method of the data provider. With fetch you can apply same filters with a Query or sorting than in Grid, and get stream of items, that matches what you want.

I looked at that but fetch() requires a query which I don’t understand. Also how does it link to the filters. Would you be so kind as to provide a code example because I have no idea how the query parameter of fetch() works. Also if you have any links to documentation that would be appreciated. All I could find was the API and it had no details or examples other than saying it was a query…

Thank you.

See discussion here:

https://github.com/vaadin/framework/issues/10115

​Actually this approach could suite you as well.

grid.getDataCommunicator().fetchItemsWithRange(…)

fetchItemsWithRange() is a protected method. Also how do you know what the range is because the items are filtered? The link is just to get an item at a specific index, I don’t understand how it would work. I don’t know which items are present because the data is filtered…

Does anyone have any further ideas on this?

fetchItemsWithRange() will be available in Vaadin 8.2.0

With Vaadin 8.x version below 8.2.0 you need to use the longer version as described in #10115

Thank you, I both completely forgot it from before and missed it this time.

Can you please expand on how to use fetchItemsWithRange() I see that it’s available in 8.2.0 but how exactly do you call it? Say I’ve got:

List<Pojo> myList = getListOfPojos();
grid.setItems(myList);
grid.getDataCommunicator().fetchItemsWithRange();

What would be the parameter of fetchItemsWithRange? Would it be 0, myList.size() ? I ask because tried that and the result was nothing was filtered.

Or maybe as you said use the fetch() method, but the problem is that I don’t understand how to use it. What do I use for the Query parameter?

Just to re-iterate what I’m trying to do is reload the grid after an action has taken place. Not necessarily a delete but some other action which can affect multiple rows, etc. And when I reload the filters need to be re-applied.

I have the same question. What should be the parameter for fetchItemsWithRange() ?
I have a TreeGrid with sum being displayed in the footer. When there is a filter applied to the grid, I want the sum to update.
Can’t seem to understand the syntax.
FYI i also tried using fetch() → it says non hierarchical queries are not allowed for heirarchical, so i tried using fetchChildren - that requires HierarchicalQuery instead of Query which again, i dont understand what should be the parameter for that…

@Stephan
I found the solution for your answer here: https://stackoverflow.com/questions/46722217/how-to-calculate-the-total-in-a-vaadin-8-grid-footer-with-filtering

But I cannot use that solution since I am using a TreeGrid.

@Tatu
Is there any example of updating the footer after items have been filtered when a TreeGrid is being used?