I’m stuck and am hoping someone has some example code on how to do this. Basically I want to display a list of products using dom-repeat. Each product has its own template which is also a vaadin component. The results I’ve had so far are limited to either nothing displays, or that I get an error sub-templates are not supported.
Then I dynamically set the templates setId() to the same as the tagId value.
How do I code the product-card-template so that the above works? Because the dom-repeat is inside a bootstrap container, I can’t use vaadin-grid or vaadin-board.
Thanks Haijian, the closing tag sorted out half the problem. The template now displays, but I don’t know how to bind the data to the Java companion class. I tried the @Uses but this results in the injection not supported in sub-templates exception.
StreamResource resource = new StreamResource("image.webp", imagesource);
Image img = new Image();
img.setSrc(resource);
img.setId(new String("productimage" + i));
product.setTagId(img.getId().get());
product.setImageSource(img.getSrc());
How do I bind the image to the html? I can’t see how the above with img.setId() would perform the binding. Perhaps another way to ask the question is how to create elements dynamically?
Sorry, I’m a bit confused of what you are trying to achieve. Maybe I can understand it better when I see the whole code. Is it an open source project? Can I check it from Gitlab for somewhere?
Thanks for your offer to help. Unfortunately the code is proprietary.
What I am trying to achieve is a common pattern: I need to be able to repeat a number of vaadin components in a dom-repeat. All the examples in the Vaadin documentation show a dom-repeat with plain text like the below example (taken from the Vaadin docs section on “Creating Template Contents Dynamically Based on a List of Items”):
Here the repeated content is just plain text, i.e. “{{item.email}}”. I want to be able to repeat a Vaadin component, e.g. a Button, or link or Image or some component, and bind its data and events to the Java code. So in the example above, I would like to also have a image showing the photo of the employee, with the image content coming from the backend DB. And then also have the Image clickable to show the detailed employee page or something like that.
I would guess that this is a common use case, I just can’t find any documentation on how this can be done. Hope this makes sense.
Thanks for the links Haijian, they were very useful and solved 90% of my problems. It seems the trick is to declare the component in html and not in Java and then just register for the events in the Java code.
What this does not yet solve is how to generate dynamic content, for e.g. an image from a DB, inside the dom-repeat. In this case the dynamic content must be declared in Java.
In summary, the idea is that you leave some slot in your template for dynamic content, and on server side you can then create those components dynamically and use Element api to append the elements of your dynamic components as children elements.