Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin 8 Grid filtering

Ravi Cheema
5 years ago Feb 27, 2017 3:05pm

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?

 

Jean-Christophe Gueriaud
5 years ago Feb 27, 2017 4:43pm

ravic: 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;
    }

 

Ravi Cheema
5 years ago Feb 28, 2017 4:02pm

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.

Alex Brown
4 years ago Oct 10, 2017 12:03pm
Khushboo kalia
3 years ago Sep 07, 2018 12:20pm
Jean-Christophe Gueriaud
3 years ago Sep 07, 2018 1:28pm