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: components/treeview/data-bind.md
+109-3Lines changed: 109 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,7 +76,7 @@ The following **Example** shows how to define simple binding to match item field
76
76
77
77
### Multiple Level Bindings
78
78
79
-
You can define different binding settings for the different levels of nodes in a treeview. With this, the children of a node can consume a different field than their parent, and this may make your application more flexible. If you use hierarchical data binding, the children can even use a different model from their parent.
79
+
You can define different binding settings for the different levels of nodes in a treeview. With this, the children of a node can consume a different field than their parent, and this may make your application more flexible. If you use [hierarchical data binding](#hierarchical-data), the children can even use a different model from their parent.
80
80
81
81
This also allows you to define a different `ItemTemplate` for different levels.
82
82
@@ -275,7 +275,7 @@ Hierarchical data means that the collection child items is provided in a field o
275
275
276
276
This lets you gather separate collections of data and/or use different models at each different level. Note that the data binding settings are per level, so a certain level will always use the same bindings, regardless of the model they represent and their parent.
277
277
278
-
>caption Example of hierarchical data that uses different models for the parent and the child
278
+
>caption Example of hierarchical data that uses different models for the parent and the child. Using different models is not required.
279
279
280
280
````CSHTML
281
281
@using Telerik.Blazor.Components.TreeView
@@ -345,7 +345,113 @@ This lets you gather separate collections of data and/or use different models at
345
345
346
346
## Load On Demand
347
347
348
+
You don't have to provide all the data the treeview will render at once - the root nodes are sufficient for an initial display. You can then use the `OnExpand` event of the treeview to provide [hierarchical data](#hierarchical-data) to the node that was just expanded. Loading nodes on demand can improve the performance of your application by requesting less data at any given time.
349
+
350
+
>caption Load on Demand in a TreeView with sample handling of the various cases. Review the code comments for details.
// check if the item is expanding, we don't need to do anything if it is collapsing
411
+
// in this example we will also check the type of the model to know how to identify the node and what data to load. If you use only one model for all levels, you don't have to do this
412
+
if (args.Expanded && args.Item is ProductCategoryItem)
413
+
{
414
+
ProductCategoryItem currCategory = args.Item as ProductCategoryItem;
415
+
if (currCategory.Products?.Count > 0)
416
+
{
417
+
return; // item has been expanded before so it has data, don't load data again
418
+
// alternatively, load it again but make sure to handle the child items correctly
419
+
// either overwrite the entire collection, or use some other logic to append/merge
420
+
}
421
+
int itemIdentifier = currCategory.CategoryId;
422
+
// in a similar fashion, you can identify the item that was just expanded through its properties
423
+
// in this example, we will hardcode some data and logic for brevity
424
+
// in a real case, you would probably await a remote endpoint/service
425
+
426
+
if (itemIdentifier == 2) // simulate no data for a certain node - the second in our example
427
+
{
428
+
currCategory.HasChildren = false; // remove the expand icon from the node
429
+
430
+
StateHasChanged(); // inform the UI that the data is changed
431
+
432
+
return;
433
+
}
434
+
435
+
// data requested and received for a certain node
436
+
List<ProductItem> theProducts = new List<ProductItem>() {
437
+
new ProductItem { ProductName= $"Category {itemIdentifier} - Product 1" },
438
+
new ProductItem { ProductName= $"Category {itemIdentifier} - Product 2" }
0 commit comments