Angular TreeView API

The TreeView widget is a tree-like representation of textual data.

import { DxTreeViewModule } from "devextreme-angular"
Type: DxTreeViewTypes.default

DevExtreme widgets are integrated with many popular libraries and frameworks. See the Installation section (for JavaScript libraries) or the Prerequisites and Installation section (for ASP.NET MVC framework) to find details on setting up DevExtreme with a particular library or framework.

The following code shows how to create the TreeView widget using every supported library and framework. For more details on working with widgets in these libraries and frameworks, see the Widget Basics topic for jQuery, Angular, AngularJS, Knockout or ASP.NET MVC.

jQuery
JavaScript
HTML
$(function () {
    $("#treeView").dxTreeView({
        dataSource: [
            { id: "1", text: "Item 1" },
            { id: "1_1", text: "Subitem 1.1", parentId: "1" },
            { id: "1_2", text: "Subitem 1.2", parentId: "1" },
            { id: "2", text: "Item 2" },
            { id: "2_1", text: "Subitem 2.1", parentId: "2" },
            { id: "2_2", text: "Subitem 2.2", parentId: "2" }
        ],
        dataStructure: "plain"
    });
});
<div id="treeView"></div>
Angular
HTML
TypeScript
<dx-tree-view
    [dataSource]="treeViewDataSource"
    dataStructure="plain">
</dx-tree-view>
import { DxTreeViewModule } from "devextreme-angular"
// ...
export class AppComponent {
    treeViewDataSource = [
        { id: "1", text: "Item 1" },
        { id: "1_1", text: "Subitem 1.1", parentId: "1" },
        { id: "1_2", text: "Subitem 1.2", parentId: "1" },
        { id: "2", text: "Item 2" },
        { id: "2_1", text: "Subitem 2.1", parentId: "2" },
        { id: "2_2", text: "Subitem 2.2", parentId: "2" }
    ];
}
@NgModule({
    imports: [
        // ...
        DxTreeViewModule
    ],
    // ...
})
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-tree-view="{
        dataSource: treeViewDataSource,
        dataStructure: 'plain'
    }"></div>
</div>
angular.module("DemoApp", ["dx"])
    .controller("DemoController", function ($scope) {
        $scope.treeViewDataSource = [
            { id: "1", text: "Item 1" },
            { id: "1_1", text: "Subitem 1.1", parentId: "1" },
            { id: "1_2", text: "Subitem 1.2", parentId: "1" },
            { id: "2", text: "Item 2" },
            { id: "2_1", text: "Subitem 2.1", parentId: "2" },
            { id: "2_2", text: "Subitem 2.2", parentId: "2" }
        ];
    });
Knockout
HTML
JavaScript
<div data-bind="dxTreeView: {
    dataSource: treeViewDataSource,
    dataStructure: 'plain'
}"></div>
var viewModel = {
    treeViewDataSource: [
        { id: "1", text: "Item 1" },
        { id: "1_1", text: "Subitem 1.1", parentId: "1" },
        { id: "1_2", text: "Subitem 1.2", parentId: "1" },
        { id: "2", text: "Item 2" },
        { id: "2_1", text: "Subitem 2.1", parentId: "2" },
        { id: "2_2", text: "Subitem 2.2", parentId: "2" }
    ]
};
ko.applyBindings(viewModel);
ASP.NET MVC Controls
Razor C#
Razor VB
@(Html.DevExtreme().TreeView()
    .ID("treeView")
    .DataSource(new object[] {
        new { id = "1", text = "Item 1" },
        new { id = "1_1", text = "Subitem 1.1", parentId = "1" },
        new { id = "1_2", text = "Subitem 1.2", parentId = "1" },
        new { id = "2", text = "Item 2" },
        new { id = "2_1", text = "Subitem 2.1", parentId = "2" },
        new { id = "2_2", text = "Subitem 2.2", parentId = "2" }
    })
    .DataStructure(TreeViewDataStructure.Plain)
)
@(Html.DevExtreme().TreeView() _
    .ID("treeView") _
    .DataSource({
        New With { .id = "1", .text = "Item 1" },
        New With { .id = "1_1", .text = "Subitem 1.1", .parentId = "1" },
        New With { .id = "1_2", .text = "Subitem 1.2", .parentId = "1" },
        New With { .id = "2", .text = "Item 2" },
        New With { .id = "2_1", .text = "Subitem 2.1", .parentId = "1" },
        New With { .id = "2_2", .text = "Subitem 2.2", .parentId = "1" }
    }) _
    .DataStructure(TreeViewDataStructure.Plain)
)
NOTE
The TreeView widget requires each data source item to contain at least a key field. Thus, the widget does not support data sources consisting of value items.

