java - 绑定(bind)一:Many data to component in Vaadin

标签 java vaadin

我的问题是关于将组件绑定(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 DataVaadin book .

总的来说,你做的一切都是正确的,但是你的类Users必须根据Vaading数据模型实现Container接口(interface):

enter image description here

你注意到表构造函数了吗?

// 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/

相关文章:

java - 如何读取文件 : Properties > Details > Content > Pages e. g。对于word文档

java - 将HL7 v2.x转换为RDF格式

java - 在具有不同 Controller 的多个端口上运行 Spring Boot 应用程序

vaadin - 如何在紧凑的 Vaadin 14/Flow Grid 中添加按钮

java - 集成 Vaadin 7 和 EJB 的最佳方式或如何制作服务层?

vaadin - 在Vaadin 14的Uploader中,如何清除之前上传的文件?

java - Android向php发送数据并接收问题nullpointerException

java - 在 IntelliJ Idea 中将常量导入替换为静态导入

java - vaadin touchkit 示例应用程序 -- 没有 ivy.xml

java - Vaadin 14 网格,其中复选框作为渲染组件