在 vaadin 网格的一个单元格中显示多个组件
displaying multiple components within a cell of vaadin grid
有什么方法可以将多个组件(如 CheckBox、Label 等)显示到 Vaadin 网格的单个单元格中?网格显示由 GeneratedPropertyContainer 动态填充的数据。
提前致谢。
如果您搜索 Vaadin directory you will find a few extensions, such as ComponentRenderer add-on,它可以让您轻松获得此类功能。您可以在下面看到基于上述附加组件的 Vaadin v7.7.3 和 v1.0.2 的代码示例。请记得更新并重新编译您的小部件集。
public class GridWithMultiComponentRenderer extends VerticalLayout {
private static final String BUTTONS_ID = "buttons";
public GridWithMultiComponentRenderer() {
// basic grid setup
Grid grid = new Grid(new BeanItemContainer<>(Person.class));
grid.setSizeFull();
addComponent(grid);
// add the decorator
ComponentGridDecorator<Person> gridDecorator = new ComponentGridDecorator<>(grid, Person.class);
// generate the column which will display the components
gridDecorator.addComponentColumn(BUTTONS_ID, person -> new HorizontalLayout(
new Button("Get name", event -> Notification.show(person.getName())),
new Button("Get surname", event -> Notification.show(person.getSurname())),
new Button("Get age", event -> Notification.show(String.valueOf(person.getAge())))
));
// set column order
grid.setColumns("name", "surname", "age", BUTTONS_ID);
// add some dummy data
Random random = new Random();
for (int i = 0; i < 10; i++) {
gridDecorator.add(new Person("Name " + i, "Surname " + i, random.nextInt(99) + 1));
}
}
// POJO for simple binding
public static class Person {
private String name;
private String surname;
private int age;
public Person(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
结果:
有什么方法可以将多个组件(如 CheckBox、Label 等)显示到 Vaadin 网格的单个单元格中?网格显示由 GeneratedPropertyContainer 动态填充的数据。
提前致谢。
如果您搜索 Vaadin directory you will find a few extensions, such as ComponentRenderer add-on,它可以让您轻松获得此类功能。您可以在下面看到基于上述附加组件的 Vaadin v7.7.3 和 v1.0.2 的代码示例。请记得更新并重新编译您的小部件集。
public class GridWithMultiComponentRenderer extends VerticalLayout {
private static final String BUTTONS_ID = "buttons";
public GridWithMultiComponentRenderer() {
// basic grid setup
Grid grid = new Grid(new BeanItemContainer<>(Person.class));
grid.setSizeFull();
addComponent(grid);
// add the decorator
ComponentGridDecorator<Person> gridDecorator = new ComponentGridDecorator<>(grid, Person.class);
// generate the column which will display the components
gridDecorator.addComponentColumn(BUTTONS_ID, person -> new HorizontalLayout(
new Button("Get name", event -> Notification.show(person.getName())),
new Button("Get surname", event -> Notification.show(person.getSurname())),
new Button("Get age", event -> Notification.show(String.valueOf(person.getAge())))
));
// set column order
grid.setColumns("name", "surname", "age", BUTTONS_ID);
// add some dummy data
Random random = new Random();
for (int i = 0; i < 10; i++) {
gridDecorator.add(new Person("Name " + i, "Surname " + i, random.nextInt(99) + 1));
}
}
// POJO for simple binding
public static class Person {
private String name;
private String surname;
private int age;
public Person(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
结果: