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
+182-4Lines changed: 182 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,9 +50,11 @@ Each `TelerikTreeViewBinding` tag exposes the following properties that refer to
50
50
* ExpandedField => Expanded
51
51
* HasChildrenField => HasChildren
52
52
* 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.
54
54
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.
56
58
57
59
````CSHTML
58
60
public class TreeItem
@@ -68,13 +70,13 @@ public class TreeItem
68
70
69
71
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.
70
72
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.
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.
78
80
79
81
This also allows you to define a different `ItemTemplate` for different levels.
80
82
@@ -163,8 +165,184 @@ If a certain level does not have an explicit data bindings tag, it will use the
163
165
164
166
## Flat Data
165
167
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`.
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
+
166
272
## Hierarchical Data
167
273
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
0 commit comments