You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/components/grid/renderers.adoc
+27Lines changed: 27 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -217,6 +217,33 @@ grid.addColumn(
217
217
() -> VaadinIcon.ARROW_LEFT.create()));
218
218
----
219
219
220
+
Component renderers allow providing an optional update function that can be used to optimize rendering.
221
+
Without an update function, component renderers recreate components for each item whenever the grid needs to be refreshed.
222
+
This has several downsides:
223
+
224
+
* It results in a large number of DOM changes in the browser as old elements are removed and new ones are added
225
+
* It results in a larger payload that needs to be sent to the browser
226
+
* Components lose their client-side state, for example, input fields lose their focus or entered text that has not been committed
227
+
228
+
By providing an update function, the grid can reuse existing components and only update their state. This results in fewer DOM changes, smaller payloads, and better user experience.
229
+
230
+
The following example shows how to provide an update function to a component renderer:
231
+
232
+
[source,java]
233
+
----
234
+
grid.addColumn(new ComponentRenderer<>(item -> {
235
+
// Create the initial component using item name as text
236
+
return new Badge(item.getName());
237
+
}, (component, item) -> {
238
+
// Update the component text when the item changes.
239
+
// Cast is necessary as component renderer is typed
240
+
// so that it allows the update function to return
241
+
// a different component type if desired.
242
+
((Badge) component).setText(item.getName());
243
+
return component;
244
+
}));
245
+
----
246
+
220
247
You can create complex content for the grid cells by using the component APIs. This example is using [classname]`ComponentRenderer` to create complex content that listens for events and wraps multiple components in layouts:
0 commit comments