Vaadin filters in Grid

感情迁移 提交于 2019-12-13 02:06:41

问题


i have problem with filters in Vaadin. Its easy to make filters for each column in Grid. But now i need to do one filter for all cells in Grid. I don't know how to do this.

I was trying to do this like in this example, but it doesn't worked

    Filter f  = 
new Or(new SimpleStringFilter(Columns.SEARCH.id(), "rpm-diastolic", true, false),  
new Or(new SimpleStringFilter(Columns.SEARCH.id(), "rpm-systolic", true, false)),
new Or(new SimpleStringFilter(Columns.SEARCH.id(), "rpm-weight", true, false))) ;
container.addContainerFilter(f);

回答1:


I created a solution to my case. I hope that help somebody

//In my case, I created a class that extends Grid.
public class GridExample extends Grid {   

    ...

    //Method to add a filter on grid
    public void setFilterGrid(BeanItemContainer<?> beanType) {
        //This create a HeaderRow to add filter fields
        HeaderRow filterRow = this.appendHeaderRow();
        for (Column column : getColumns()) {
            //For each column from the grid
            HeaderCell cellFilter = filterRow.getCell(column.getPropertyId());
            //Add a textfield
            cellFilter.setComponent(createFieldFilter(beanType, column));       
        }       
    } 

   //This create a TextField to filter the information 
   private TextField createFieldFilter(final BeanItemContainer<?> container, final Column column) {
       TextField filter = new TextField();
       filter.setImmediate(true);
       filter.addTextChangeListener(new TextChangeListener(){   
           @Override
           public void textChange(TextChangeEvent event) {
               String newValue = event.getText();
               //Remove the previous filter
               container.removeContainerFilters(column.getPropertyId());
               if (newValue != null && !newValue.isEmpty()) {
                    //Filter the information
                    container.addContainerFilter(new SimpleStringFilter(column.getPropertyId(), newValue, true, false));
                }
                recalculateColumnWidths();
           }
       });     
       return filter;
   }  
}

For more information check here: http://krishnasjavaworld.blogspot.com.br/2015/04/step-by-step-vaadin-grid-part-3filters.html



来源:https://stackoverflow.com/questions/33456475/vaadin-filters-in-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!