Using Vaadin 22+
I'm trying to display some dynamic html in a Vaadin Grid, using a LitRenderer.
This could previously be achieved using the now-deprecated TemplateRenderer using this hack
var templateRenderer = TemplateRenderer.<Event>of("""
<div>
<ul inner-h-t-m-l="[[item.html]]">
</ul>
</div>
""").withProperty("html", item -> {
String listItems = "";
for (EventItem eventItem : item.getEventItems()) {
listItems += "<li>"+eventItem.getValue()+"</li>";
}
return listItems;
});
/*
Produces html like
<div>
<ul>
<li>Thing 1</li>
<li>Thing 3</li>
<li>Thing 845</li>
</ul>
</div>
*/
The important part here was setting the innerHTML of the wrapping element.
Is there a way to do this using the LitRenderer? I see that Lit-Template itself has the unsafeHTML directive for just this kind of purpose. But I've had no luck trying to get it or another method working.
Thanks
Updated: 2021.3.2 to fix the example code
You can use a lit renderer without unsafeHTML to render your example:
If you really want to display custom html, you can use a Component Renderer:
And if you don't want to use a Component Renderer you can create a custom lit component with unsafeHTML:
And you can use it in Java:
EDIT: There is also a fourth solution:
Here is a full example with the 4 ways to display your example without the TemplateRenderer:
There is a ticket on github with more solutions: https://github.com/vaadin/flow-components/issues/2753