Vaadin 8 Grid filtering

The Vaadin 8 Grid documentation mentions use of filtering, but fails to include any examples! In fact, I’ve noticed a couple of places where features are mentioned but there is no mention of how to implement or an example. IMHO - the documentation for Vaadin 8 is pretty poor, lacking in solid examples and contains mistakes.

For example, the Filtering section in
https://vaadin.com/docs/-/part/framework/datamodel/datamodel-providers.html#datamodel.dataproviders
mentions grid.setDataProvider(johnPersons);
however there is no JohnPersons!!

How can we implement SIMPLE grid column based filtering without using a DataProvider?

I think that the filter is done on dataProvider, so you need a dataprovider to filter your table.
In the documentation, perhaps you just have to change johnPersons → wrapper.
If you want to filter a simple list, you can do something like that (to filter on multiple criteria:

    private TextField filterColumn1;
    private TextField filterColumn2; // you can use Combobox<String> ...
    private Grid<YourObject> yourObjectGrid;
    private ConfigurableFilterDataProvider<YourObject, Void, SerializablePredicate<YourObject>> filterYourObjectDataProvider;
     
    public Grid<YourObject> getYourObjectGrid() {
        if (yourObjectGrid == null){
            yourObjectGrid = new Grid<YourObject>();
            ListDataProvider<YourObject> dataProvider =  new ListDataProvider<YourObject>(getAllYourObjects()); // your List<YourObject>
            // filter list by custom filter
            filterYourObjectDataProvider = dataProvider.withConfigurableFilter();
            yourObjectGrid.setDataProvider(filterYourObjectDataProvider);
            // refresh data when value changed
            filterColumn1.addValueChangeListener(e -> {refreshyourObjectGrid();});
            filterColumn2.addValueChangeListener(e -> {refreshyourObjectGrid();});
            yourObjectGrid.addColumn(YourObject::getColumn1).setCaption("yourObject.column1));
            yourObjectGrid.addColumn(YourObject::getColumn2).setCaption("yourObject.column2));
        }
        return yourObjectGrid;
    }
    
    private void refreshyourObjectGrid(){
        filterYourObjectDataProvider.setFilter(filterYourObjectGrid());
        yourObjectGrid.getDataProvider().refreshAll();
    }
    
    private SerializablePredicate<YourObject> filterYourObjectGrid(){
        SerializablePredicate<YourObject> columnPredicate = null;
        columnPredicate = yourObject -> (yourObject.getColumn1().equals(filterColumn1.getValue()) && yourObject.getColumn2().equals(filterColumn2.getValue()));
        return columnPredicate;
    }

Thank you for your suggestion. I took your comments and created FilterableGrid which wraps a standard Grid and allows me to set text filters for string based fields.

How getallYourObjects() typically looks like?

Jean-Christophe Gueriaud:

It helped me to resolve the multiple filteration search of grid

yourObject -> (yourObject.getColumn1().equals(filterColumn1.getValue()) && yourObject.getColumn2().equals(filterColumn2.getValue()));

This POC did the work i.e., using the AND operator between columns.

But, here comes the complexity what if there are ten or more columns in the grid. This line needs to operate AND on that many columns and this will lead the (N*M) comparisons on a single change event of textfield.

Any help would be greatly appreciated.
Thanks.

Best,
Khushboo

Hi,

My example was for simple filtering.

In a real world, you may have:

  • a lot of columns (and filters)
  • a grid with lazy data provider

When I filter an Object (for example Person) I like to create a bean PersonFilter (with all my filter column).
You can bind your filter fields to your bean PersonFilter (with a vaadin binder).

Then you can build a Predicate or your SQL query.

It’s quite hard to make the “right” answer. Perhaps if you have an example of your code (or a use case) I may answer :slight_smile:
Perhaps you can use an add-on (i think there are some add-on to filter a grid) or querydsl java libraries.

In my opinion, if you change 1 filter value then you should query your database with a new filter.