Search code examples
vaadinvaadin7

How display several Vaadin properties in a single table cell?


I have 3 properties of a java class which I require to display below each other in a table cell.

The 3 properties are added to a container which is bound to a table - however, this leads to each property occupying a different cell in the same row.

e.g.
Container container = new IndexedContainer();
container.addContainerProperty("property1", String.class, "dftvalue1");
container.addContainerProperty("property2", String.class, "dftvalue2");
container.addContainerProperty("property3", String.class, "dftvalue3");

container.getContainerProperty(itemId, "property1").setValue("value1");
...
Table table = new Table("My Table");
table.setContainerDataSource(container);

I require to display as below.


          |  property1   |
col1      |  property2   |
          |  property3   |

Please can you inform/advise how this can be done? Thank you Steve.


Solution

  • You would implement a Table.ColumnGenerator for this. Then create the content for the cell there. The content can be "any" vaadin Component.

    Either use e.g. Label with HTML content and concat your props with <br/> (be aware of XSS!). Or you can also create a vertical layout and add a label for each property.

    Something like this:

    def c = new BeanItemContainer<SomeBean>(SomeBean)
    c.addAll([
            new SomeBean(prop1: "prop11", prop2: "prop12", prop3: "prop13"),
            new SomeBean(prop1: "prop21", prop2: "prop22", prop3: "prop23"),
            new SomeBean(prop1: "prop31", prop2: "prop32", prop3: "prop33"),
    ])
    setContent(new Table().with{
        setContainerDataSource(c, [])
        addGeneratedColumn("combined", {Table source, Object itemId, Object columnId ->
            (source.getItem(itemId) as BeanItem<SomeBean>).bean.with{
                // UNSAFE CODE! DON'T USE VERBATIM
                new Label("$it.prop1<br />$it.prop2<br />$it.prop3", ContentMode.HTML)
            }
        })
        setVisibleColumns(["combined"].toArray())
        it
    })