Package com.vaadin.flow.data.renderer
Class LitRenderer<SOURCE>
java.lang.Object
com.vaadin.flow.data.renderer.Renderer<SOURCE>
com.vaadin.flow.data.renderer.LitRenderer<SOURCE>
- Type Parameters:
SOURCE
- the type of the model object used inside the template expression
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ComponentRenderer
LitRenderer is a
Renderer
that uses a Lit-based template literal to
render given model objects in the components that support the JS renderer
functions API. Mainly it's intended for use with Grid
,
ComboBox
and VirtualList
, but is not limited to these.- Since:
- 22.0.
- Author:
- Vaadin Ltd
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionprotected String
Returns the Lit template expression used to render items.Map<String,
ValueProvider<SOURCE, ?>> Gets the property mapped toValueProvider
s in this renderer.static <SOURCE> LitRenderer<SOURCE>
Creates a new LitRenderer based on the provided template expression.render
(Element container, DataKeyMapper<SOURCE> keyMapper, String rendererName) Registers a renderer function with the given name to the given container element.withFunction
(String functionName, SerializableBiConsumer<SOURCE, elemental.json.JsonArray> handler) Adds a function that can be called from within the template expression.withFunction
(String functionName, SerializableConsumer<SOURCE> handler) Adds a function that can be called from within the template expression.withProperty
(String property, ValueProvider<SOURCE, ?> provider) Makes a property available to the template expression.
-
Method Details
-
of
Creates a new LitRenderer based on the provided template expression. The expression accepts content that is allowed inside JS template literals, and works with the Lit data binding syntax.The template expression has access to:
item
the model item being renderedindex
the index of the current item (when rendering a list)item.property
any property of the model item exposed viawithProperty(String, ValueProvider)
- any function exposed via
withFunction(String, SerializableConsumer)
Examples:
// Prints the `name` property of a person LitRenderer.<Person> of("<div>Name: ${item.name}</div>") .withProperty("name", Person::getName); // Prints the index of the item inside a repeating list LitRenderer.of("${index}");
- Type Parameters:
SOURCE
- the type of the input object used inside the template- Parameters:
templateExpression
- the template expression used to render items, notnull
- Returns:
- an initialized LitRenderer
- See Also:
-
render
public Rendering<SOURCE> render(Element container, DataKeyMapper<SOURCE> keyMapper, String rendererName) Description copied from class:Renderer
Registers a renderer function with the given name to the given container element. Creates the setup to handle rendering of individual data items as requested by the renderer function invocation.- Specified by:
render
in classRenderer<SOURCE>
- Parameters:
container
- the element which accepts the renderer function on the client.keyMapper
- mapper used internally to fetch items by key and to provide keys for given items.rendererName
- name of the renderer function the container element accepts- Returns:
- the context of the rendering, that can be used by the components to provide extra customization
-
getTemplateExpression
Returns the Lit template expression used to render items.- Returns:
- the template expression
-
withProperty
Makes a property available to the template expression. Each property is referenced inside the template by using the${item.property}
syntax.Examples:
// Regular property LitRenderer.<Person> of("<div>Name: ${item.name}</div>") .withProperty("name", Person::getName); // Property that uses a bean. Note that in this case the entire "Address" object will be sent to the template. // Note that even properties of the bean which are not used in the template are sent to the client, so use // this feature with caution. LitRenderer.<Person> of("<span>Street: ${item.address.street}</span>") .withProperty("address", Person::getAddress); // In this case only the street field inside the Address object is sent LitRenderer.<Person> of("<span>Street: ${item.street}</span>") .withProperty("street", person -> person.getAddress().getStreet());
JsonSerializer
are valid types for the LitRenderer.- Parameters:
property
- the name of the property used inside the template expression, notnull
provider
- aValueProvider
that provides the actual value for the property, notnull
- Returns:
- this instance for method chaining
-
withFunction
Adds a function that can be called from within the template expression.Examples:
// Standard event LitRenderer.of("<button @click=${handleClick}>Click me</button>") .withFunction("handleClick", object -> doSomething());
- Parameters:
functionName
- the name of the function used inside the template expression, must be alphanumeric and notnull
, must not be one of the JavaScript reserved words (https://www.w3schools.com/js/js_reserved.asp)handler
- the handler executed when the function is called, notnull
- Returns:
- this instance for method chaining
- See Also:
-
withFunction
public LitRenderer<SOURCE> withFunction(String functionName, SerializableBiConsumer<SOURCE, elemental.json.JsonArray> handler) Adds a function that can be called from within the template expression. The function accepts arguments that can be consumed by the given handler.Examples:
// Standard event LitRenderer.of("<button @click=${handleClick}>Click me</button>") .withFunction("handleClick", item -> doSomething()); // Function invocation with arguments LitRenderer.of("<input @keypress=${(e) => handleKeyPress(e.key)}>") .withFunction("handleKeyPress", (item, args) -> { System.out.println("Pressed key: " + args.getString(0)); });
- Parameters:
functionName
- the name of the function used inside the template expression, must be alphanumeric and notnull
, must not be one of the JavaScript reserved words (https://www.w3schools.com/js/js_reserved.asp)handler
- the handler executed when the function is called, notnull
- Returns:
- this instance for method chaining
- See Also:
-
getValueProviders
Gets the property mapped toValueProvider
s in this renderer. The returned map is immutable.- Returns:
- the mapped properties, never
null
-