假设我有以下情况:
有两个实体:
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.
答案 0 :(得分: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);
}
}
<强>结果:强>