在vaadin网格的单元格中显示多个组件

时间:2016-10-07 13:21:23

标签: grid vaadin7

有什么方法可以将多个组件(如CheckBox,Label等)显示到Vaadin Grid的单个单元格中? Grid显示由GeneratedPropertyContainer动态填充的数据。

提前致谢。

1 个答案:

答案 0 :(得分:3)

如果你搜索Vaadin directory,你会发现一些扩展程序,例如ComponentRenderer add-on,这些扩展程序允许您以一种无痛的方式拥有此类功能。您可以在下面看到基于前面提到的附加组件的Vaadin v7.7.3和v1.0.2的代码示例。请记住更新并重新编译您的widgetset。

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;
        }
    }
}

结果:

multiple components in grid column