Reputation: 155
My question regards binding components to data in Vaadin. It is possible to bind nested properties when you have bean with 1:1 relationship using addnestedpcontainerproperty.
Is it possible to bind property with One:Many relationship. For example having java class
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;
}
I would like to display Users as a table, for example: each User object from Users list as row, prop map's keys as table columns, and map's values as cells.
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);
}
}
I would like this to be displayed as
| p1 | p2 |
----------------------
| val_b_1 | val_b_2 |
| val_a_1 | val_a_2 |
Assumption is that each user in User's list will have the same values as keys. So the number of columns is always the (for each user in user list, for different list of user it may differ)
Upvotes: 1
Views: 3392
Reputation: 17895
Please check Chapter 9. Binding Components to Data of Vaadin book.
In general you did everything right, but your class Users have to implement Container interface according to Vaading data model:
Did you notice Table constructor?
// 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.
You can use Array container or any other implementation to save some time.
Please also check Model view controller pattern description.
Happy coding.
Upvotes: 1