2016-08-03 64 views
2

假设我有下面的情况下设置成员对象的字段:Vaadin电网setColumn() - 作为列

有两个实体:

public class Address { 

    private City city; 
    private String street; 

    public String getStreet() { 
    return street; 
    } 

    public void setStreet(String street) { 
    this.street = street; 
    } 

    public City getCity() { 
    return city; 
    } 

    public void setCity(City city) { 
    this.city = city; 
    } 
} 

而且

public class City { 

    private String name; 

    public String getName() { 
    return name; 
    } 

    public void setName(String name) { 
    this.name = name; 
    } 
} 

我想使用BeanItemContainer在网格组件上显示街道和城市名称,应如何指定列名称? P尝试使用“street”和“city.name”,但它引发异常。

java.lang.IllegalStateException: Found at least one column in Grid that does not exist in the given container: city.name with the header "Name". Call removeAllColumns() before setContainerDataSource() if you want to reconfigure the columns based on the new container. 

回答

3

你没显示关联于电网的代码,但是你可以看到下面这样做至少2种方式(请注意,为方便起见,我已经创造了一些构造为对象):

代码:

public class MyUi extends UI { 
    @Override 
    protected void init(VaadinRequest request) { 
     // basic stuff 
     Layout content = new VerticalLayout(); 
     content.setSizeFull(); 
     setContent(content); 

     // container & grid 
     BeanItemContainer<Address> container = new BeanItemContainer<>(Address.class); 
     Grid grid = new Grid(container); 

     // 1) either manually add nested properties and hide the actual inner bean 
     container.addNestedContainerProperty("city.name"); 
     grid.getColumn("city.name").setHeaderCaption("City"); 
     grid.setColumns("street", "city.name"); // hide bean column 

     // 2) or make the container create nested properties for your inner beans 
     container.addNestedContainerBean("city"); 
     grid.getColumn("city.name").setHeaderCaption("City"); 

     // create some dummy data to populate the grid 
     City city = new City("There"); 
     Address address = new Address(city, "Here"); 
     container.addItem(address); 
     content.addComponent(grid); 
    } 
} 

结果:

Sample