将BeanItemContainer绑定到表中的数组

时间:2015-04-09 14:55:25

标签: java arrays binding vaadin

我有BeanItemContainer,我想在我的表中绑定为数据源。 BeanItemContainer内部bean是一个数组。 我怎么能这样做?

下面是我的bean(POJO)嵌套数组:

private EmployeesArray[] employeesArray;

public EmployeesArray[] getEmployeesArray() {
    return employeesArray;
}

public void setEmployeesArray (EmployeesArray[] employeesArray) {
    this.employeesArray = employeesArray;
}

public EmployeesListPOJO(){ 
}

这是嵌套数组:

private String lastname;
private String firstname;

public String getLastname () {
    return lastname;
}

public void setLastname (String lastname) {
    this.lastname = lastname;
}

public String getFirstname () {
    return firstname;
}

public void setFirstname (String firstname) {
    this.firstname = firstname;
}

@Override
public String toString() {
    return "ClassPojo [lastname = "+lastname+", firstname = "+firstname+"]";
}

public EmployeesArray(){
}

这是我尝试将其绑定到桌子上的方法:

BeanItemContainer<EmployeesListPOJO> container = new BeanItemContainer<EmployeesListPOJO>(EmployeesListPOJO.class);

Table table = new Table();
table.setContainerDataSource(container);
table.setVisibleColumns(new String[] {"employeesArray"});
table.setColumnHeader("employeesArray", "First name", "Last name");`

我想在我的表格中有两个列标题:“名字”和“姓氏” 与数组中的字段firstnamelastname相关联。

1 个答案:

答案 0 :(得分:1)

我不是试图屈尊俯就,但我相信这段代码存在一些问题,所以让我们逐一解决它们:

  1. EmployeesArray类可能应该命名为Employee,因为它包含员工数据,而您的数组变量将变为Employee[] employees

  2. 由于您希望在表格中显示员工,因此您应该拥有相应的容器:BeanItemContainer<Employee> container = new BeanItemContainer<>(Employee.class);

  3. 如果无法更改EmployeesListPOJO,则需要迭代数组并将每个employee bean添加到具有container.addBean(employees[i]);的容器中。或者,如果您可以将其更改为集合,则只需拨打container.addAll(employees);即可。也许EmployeesListPOJO可能有一个更友好的名字,但我不知道整个上下文所以由你决定

  4. 全部放在一起:

        // create the table
        Table table = new Table()
    
        // and the appropriate data holder 
        BeanItemContainer<Employee> container = new BeanItemContainer<>(Employee.class);
        table.setContainerDataSource(container);
    
        // set desired headers
        table.setColumnHeaders("First name", "Last name");
    
        // get all of the employees to display
        employees = employeesListPOJO.getEmployeesArray();
    
        // add each employee to the container
        for (int i = 0; i < employees.length; i++) {
            container.addBean(employees[i]);
        }
    
        // or if you can get a collection/list of employees
        // container.addAll(employeesListPOJO.getEmployees());
    

    使用:

    public class Employee {
    
        private String lastname;
        private String firstname;
    
        public Employee(String lastname, String firstname) {
            this.lastname = lastname;
            this.firstname = firstname;
        }
    
        public String getLastname () {
            return lastname;
        }
    
        public void setLastname (String lastname) {
            this.lastname = lastname;
        }
    
        public String getFirstname () {
            return firstname;
        }
    
        public void setFirstname (String firstname) {
            this.firstname = firstname;
        }
    
        @Override
        public String toString() {
            return "Employee [lastname = "+lastname+", firstname = "+firstname+"]";
        }
    }
    

    应该导致

    Employee table

    <强> P.S。 1:您还可以在Vaadin documentation

    中找到更多信息

    <强> P.S。 2:如果您正在使用最新的Vaadin版本(7.4.0 +)并根据您的需要,我建议您查看较新的Grid component,它应该比其更灵活比较老的对应表。您可以在sampler site

    上查看其功能

    干杯