# Data Binding

The MultiSelectTree provides core settings for data binding scenarios and enables you to work with various types of data.

## Basics

The MultiSelectTree exposes the following core data-binding options:

* [`data`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#data) (`any[]`)—Binds the items which will be rendered in a tree-like structure inside the MultiSelectTree.
* [`textField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#textfield) (`string | string[]`)—Determines the item field that will be displayed.
* [`valueField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#valuefield) (`string | string[]`)—Determines the item field which will be used as item identifier.
* [`hasChildren`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#haschildren) (`(node: any) => boolean`)—Determines if a given item has children.
* [`fetchChildren`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#fetchchildren) (`(node: any) => Observable<any[]>`)—Supplies the child items based on the provided parent.

To render a hierarchical set of data, implement the [`fetchChildren`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#fetchchildren) and [`hasChildren`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#haschildren) functions or use the built-in directives.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dropdowns/multiselecttree/data_binding/manual_setup/source.txt)

## Built-in Directives

The built-in data-binding directives provide specialized implementations of the required callbacks and events, simplify data binding, minimize boilerplate code, and support flat or heterogeneous data.

The MultiSelectTree supports the following built-in directives:

* [`kendoMultiSelectTreeHierarchyBinding`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreehierarchybindingdirective)—Handles the binding to hierarchical data.
* [`kendoMultiSelectTreeFlatBinding`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective)—Handles the binding to flat data by mapping the data to a hierarchical structure and by using the configured parent-child relations.

> The built-in data-binding directives work only with the complete data set and do not support lazy-loading for the child items.

### Hierarchy Binding Directive

To utilize the hierarchy binding directive, provide to the directive selector the desired data set and specify a [`childrenField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreehierarchybindingdirective#childrenfield) value which will be used to identify the child item collections.

The following example demonstrates the [`kendoMultiSelectTreeHierarchyBinding`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreehierarchybindingdirective) directive in action.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dropdowns/multiselecttree/data_binding/hierarchy_binding_directive/source.txt)

### Flat Binding Directive

To utilize the flat binding directive, provide to the directive selector the desired data set. The relations between the items is determined by the specified [`valueField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective#valuefield)
and [`parentIdField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective#parentidfield). The [`valueField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective#valuefield) serves as item identifier, while the [`parentIdField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective#parentidfield) value has to point to the [`valueField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective#valuefield) of the item's parent. If the [`parentIdField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective#parentidfield) value does not point to any other element, that item will be rendered at root level.

The following example demonstrates the [`kendoMultiSelectTreeFlatBinding`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreehierarchybindingdirective) directive in action.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dropdowns/multiselecttree/data_binding/flat_binding_directive/source.txt)

## Lazy Loading of Items

To enable lazy loading of the component items, set the [`loadOnDemand`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#loadondemand) property to `true`. By default `loadOnDemand` is set to `false`.

The MultiSelectTree doesn't cache the child items by default. To avoid fetching the items again on subsequent collapse-expand iterations, cache the already loaded items manually.

> When `loadOnDemand` is enabled, you have to set the [`checkChildren`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecheckablesettings#checkchildren) property of the [`MultiSelectTreeCheckableSettings`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#checkablesettings) to `false`. Otherwise, after a checked item gets expanded and its inner items are loaded, the selected value and the checked items in the drop-down will no longer be in sync. Currently, such a scenario is not supported.

The following example demonstrates how to fetch the child items on demand, only after they have been expanded.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dropdowns/multiselecttree/data_binding/remote_binding/source.txt)

## Heterogenous Data

You can also bind the MultiSelectTree to heterogenous data, that is, to a different [`valueField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#valuefield) and [`textField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#textfield) value at each level.

To configure the component to use different fields for each next level, supply the [`valueField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#valuefield) and [`textField`](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent#textfield) properties with arrays of strings. The items will use the field on the corresponding level as their value and text.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dropdowns/multiselecttree/data_binding/heterogenous_data/source.txt)

## Suggested Links

* [API Reference of the MultiSelectTree](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreecomponent)
* [API Reference of the Hierarchy Binding Directive](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreehierarchybindingdirective)
* [API Reference of the Flat Binding Directive](https://www.telerik.com/kendo-angular-ui/components/dropdowns/api/multiselecttreeflatbindingdirective)
* [Value Binding](https://www.telerik.com/kendo-angular-ui/components/dropdowns/multiselecttree/value-binding)
