Search code examples
vaadinvaadin-flowvaadin-grid

Vaadin one to many and Grid binding.Property not found


There is the Customer entity which has a one to many relationship to entity Address,so there is a List<Address> getAddress in Customer.Address has an Email field.

So for instance to get an email from the first customer you do customer.getAddress.get(0).getEmail

When I bind to a Customer.class grid,setColumns("customerid","address.email") it throws an exception Property not found.

If I do addColumn(customer->customer.getAddress.get(0).get email).setHeader(email) it does display the email.

But that's for the first customer.How to do the same for all customers?


Solution

  • The default presentation for a List is toString(). You need to create a Renderer to display the list nicely. For better performance you can create a LitRenderer. See also the Lit documentation for repeating templates.

    grid.addColumn(LitRenderer.<Customer>of("""
            ${item.address.map((adr) => 
                html`<span>${adr.email}</span><br />`
            )}
            """).withProperty("address", Customer::getAddress));
    

    Would look like this:

    enter image description here

    Maybe replace the span element with <a href="mailto:${adr.email}">${adr.email}</a> for better UX.