Skip to content

Commit 4c7e1ef

Browse files
committed
improve sorting example
1 parent 99dc32e commit 4c7e1ef

1 file changed

Lines changed: 37 additions & 23 deletions

File tree

articles/components/tree-grid/data-binding.adoc

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,47 +143,61 @@ treeGrid.addHierarchyColumn(Folder::name).setHeader("Folder").setSortable(true);
143143
More examples of how to configure sortable columns, including multi-column sorting and custom comparators, can be found in the <<../grid/index#sorting,Grid>> documentation.
144144
====
145145

146-
In addition to column-level sorting, you can define a sort comparator directly on the `TreeDataProvider` instance using the `setSortComparator(SerializableComparator<T>)` method. This comparator is applied when no column sorting is used, and it acts as a secondary comparator if column sorting is used.
146+
In addition to column-level sorting, you can define a sort comparator directly on the `TreeDataProvider` instance using the `setSortComparator(SerializableComparator<T>)` method. This comparator is applied when no column sorting is used, and it acts as a secondary comparator when combined with column sorting.
147+
148+
In the example below, a Select component is used to let users choose how folders are sorted in the Tree Grid. The `setSortComparator` method is called to apply the corresponding comparator based on the selected value:
147149

148150
[source,java]
149151
----
150-
// Set a custom sort comparator to sort folders by name in descending order.
151-
// setSortComparator requires a Serializable comparator, so we use a lambda
152-
// here since standard Comparator isn't serializable.
153-
treeDataProvider.setSortComparator((folder0, folder1) ->
154-
Comparator.comparing(Folder::name).reversed().compare(folder0, folder1));
155-
156-
...
157-
158-
// Remove the custom sort comparator
159-
treeDataProvider.setSortComparator(null);
152+
Select<String> sortBySelect = new Select<>("Sort by", "", "Name (A-Z)", "Name (Z-A)");
153+
sortBySelect.setValue("");
154+
sortBySelect.addValueChangeListener(event -> {
155+
Comparator<Folder> comparator = switch (event.getValue()) {
156+
case "Name (A-Z)" -> Comparator.comparing(Folder::name);
157+
case "Name (Z-A)" -> Comparator.comparing(Folder::name).reversed();
158+
default -> null;
159+
};
160+
161+
if (comparator != null) {
162+
// The comparator can't be passed directly because it doesn't implement
163+
// SerializableComparator, which is required by setSortComparator. Using
164+
// comparator::compare works as a workaround, as long as no serialization
165+
// is triggered at runtime.
166+
treeDataProvider.setSortComparator(comparator::compare);
167+
} else {
168+
treeDataProvider.setSortComparator(null);
169+
}
170+
});
160171
----
161172

162-
The `setSortComparator` method automatically re-renders the Tree Grid to reflect the new sort order.
163-
164-
Programmatically managing sorting can be useful when you have sort controls outside the Tree Grid and you want to integrate them.
173+
The `setSortComparator` method re-renders the Tree Grid automatically, so calling `refreshAll` isn't necessary.
165174

166175
[NOTE]
167176
====
168-
To avoid breaking parent-child relationships, sorting only compares items within the same hierarchy level.
177+
Sorting in tree structures is always performed independently at each level to avoid breaking the links between parents and their children.
169178
====
170179

171180
=== Filtering Items
172181

173-
Tree Data Provider also supports in-memory filtering. To filter items, you can set a filter predicate on the [classname]`TreeDataProvider` instance using the [methodname]`setFilter(SerializablePredicate<T>)` method:
182+
Tree Data Provider also supports in-memory filtering. You can apply filtering by setting a predicate via the `TreeDataProvider#setFilter(SerializablePredicate<T>)` method.
183+
184+
The following example demonstrates how this method can be used to filter folders based on a search term entered in a TextField component. As the user types, the Tree Grid updates in real time to show only folders whose names contain the search term:
174185

175186
[source,java]
176187
----
177-
// Only show folders whose names contain "doc" (case-insensitive)
178-
treeDataProvider.setFilter(folder -> folder.name().toLowerCase().contains("doc"));
179-
180-
...
188+
TextField searchInput = new TextField("Search folders", "Enter folder name");
189+
searchInput.setValueChangeMode(ValueChangeMode.EAGER);
190+
searchInput.addValueChangeListener(event -> {
191+
String searchTerm = event.getValue();
181192
182-
// Reset to show all folders
183-
treeDataProvider.setFilter(null);
193+
treeDataProvider.setFilter((folder) ->
194+
folder.name().toLowerCase().contains(searchTerm.toLowerCase()));
195+
});
184196
----
185197

186-
The `setFilter` method automatically re-renders the Tree Grid to show the filtered results. When filtering, the Tree Data Provider evaluates the entire expanded hierarchy so that if a nested item matches, all its parents are included as well – even if they don't match the filter themselves.
198+
The `setFilter` method re-renders the Tree Grid automatically, so calling `refreshAll` isn't necessary.
199+
200+
When filtering, the Tree Data Provider evaluates the entire expanded hierarchy. If a nested item matches the filter, all of its parent items are also included – even if they don't match the filter themselves – to ensure the matching item actually appears in the filtered tree.
187201

188202
== Custom Data Providers
189203

0 commit comments

Comments
 (0)