View Demo Watch Video

See Also

Configuration

An object defining configuration options for the TreeView widget.

Name Description
accessKey

Specifies the shortcut key that sets focus on the widget.

activeStateEnabled

Specifies whether or not the widget changes its state when interacting with a user.

animationEnabled

Specifies whether or not to animate item collapsing and expanding.

createChildren

Allows you to load nodes. Applies if the dataStructure is "plain" and the dataSource and items are undefined.

dataSource

A data source used to fetch data to be displayed by the widget.

dataStructure

Specifies whether a nested or plain array is used as a data source.

disabled

Specifies whether the widget responds to user interaction.

disabledExpr

Specifies the name of the data source item field whose value defines whether or not the corresponding widget item is disabled.

displayExpr

Specifies the name of the data source item field whose value is displayed by the widget.

elementAttr

Specifies the attributes to be attached to the widget's root element.

expandAllEnabled

Specifies whether or not a user can expand all tree view items by the "*" hot key.

expandedExpr

Specifies the name of the data source item field whose value defines whether or not the corresponding widget item is displayed expanded.

expandNodesRecursive

Specifies whether or not all parent nodes of an initially expanded node are displayed expanded.

focusStateEnabled

Specifies whether the widget can be focused using keyboard navigation.

hasItemsExpr

Specifies the name of the data source item field whose value defines whether or not the corresponding node includes child nodes.

height

Specifies the widget's height.

hint

Specifies text for a hint that appears when a user pauses on the widget.

hoverStateEnabled

Specifies whether the widget changes its state when a user pauses on it.

itemHoldTimeout

The time period in milliseconds before the onItemHold event is raised.

items

An array of items displayed by the widget.

itemsExpr

Specifies which data field contains nested items. Applies only if the dataStructure option is "tree".

itemTemplate

Specifies a custom template for items.

keyExpr

Specifies which data field provides keys for TreeView items.

noDataText

The text or HTML markup displayed by the widget if the item collection is empty.

onContentReady

A function that is executed when the widget's content is ready and each time the content is changed.

onDisposing

A function that is executed before the widget is disposed of.

onInitialized

A function that is executed only once, after the widget is initialized.

onItemClick

A function that is executed when a collection item is clicked or tapped.

onItemCollapsed

A function that is executed when a tree view item is collapsed.

onItemContextMenu

A function that is executed when a collection item is right-clicked or pressed.

onItemExpanded

A function that is executed when a tree view item is expanded.

onItemHold

A function that is executed when a collection item has been held for a specified period.

onItemRendered

A function that is executed after a collection item is rendered.

onItemSelectionChanged

A function that is executed when a tree view item is selected or the selection is canceled.

onOptionChanged

A function that is executed after a widget option is changed.

onSelectAllValueChanged

A function that is executed when the "Select All" check box value is changed. Applies only if showCheckBoxesMode is "selectAll" and selectionMode is "multiple".

onSelectionChanged

A function that is executed when a tree view item is selected or the selection is canceled.

parentIdExpr

Specifies the name of the data source item field for holding the parent key of the corresponding node.

rootValue

Specifies the parent ID value of the root item.

rtlEnabled

Switches the widget to a right-to-left representation.

scrollDirection

A string value specifying available scrolling directions.

searchEditorOptions

Configures the search panel.

searchEnabled

Specifies whether the search panel is visible.

searchExpr

Specifies a data object's field name or an expression whose value is compared to the search string.

searchMode

Specifies a comparison operation used to search widget items.

searchTimeout

Specifies a delay in milliseconds between when a user finishes typing, and the search is executed.

searchValue

Specifies the current search string.

selectAllText

