我的问题是关于将组件绑定(bind)到 Vaadin 中的数据。 当您使用 addnestedpcontainerproperty 拥有具有 1:1 关系的 bean 时,可以绑定(bind)嵌套属性。
是否可以将属性与一对多关系绑定(bind)。例如有java类
public class User {
private String name;
private Map<String, String> prop;
public User() { ... }
public addProp(String column, String value) {
prop.put(column, value);
}
}
public class Users {
private List<User> users;
}
我想将用户显示为表格,例如:用户列表中的每个用户对象作为行, Prop 映射的键作为表列,映射的值作为单元格。
public class Users {
private List<User> users;
public Users() {
User user1 = new User("first user");
user1.addProp("p1", "val_b_1");
user1.addProp("p2", "val_b_2");
User user2 = new User("second_user");
user2.addProp("p1", "val_a_1");
user2.addProp("p2", "val_a_2");
users = new HashMap;
users.add(user1);
users.add(user2);
}
}
我希望它显示为
| p1 | p2 |
----------------------
| val_b_1 | val_b_2 |
| val_a_1 | val_a_2 |
假设用户列表中的每个用户都具有与键相同的值。所以列数始终是(对于用户列表中的每个用户,对于不同的用户列表可能会有所不同)
最佳答案
请查看Chapter 9. Binding Components to Data的Vaadin book .
总的来说,你做的一切都是正确的,但是你的类Users必须根据Vaading数据模型实现Container接口(interface):
你注意到表构造函数了吗?
// Creates a new table with caption and connect it to a Container.
Table(String caption, Container dataSource)
Basically the Container is a set of Items, but it imposes certain constraints on its contents. These constraints state the following:
- All Items in the Container must have the same number of Properties.
- All Items in the Container must have the same Property ID's (see
- Item.getItemPropertyIds()). All Properties in the Items corresponding to the same Property ID must have the same data type. All Items within a container are uniquely identified by their non-null IDs.
您可以使用Array container或任何其他实现来节省一些时间。
另请检查Model view controller模式描述。
祝你编码愉快。
关于java - 绑定(bind)一:Many data to component in Vaadin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13510013/