Vaadin过滤网格

时间:2015-10-31 20:49:21

标签: java vaadin vaadin7

我在Vaadin中遇到过滤器问题。它很容易为Grid中的每个列生成过滤器。但现在我需要为Grid中的所有单元格做一个过滤器。 我不知道该怎么做。

我试图像在这个例子中那样做,但它没有工作

    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 个答案:

答案 0 :(得分:3)

我为我的案子创建了一个解决方案。我希望帮助某人

//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;
   }  
}

有关更多信息,请点击此处: http://krishnasjavaworld.blogspot.com.br/2015/04/step-by-step-vaadin-grid-part-3filters.html