假设我有以下场景:
有两个实体:
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.
最佳答案
您没有显示与网格相关的代码,但是您可以在下面看到至少两种实现方式(请注意,为了方便起见,我已经为您的对象创建了一些构造函数):
代码:
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);
}
}
结果:
关于java - Vaadin Grid setColumn() - 将成员对象的字段设置为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38751249/