javavaadinvaadin7vaadin-grid

How to add nested complex property with filtering to Vaadin 7 Grid?


I doing a Vaadin (7.10) app, and in some view, I would need to add an "special" nested property into the container. For the sake of the application, we're using BeanItemContainer and Grid. I have some class that stores a List of another POJO(s), and I would need to use one property inside those second POJO to filter the grid. A basic example of the config would be:

public class A {
    private String property1;
    private String property2;
    //There are too getters and setters for this two properties
}

public class B { //This class stores a list of As
    private String name;
    private List<A> list;
    //Getters and setters too
}

These are my two basic classes, which I use to store data. The Vaadin code to show data would be:

Grid grid = new Grid();
BeanItemContainer<B> container = new BeanItemContainer<>(B.class);

//////////////
container.addNestedContainerProperty("list.property1"); 
//This OBVIOUSLY doesn't work, because property1 is not part of List
/////////////

grid.setColumns("name");
grid.setContainerDataSource(container);

So, my question is:
Is possible to show in Grid this property1 without changing from BeanItemContainer?


Solution

  • This seems like a job for a generated property.

    BUT: it still requires changing from BeanItemContainer or more detailed it requires wrapping it. Anyway it was not a problem when i did it (years ago).

    For this you need GeneratedPropertyContainer. It is a wrapper for other containers that need generated properties added.

    GeneratedPropertyContainer container =
        new GeneratedPropertyContainer(yourBeanItemContainer);
    

    Add generated properties to that container

    container.addGeneratedProperty("property1"
       ,new PropertyValueGenerator<String>() { ... });
    

    Above mentiond PropertyValueGenerator should then return String that you possibly choose from some pojo A.

    Vaadin API for PropertyValueGenerator

    Update considering filtering: PropertyValueGenerator overrides method

    modifyFilter(Container.Filter filter)
    

    Return an updated filter that should be compatible with the underlying container.

    For example: if you just pick the first pojo A from list and its property1 the you could implement this to make the filter to filter out all Ba whose first As property1 does not match.