Konrad Höffner
Konrad Höffner

Reputation: 12207

Customizing Vaadin bean property to table cell transformation

We have a BeanItemContainer we display as a Vaadin table which works very well. The only problem is that one of the bean properties is an URL and we want it to be a link.

Adding "a href=..." to the url in the setURL()-function works but

  1. it does not strike me as very elegant code
  2. other callers now have to strip the tags from it after using the get method.

Adding a click listener to the table works as well but

  1. it may open a window instead of a tab
  2. it may be prevented by a popup blocker

Is there a way to control the process Vaadin transforms bean propery values to table cells?

Upvotes: 4

Views: 2306

Answers (1)

Charles Anthony
Charles Anthony

Reputation: 3155

Use a ColumnGenerator on the table, and generate a Link component, e.g.

table.addGeneratedColumn("link", new Table.ColumnGenerator() {
  @Override
  public Object generateCell(Table source, Object itemId, Object columnId) {
    Item item = source.getItem(itemId);
    String columnValue = String.valueOf(item.getItemProperty(columnId).getValue());

    String urlValue = columnValue; // Assume columnValue contains full url including protocol, e.g. http://stackoverflow.com
    String urlDescription = columnValue; // Description is the same as the
    return new Link(urlDescription, new ExternalResource(urlValue));  
  }
})

See documentation and javadoc for more details

Upvotes: 6

Related Questions