Skip to content

Commit 8f75390

Browse files
committed
docs: explain TreeDataProvider sorting
1 parent 316fbb8 commit 8f75390

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,41 @@ Shortcuts.addShortcutListener(treeGrid, () -> {
129129

130130
As before, make sure to call [methodname]`TreeDataProvider#refreshAll()` to update the Tree Grid UI after making any structural changes to the [classname]`TreeData`.
131131

132+
=== Sorting Items
133+
134+
Tree Data Provider provides built-in support for in-memory sorting. You can enable sorting on Tree Grid columns, and the data provider will automatically sort the items based on the sort order selected by the user:
135+
136+
[source,java]
137+
----
138+
treeGrid.addHierarchyColumn(Folder::name).setHeader("Folder").setSortable(true);
139+
----
140+
141+
[NOTE]
142+
====
143+
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.
144+
====
145+
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 will be applied by default and will run after any column-level comparators:
147+
148+
[source,java]
149+
----
150+
// setSortComparator requires a serializable comparator, so
151+
// we use a lambda since Comparator itself isn't serializable.
152+
treeDataProvider.setSortComparator((folder0, folder1) ->
153+
Comparator.comparing(Folder::name).reversed().compare(folder0, folder1));
154+
----
155+
156+
Sorting is always applied independently at each hierarchy level to preserve the tree structure.
157+
132158
== Custom Data Providers
133159

134160
Conceptually, any hierarchical data provider, including [classname]`TreeDataProvider`, is a class that implements the [classname]`HierarchicalDataProvider` interface.
135161

136-
For convenience, Flow provides a related abstract base class [classname]`AbstractHierarchicalDataProvider`, which you can extend to create a custom hierarchical data providers. This class requires implementing the following methods:
162+
For convenience, Flow provides a related abstract base class [classname]`AbstractHierarchicalDataProvider`, which you can extend to create a custom hierarchical data provider. This class requires implementing the following methods:
137163

138164
[source,java]
139165
----
140-
class CustomDataProvider extends AbstractHierarchicalDataProvider<T, F> {
166+
public class CustomDataProvider extends AbstractHierarchicalDataProvider<T, F> {
141167
@Override
142168
public boolean isInMemory() { // <1>
143169
// Your implementation here
@@ -194,7 +220,7 @@ Below is an example of a simple in-memory data provider using the nested hierarc
194220
[source,java]
195221
.FolderDataProvider.java
196222
----
197-
class FolderDataProvider implements AbstractHierarchicalDataProvider<Folder, Void> {
223+
public class FolderDataProvider implements AbstractHierarchicalDataProvider<Folder, Void> {
198224
public FolderTreeData folderTreeData = new FolderTreeData();
199225
200226
@Override
@@ -298,7 +324,7 @@ Below is an example of a simple in-memory data provider using the flattened hier
298324
[source,java]
299325
.FolderDataProvider.java
300326
----
301-
class FolderDataProvider implements AbstractHierarchicalDataProvider<Folder, Void> {
327+
public class FolderDataProvider implements AbstractHierarchicalDataProvider<Folder, Void> {
302328
public FolderTreeData folderTreeData = new FolderTreeData();
303329
304330
@Override

0 commit comments

Comments
 (0)