Filtering Grid (Vaadin 8)

We saw how to work with Grid now moving on to Example of How to Filter a Grid.

Here is the Code Snippet For the Gird Filter

We  have our  File we have created in the previous Post in which we had a Grid and ListDataProvider in it keep it as it is, Create a new Java File say FilterGrid.java. and It write the folowing Code.


public class FilterGrid extends CustomComponent  {

   private VerticalLayout vlytbase;

    private TextField search;                              //for searching the Respctive Text
    TestGrid tstgrid = new TestGrid();
    String input;

    public TestFilter() {


        vlytbase = new VerticalLayout();
        vlytbase.setMargin(true);
        vlytbase.setSizeFull();
        search = new TextField();
        search.setPlaceholder("Search");
        vlytbase.addComponents(search, tstgrid);
        vlytbase.setSpacing(true);
        vlytbase.setMargin(true);
        vlytbase.setSizeFull();
        setCompositionRoot(vlytbase);
        addListener();
    }

    private void addListener() {
        search.addValueChangeListener(e -> CheckHasValue(e));   //Listener

    }

    private void CheckHasValue(ValueChangeEvent<String> e) {     //casting provider
        input = e.getValue();
            ((ListDataProvider<Company>)tstgrid.cmpygrid.getDataProvider())
                    .setFilter(p -> Casesensitivesearch(p, input));
    }

    private boolean Casesensitivesearch(Company p, String searchedToken) {
        return p.getName().toLowerCase().contains(searchedToken.toLowerCase());

}



                                         





Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another