Reputation:
I have BeanItemContainer which I would like to bind as a data source in my table. BeanItemContainer has inside bean within is an array. How could I do this ?
Below is my bean (POJO) within is nested the array:
private EmployeesArray[] employeesArray;
public EmployeesArray[] getEmployeesArray() {
return employeesArray;
}
public void setEmployeesArray (EmployeesArray[] employeesArray) {
this.employeesArray = employeesArray;
}
public EmployeesListPOJO(){
}
And here is the nested array:
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(){
}
And here's my attempt to bind it to the table:
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");`
I would like to have two column headers in my table: "First name" and "Last name"
which are associated to fields firstname
and lastname
in the array.
Upvotes: 0
Views: 435
Reputation: 15518
I'm not trying to be condescending, but I believe there are a few issues with this code, so let's tackle them 1 by 1:
The EmployeesArray
class should probably be named Employee
since it holds employee data, and your array variable would become Employee[] employees
.
Since you want to display employees in your table, you should have a corresponding container: BeanItemContainer<Employee> container = new BeanItemContainer<>(Employee.class);
If EmployeesListPOJO
can not be changed, you'll then need to iterate over the array and add each employee bean to the container with container.addBean(employees[i]);
. Or, if you can change it to a collection instead you can just simply call container.addAll(employees);
. Also probably EmployeesListPOJO
should have a friendlier name but I don't know the whole context so it's up to you
Putting it all together:
// 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());
With:
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+"]";
}
}
Should result in
P.S. 1: You can also find more information in the Vaadin documentation
P.S. 2: If you're working with the latest Vaadin versions (7.4.0 +) and depending on your needs, I suggest taking a look at the newer Grid component which is supposed to be more flexible than its older counterpart the table. You can see its features in action on the sampler site
Cheers
Upvotes: 1