Search code examples
javavaadin10

Why is vaadin-grid not showing links (anchors)?


I am trying to have a table column with links in my Vaadin 10, Spring-boot application.

I am displaying data in a grid as follows:

Grid<Person> grid = new Grid<>();
UI.getCurrent().getRouter();

grid.addColumn(
   p -> new Anchor(UI.getCurrent().getRouter().getUrl(
                    PersonView.class, p.getName()),
                   p.getName())).setHeader("Name");
grid.addColumn(p ->   
   p.getProjects().size()).setHeader("#Projects");

grid.setItems(repo.findAll());
add(grid);
setSizeFull();

But the grid column with the links is only displaying text like Anchor@XXXXXXX


Solution

  • The problem is, that the column data is interpreted as value and not as component. You need to tell the grid, that it should use a renderer, which displays components and does not wrap them.

    This can be done using a ComponentRenderer.

    If you modify your column as follows, it works:

    grid.addColumn(new ComponentRenderer<>(p -> new Anchor(UI.getCurrent().getRouter().getUrl(
                PersonView.class, p.getName()), p.getName()))).setHeader("Name");