Skip to content

Commit 7b72922

Browse files
docs(treeview): data binding examples wip
1 parent 6ef5e0f commit 7b72922

File tree

1 file changed

+182
-4
lines changed

1 file changed

+182
-4
lines changed

components/treeview/data-bind.md

Lines changed: 182 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ Each `TelerikTreeViewBinding` tag exposes the following properties that refer to
5050
* ExpandedField => Expanded
5151
* HasChildrenField => HasChildren
5252
* ItemsField => Items
53-
* Level - this is used for defining [differenrt bindings for different levels](#multiple-level-bindings). If no level is set, the bindings are taken as default for any level that does not have explicit settings. You should have one `TelerikTreeViewBinding` without a level.
53+
* Level - this is used for defining [different bindings for different levels](#multiple-level-bindings). If no level is set, the bindings are taken as default for any level that does not have explicit settings. You should have one `TelerikTreeViewBinding` without a level.
5454

55-
The default values for the fields match the properties, so the following tree item model does not require explicit binding settings:
55+
>tip There are default values for the field names. If your model names match the defaults, you don't have to define them in the bindings settings.
56+
57+
>caption Default field names for treeview item bindings. If you use these, you don't have to specify them in the `TelerikTreeViewBinding` tag explicitly.
5658
5759
````CSHTML
5860
public class TreeItem
@@ -68,13 +70,13 @@ public class TreeItem
6870

6971
The following **Example** shows how to define simple binding to match item fields to a model so a tree renders the provided flat data source.
7072

71-
>caption Sample binding on a flat data source
73+
>caption Sample binding on a flat data source. Showcases how to set the properties to match the model. With this model, the only field name you must explicitly specify is `ParentIdField`, the others match the defaults.
7274
7375
@[template](/_contentTemplates/treeview/basic-example.md#basic-example-with-screenshot)
7476

7577
### Multiple Level Bindings
7678

77-
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.
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.
7880

7981
This also allows you to define a different `ItemTemplate` for different levels.
8082

@@ -163,8 +165,184 @@ If a certain level does not have an explicit data bindings tag, it will use the
163165

164166
## Flat Data
165167

168+
Flat data means that the entire collection of treeview items is available at one level, for example `List<MyTreeItemModel>`.
169+
170+
The parent-child relationships are created through internal data in the model - the `ParentId` field which points to the `Id` of the item that will contain the current item. The root level has `null` for `ParentId`.
171+
172+
>caption Example of flat data in a treeview
173+
174+
````CSHTML
175+
@using Telerik.Blazor.Components.TreeView
176+
177+
<TelerikTreeView Data="@FlatData">
178+
<TelerikTreeViewBindings>
179+
<TelerikTreeViewBinding ParentIdField="Parent" ExpandedField="IsExpanded"></TelerikTreeViewBinding>
180+
</TelerikTreeViewBindings>
181+
</TelerikTreeView>
182+
183+
@code {
184+
public IEnumerable<TreeItem> FlatData { get; set; }
185+
186+
public class TreeItem //most fields use the default names and will bind automatically in this example
187+
{
188+
public int Id { get; set; }
189+
public string Text { get; set; }
190+
public int? Parent { get; set; } //this is a non-default field name
191+
public bool HasChildren { get; set; }
192+
public bool IsExpanded { get; set; } //this is a non-default field name
193+
}
194+
195+
protected override void OnInit()
196+
{
197+
FlatData = LoadFlat();
198+
}
199+
200+
private List<TreeItem> LoadFlat()
201+
{
202+
List<TreeItem> items = new List<TreeItem>();
203+
204+
items.Add(new TreeItem()
205+
{
206+
Id = 1,
207+
Text = "Parent 1",
208+
Parent = null, // indicates a root (zero-level) item
209+
HasChildren = true, // informs the treeview there are children so it renders the expand option
210+
IsExpanded = true // an item can be expanded by default
211+
});
212+
213+
items.Add(new TreeItem()
214+
{
215+
Id = 2,
216+
Text = "Parent 2",
217+
Parent = null, // indicates a root item
218+
HasChildren = true,
219+
IsExpanded = false
220+
});
221+
222+
items.Add(new TreeItem()
223+
{
224+
Id = 3,
225+
Text = "Parent 3",
226+
Parent = null, // indicates a root item
227+
HasChildren = false, //there will be no children in this item
228+
IsExpanded = true // will not have an effect if there are no children
229+
});
230+
231+
items.Add(new TreeItem()
232+
{
233+
Id = 4,
234+
Text = "Child 1 of Parent 1",
235+
Parent = 1, // the parent will be the first item
236+
HasChildren = false,
237+
IsExpanded = false
238+
});
239+
240+
items.Add(new TreeItem()
241+
{
242+
Id = 5,
243+
Text = "Child 2 of Parent 1",
244+
Parent = 1, // the parent will be the first item
245+
HasChildren = true,
246+
IsExpanded = true
247+
});
248+
249+
items.Add(new TreeItem()
250+
{
251+
Id = 6,
252+
Text = "Child 1 of Child 2",
253+
Parent = 5, // the parent will be the first child of the first root item
254+
HasChildren = false,
255+
IsExpanded = false
256+
});
257+
258+
items.Add(new TreeItem()
259+
{
260+
Id = 7,
261+
Text = "Child 1 of Parent 2",
262+
Parent = 2, // the parent will be the second root item
263+
HasChildren = false,
264+
IsExpanded = false
265+
});
266+
267+
return items;
268+
}
269+
}
270+
````
271+
166272
## Hierarchical Data
167273

274+
Hierarchical data means that the collection child items is provided in a field of its parent's model. By default, this is the `Items` field.
275+
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+
278+
>caption Example of hierarchical data that uses different models for the parent and the child
279+
280+
````CSHTML
281+
@using Telerik.Blazor.Components.TreeView
282+
283+
<TelerikTreeView Data="@HierarchicalData">
284+
<TelerikTreeViewBindings>
285+
<TelerikTreeViewBinding TextField="Category" ItemsField="Products"></TelerikTreeViewBinding>
286+
<TelerikTreeViewBinding Level="1" TextField="ProductName"></TelerikTreeViewBinding>
287+
</TelerikTreeViewBindings>
288+
</TelerikTreeView>
289+
290+
@code {
291+
public IEnumerable<ProductCategoryItem> HierarchicalData { get; set; }
292+
293+
public class ProductCategoryItem
294+
{
295+
public string Category { get; set; }
296+
public List<ProductItem> Products { get; set; }
297+
public bool Expanded { get; set; }
298+
public bool HasChildren { get; set; }
299+
}
300+
301+
public class ProductItem
302+
{
303+
public string ProductName { get; set; }
304+
// the following fields are to denote you can keep having hierarchy further down. They are not required
305+
// they are not really used in this example and you would have a collection of child items too
306+
// see the information about multiple data bindings earlier in this article on using them
307+
public bool Expanded { get; set; }
308+
public bool HasChildren { get; set; }
309+
}
310+
311+
312+
protected override void OnInit()
313+
{
314+
LoadHierarchical();
315+
}
316+
317+
private void LoadHierarchical()
318+
{
319+
List<ProductCategoryItem> roots = new List<ProductCategoryItem>();
320+
321+
List<ProductItem> firstCategoryProducts = new List<ProductItem>()
322+
{
323+
new ProductItem { ProductName= "Category 1 - Product 1" },
324+
new ProductItem { ProductName= "Category 1 - Product 2" }
325+
};
326+
327+
roots.Add(new ProductCategoryItem
328+
{
329+
Category = "Category 1",
330+
Expanded = true,
331+
Products = firstCategoryProducts, // this is how child items are provided
332+
HasChildren = firstCategoryProducts?.Count > 0, // set this depending on the presence of items in the child items collection
333+
334+
});
335+
336+
roots.Add(new ProductCategoryItem
337+
{
338+
Category = "Category 2" // we will set no other properties and it will not have children, nor will it be expanded
339+
});
340+
341+
HierarchicalData = roots;
342+
}
343+
}
344+
````
345+
168346
## Load On Demand
169347

170348
## See Also

0 commit comments

Comments
 (0)