SOURCE
- the type of the model object used inside the template expression@JsModule(value="./lit-renderer.ts") public class LitRenderer<SOURCE> extends Renderer<SOURCE>
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.of(String)
,
https://lit.dev/docs/templates/overview/,
<vaadin-combo-box>.renderer
,
Serialized FormModifier and Type | Method and Description |
---|---|
Map<String,SerializableConsumer<SOURCE>> |
getEventHandlers()
Deprecated.
LitRenderer doesn't support getting the event handlers. Don't
use.
|
static <SOURCE> LitRenderer<SOURCE> |
of(String templateExpression)
Creates a new LitRenderer based on the provided template expression.
|
Rendering<SOURCE> |
render(Element container,
DataKeyMapper<SOURCE> keyMapper)
Sets up rendering of model objects inside a given
Element container element. |
Rendering<SOURCE> |
render(Element container,
DataKeyMapper<SOURCE> keyMapper,
Element contentTemplate)
Deprecated.
LitRenderer doesn't support
<template> elements.
Don't use. |
Rendering<SOURCE> |
render(Element container,
DataKeyMapper<SOURCE> keyMapper,
String rendererName)
Sets up rendering of model objects inside a given
Element container element. |
LitRenderer<SOURCE> |
withEventHandler(String eventHandlerName,
SerializableConsumer<SOURCE> handler)
Deprecated.
Use
withFunction(String, SerializableConsumer)
instead. |
LitRenderer<SOURCE> |
withFunction(String functionName,
SerializableBiConsumer<SOURCE,elemental.json.JsonArray> handler)
Adds a function that can be called from within the template expression.
|
LitRenderer<SOURCE> |
withFunction(String functionName,
SerializableConsumer<SOURCE> handler)
Adds a function that can be called from within the template expression.
|
LitRenderer<SOURCE> |
withProperty(String property,
ValueProvider<SOURCE,?> provider)
Makes a property available to the template expression.
|
getValueProviders, setEventHandler, setProperty
public static <SOURCE> LitRenderer<SOURCE> of(String templateExpression)
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 via
withProperty(String, ValueProvider)
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}");
SOURCE
- the type of the input object used inside the templatetemplateExpression
- the template expression used to render items, not
null
withProperty(String, ValueProvider)
,
withFunction(String, SerializableConsumer)
@Deprecated public Rendering<SOURCE> render(Element container, DataKeyMapper<SOURCE> keyMapper, Element contentTemplate)
<template>
elements.
Don't use.Renderer
<template>
element in the given container.
Subclasses of Renderer usually override this method to provide additional features.
render
in class Renderer<SOURCE>
container
- the element in which the template will be attached to, not
null
keyMapper
- mapper used internally to fetch items by key and to provide
keys for given items. It is required when either event
handlers or DataGenerator
are supportedcontentTemplate
- the <template>
element to be used for rendering in the
container, not null
@Deprecated public Map<String,SerializableConsumer<SOURCE>> getEventHandlers()
Renderer
getEventHandlers
in class Renderer<SOURCE>
null
Renderer.setEventHandler(String, SerializableConsumer)
public Rendering<SOURCE> render(Element container, DataKeyMapper<SOURCE> keyMapper)
Element container
element. The model objects are rendered using
the Lit template literal provided when creating this LitRenderer
instance, and the Vaadin-default JS renderer function name.render
in class Renderer<SOURCE>
container
- the DOM element that supports setting a renderer functionkeyMapper
- mapper used internally to fetch items by key and to provide
keys for given items. It is required when either functions or
DataGenerator
are supportedpublic Rendering<SOURCE> render(Element container, DataKeyMapper<SOURCE> keyMapper, String rendererName)
Element container
element. The model objects are rendered using
the Lit template literal provided when creating this LitRenderer
instance, and a given String rendererName
JS renderer function.container
- the DOM element that supports setting a renderer functionkeyMapper
- mapper used internally to fetch items by key and to provide
keys for given items. It is required when either functions or
DataGenerator
are supportedrendererName
- name of the renderer function the container element acceptspublic LitRenderer<SOURCE> withProperty(String property, ValueProvider<SOURCE,?> provider)
${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());
Any types supported by the JsonSerializer
are valid types for the
LitRenderer.property
- the name of the property used inside the template expression,
not null
provider
- a ValueProvider
that provides the actual value for the
property, not null
public LitRenderer<SOURCE> withEventHandler(String eventHandlerName, SerializableConsumer<SOURCE> handler)
withFunction(String, SerializableConsumer)
instead.Examples:
// Standard event
LitRenderer.of("<button @click=${handleClick}>Click me</button>")
.withEventHandler("handleClick", object -> doSomething());
The name of the event handler used in the template expression should be
the name used at the eventHandlerName parameter. This name must be a
valid JavaScript function name.eventHandlerName
- the name of the event handler used inside the template
expression, must be alphanumeric and not null
,
must not be one of the JavaScript reserved words
(https://www.w3schools.com/js/js_reserved.asp)handler
- the handler executed when the event handler is called, not
null
public LitRenderer<SOURCE> withFunction(String functionName, SerializableConsumer<SOURCE> handler)
Examples:
// Standard event
LitRenderer.of("<button @click=${handleClick}>Click me</button>")
.withFunction("handleClick", object -> doSomething());
The name of the function used in the template expression should be the
name used at the functionName parameter. This name must be a valid
JavaScript function name.functionName
- the name of the function used inside the template expression,
must be alphanumeric and not null
, 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, not
null
public LitRenderer<SOURCE> withFunction(String functionName, SerializableBiConsumer<SOURCE,elemental.json.JsonArray> 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));
});
The name of the function used in the template expression should be the
name used at the functionName parameter. This name must be a valid
Javascript function name.functionName
- the name of the function used inside the template expression,
must be alphanumeric and not null
, 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, not
null
Copyright © 2024. All rights reserved.