Specifies the text displayed at the "Select All" check box.

selectByClick

Specifies whether or not an item becomes selected if a user clicks it.

selectedExpr

Specifies the name of the data source item field whose value defines whether or not the corresponding widget items is selected.

selectionMode

Specifies item selection mode.

selectNodesRecursive

Specifies whether or not to select nodes recursively.

showCheckBoxesMode

Specifies the current check boxes display mode.

tabIndex

Specifies the number of the element when the Tab key is used for navigating.

virtualModeEnabled

Specifies if the virtual mode is enabled.

visible

Specifies whether the widget is visible.

width

Specifies the widget's width.

Methods

This section describes members used to manipulate the widget.

Name Description
beginUpdate()

Prevents the widget from refreshing until the endUpdate() method is called.

collapseAll()

Collapses all items.

collapseItem(itemData)

Collapses an item with a specific key.

collapseItem(itemElement)

Collapses an item found using its DOM node.

collapseItem(key)

Collapses an item with a specific key.

defaultOptions(rule)

Specifies the device-dependent default configuration options for this component.

dispose()

Disposes of all the resources allocated to the TreeView instance.

element()

Gets the root widget element.

endUpdate()

Refreshes the widget after a call of the beginUpdate() method.

expandAll()

Expands all items. If you load items on demand, this method expands only the loaded items.

expandItem(itemData)

Expands an item found using its data object.

expandItem(itemElement)

Expands an item found using its DOM node.

expandItem(key)

Expands an item with a specific key.

focus()

Sets focus on the widget.

getDataSource()

Gets the DataSource instance.

getInstance(element)

Gets the instance of a widget found using its DOM node.

getNodes()

Gets all nodes.

instance()

Gets the widget's instance. Use it to access other methods of the widget.

off(eventName)

Detaches all event handlers from a single event.

off(eventName, eventHandler)

Detaches a particular event handler from a single event.

on(eventName, eventHandler)

Subscribes to an event.

on(events)

Subscribes to events.

option()

Gets all widget options.

option(optionName)

Gets the value of a single option.

option(optionName, optionValue)

Updates the value of a single option.

option(options)

Updates the values of several options.

registerKeyHandler(key, handler)

Registers a handler to be executed when a user presses a specific key.

repaint()

Repaints the widget without reloading data. Call it to update the widget's markup.

selectAll()

Selects all items.

selectItem(itemData)

Selects an item found using its data object.

selectItem(itemElement)

Selects an item found using its DOM node.

selectItem(key)

Selects an item with a specific key.

unselectAll()

Cancels the selection of all items.

unselectItem(itemData)

Cancels the selection of an item found using its data object.

unselectItem(itemElement)

Cancels the selection of an item found using its DOM node.

unselectItem(key)

Cancels the selection of an item with a specific key.

updateDimensions()

Updates the tree view scrollbars according to the current size of the widget content.

Events

This section describes events fired by this widget.

Name Description
contentReady

Raised when the widget's content is ready.

disposing

Raised before the widget is disposed of.

initialized

Raised only once, after the widget is initialized.

itemClick

Raised when a collection item is clicked or tapped.

itemCollapsed

Raised when a tree view item is collapsed.

itemContextMenu

Raised when a collection item is right-clicked or pressed.

itemExpanded

Raised when a tree view item is expanded.

itemHold

Raised when a collection item has been held for a specified period.

itemRendered

Raised after a collection item is rendered.

itemSelectionChanged

Raised when a tree view item is selected or the selection is canceled.

optionChanged

Raised after a widget option is changed.

selectAllValueChanged

Raised when the "Select All" check box value is changed.

selectionChanged

Raised when a tree view item is selected or the selection is canceled.

See Also

Node

A TreeView node.

Type:

Object

This section describes the contents of a TreeView node object. Such objects can be accessed using the getNodes() method and within functions handling the events whose name begins with item....

See Also

Default Item Template

This section lists the data source fields that are used in a default item template.

Type:

Object

By default, a predefined item template is applied to display the items of this widget. This template is based on certain fields of the data source provided for this widget. Below is the list of these fields. If the default item template is not appropriate for your task, implement a custom item template based on these or other fields of your data source.

See Also