How to re-apply filters after grid.setItems()

I’ve added filters to my Grid such as the following:

filterTextField.addValueChangeListener(event ->
{
    myListDataProvider.addFilter(
         columnName,
         value-> value.toLowerCase().contain(filterTextField.getValue()));
});

This sets and applies the filter perfectly however later I perform an action on the grid which necessitates a reload of the items such that:

grid.setItems(reloadItemsDueToAction()); When I do this the filterTextField is of course still populate and the grid is reloaded, however the problem is that I don’t know how to re-apply the filters that were set in the ListDataProvider so that the grid is once again filtered according to the filterTextField. The filters should still be set in the ListDataProvider but how do I apply them in the grid?

You can just apply the filter to a new ListDataProvider and set the new data provider to grid.

How exactly do you do that? I’ve been trying but I can’t find the API calls, that or they aren’t obvious, require parameters I’m not familiar with, etc…

This is the code that worked for me:

SerializablePredicate filter = ((ListDataProvider)grid.getDataProvider()).getFilter();
grid.setItems(updatedItems);
((ListDataProvider)grid.getDataProvider()).setFilter(filter);

For those curious about how to do it in Vaadin 7, the below worked for me:

Collection<Filter> filters = grid.getContainerDataSource().getContainerFilters();
...
for( Filter filter : filters )
{
	grid.getContainerDataSource().addContainerFilter(filter);
}

Obviously, the above code is used because there is no public setContainerFilters method. Works nicely. If I really needed to, I could call the removeAllContainerFilters method to cleanup current filters, thus mimicking a setter.