Skip to content

Commit 5cb4150

Browse files
authored
docs: update Grid renderer code snippets (#5045)
1 parent 33cae41 commit 5cb4150

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

articles/components/grid/renderers.adoc

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ifdef::react[]
3939
include::{root}/frontend/demo/component/grid/react/grid-content.tsx[render,tags=snippet,indent=0,group=React]
4040
----
4141
endif::[]
42+
4243
--
4344

4445

@@ -327,12 +328,14 @@ The example that follows is using the [methodname]`withProperty()` method to acc
327328

328329
[source,java]
329330
----
330-
grid.addColumn(LitRenderer.<Person>of(
331-
"<div>${item.address.street}, number " +
332-
"${item.address.number}<br>" +
333-
"<small>${item.address.postalCode}</small>" +
334-
"</div>")
335-
.withProperty("address", Person::getAddress))
331+
grid.addColumn(LitRenderer.<Person> of(
332+
"""
333+
<div>
334+
${item.address.street}, number ${item.address.number}<br>
335+
<small>${item.address.postalCode}</small>
336+
</div>
337+
""")
338+
.withProperty("address", Person::getAddress))
336339
.setHeader("Address");
337340
----
338341

@@ -345,15 +348,16 @@ The example that follows is using the [methodname]`withFunction()` method to map
345348

346349
[source,java]
347350
----
348-
grid.addColumn(LitRenderer.<Person>of(
349-
"<button @click=\"${handleUpdate}\">Update</button>" +
350-
"<button @click=\"${handleRemove}\">Remove</button>")
351+
grid.addColumn(LitRenderer.<Person> of(
352+
"""
353+
<button @click="${handleUpdate}">Update</button>
354+
<button @click="${handleRemove}">Remove</button>
355+
""")
351356
.withFunction("handleUpdate", person -> {
352357
person.setName(person.getName() + " Updated");
353358
grid.getDataProvider().refreshItem(person);
354359
}).withFunction("handleRemove", person -> {
355-
ListDataProvider<Person> dataProvider =
356-
(ListDataProvider<Person>) grid
360+
ListDataProvider<Person> dataProvider = (ListDataProvider<Person>) grid
357361
.getDataProvider();
358362
dataProvider.getItems().remove(person);
359363
dataProvider.refreshAll();
@@ -370,13 +374,16 @@ Below is an example of this:
370374

371375
[source,java]
372376
----
373-
grid.addColumn(LitRenderer.<Person>of(
374-
"<input .value=\"${item.profession}\" @change=\"${e => changed(e.target.value)}\">")
377+
grid.addColumn(LitRenderer.<Person> of(
378+
"""
379+
<input .value="${item.profession}" @change="${e => changed(e.target.value)}">
380+
""")
381+
.withProperty("profession", Person::getProfession)
375382
.withFunction("changed", (person, args) -> {
376383
String profession = args.getString(0);
377384
person.setProfession(profession);
378385
grid.getDataProvider().refreshItem(person);
379-
}).withProperty("profession", Person::getProfession));
386+
}));
380387
----
381388

382389
The functions defined by the [methodname]`withFunction()` method can be called with any number of additional parameters. The additional argument of type [classname]`String` (the updated profession) is obtained from the second handler parameter with [methodname]`args.getString(0)`, where the number is the index of the argument in the [classname]`JsonArray`.
@@ -398,30 +405,34 @@ Indicates whether the item is selected or not.
398405
`model.detailsOpened`::
399406
Indicates whether the details row for the item is opened or closed.
400407

408+
`model.hasChildren`::
409+
Indicates whether the item has child items and therefore can be expanded.
410+
401411
The example below shows how to create a custom tree toggle for the `TreeGrid`:
402412

403413
[source,java]
404414
----
405-
// The click listener needs to check if the event gets canceled (by
406-
// vaadin-grid-tree-toggle) and only invoke the callback if it does.
407-
// vaadin-grid-tree-toggle will cancel the event if the user clicks on
408-
// a non-focusable element inside the toggle.
409-
var clickListener = "e => requestAnimationFrame(() => { e.defaultPrevented && onClick(e) })";
410-
411415
grid.addColumn(LitRenderer.<Person> of(
412-
"<vaadin-grid-tree-toggle @click=${" + clickListener + "} .leaf=${item.leaf} .expanded=${model.expanded} .level=${model.level}>"
413-
+ "${item.name}</vaadin-grid-tree-toggle>")
414-
.withProperty("leaf",
415-
item -> !grid.getDataCommunicator().hasChildren(item))
416-
.withProperty("name",
417-
item -> item.getName())
416+
// The click listener needs to check if the event gets canceled
417+
// (by vaadin-grid-tree-toggle) and only invoke the callback if
418+
// it does. vaadin-grid-tree-toggle will cancel the event if the
419+
// user clicks on a non-focusable element inside the toggle.
420+
"""
421+
<vaadin-grid-tree-toggle
422+
@click="${(e) => requestAnimationFrame(() => e.defaultPrevented && onClick(e))}"
423+
.leaf="${!model.hasChildren}"
424+
.level="${model.level}"
425+
.expanded="${live(model.expanded)}"
426+
>
427+
${item.name}
428+
</vaadin-grid-tree-toggle>
429+
""")
430+
.withProperty("name", item -> item.getName())
418431
.withFunction("onClick", item -> {
419-
if (grid.getDataCommunicator().hasChildren(item)) {
420-
if (grid.isExpanded(item)) {
421-
grid.collapse(item);
422-
} else {
423-
grid.expand(item);
424-
}
432+
if (grid.isExpanded(item)) {
433+
grid.collapse(item);
434+
} else {
435+
grid.expand(item);
425436
}
426437
}));
427438
----

0 commit comments

Comments
 (0)