ASP.NET Core DropDownTree Overview

Telerik UI for ASP.NET Core Ninja image

The DropDownTree is part of Telerik UI for ASP.NET Core, a professional grade UI library with 110+ components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.

The Telerik UI DropDownTree TagHelper and HtmlHelper for ASP.NET Core are server-side wrappers for the Kendo UI DropDownTree widget.

The DropDownTree represents an editor of hierarchical data, rendered in a tree-like structure, which provides multiple selection option and custom nodes.

Initializing the DropDownTree

The following example demonstrates how to define the DropDownTree.

@(Html.Kendo().DropDownTree()
    .Name("dropdowntree")
    .DataTextField("Name")
    .DataValueField("id")
    .DataSource(dataSource => dataSource
        .Read(read => read
            .Action("Read_DropDownTreeData", "Home")
        )
    )
)
    <kendo-dropdowntree datatextfield="Name" datavaluefield="id" name="dropdowntree"  style="width: 100%">
        <hierarchical-datasource>
            <schema>
                <hierarchical-model id="id"></hierarchical-model>
            </schema>
            <transport>
                <read url="@Url.Action("Remote_DropDownTreeData", "Home")" />
            </transport>
        </hierarchical-datasource>
    </kendo-dropdowntree>
public static IList<HierarchicalViewModel> GetHierarchicalData()
{
    var result = new List<HierarchicalViewModel>()
    {
        new HierarchicalViewModel() { ID = 1, ParentID = null, HasChildren = true, Name = "Parent item" },
        new HierarchicalViewModel() { ID = 2, ParentID = 1, HasChildren = true, Name = "Parent item" },
        new HierarchicalViewModel() { ID = 3, ParentID = 1, HasChildren = false, Name = "Item" },
        new HierarchicalViewModel() { ID = 4, ParentID = 2, HasChildren = false, Name = "Item" },
        new HierarchicalViewModel() { ID = 5, ParentID = 2, HasChildren = false, Name = "Item" }
    };

    return result;
}

public IActionResult Read_DropDownTreeData(int? id)
{
    var result = GetHierarchicalData()
        .Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null)
        .Select(item => new {
            id = item.ID,
            Name = item.Name,
            hasChildren = item.HasChildren
        });

    return Json(result);
}

Basic Configuration

The following example demonstrates the basic configuration of the DropDownTree.

    @(Html.Kendo().DropDownTree()
        .Name("dropdowntree")
        .Items(dropdowntree =>
        {
            dropdowntree.Add().Text("My Web Site")
                .SpriteCssClasses("folder")
                .Expanded(true)
                .Items(root =>
                {
                    root.Add().Text("images")
                        .Expanded(true)
                        .SpriteCssClasses("folder")
                        .Items(images =>
                        {
                            images.Add().Text("logo.png")
                                .SpriteCssClasses("image");
                        });

                    root.Add().Text("resources")
                        .Expanded(true)
                        .SpriteCssClasses("folder")
                        .Items(resources =>
                        {
                            resources.Add().Text("pdf")
                                .Expanded(true)
                                .SpriteCssClasses("folder")
                                .Items(pdf =>
                                {
                                    pdf.Add().Text("prices.pdf")
                                        .SpriteCssClasses("pdf");
                                });

                            resources.Add().Text("zip")
                                .SpriteCssClasses("folder");
                        });

                    root.Add().Text("about.html")
                        .SpriteCssClasses("html");

                    root.Add().Text("index.html")
                        .SpriteCssClasses("html");
                });
        })
    )
    <kendo-dropdowntree name="dropdowntree">
        <items>
            <dropdowntree-item expanded="true" text="My Web Site" sprite-css-class="folder">
                <items>
                    <dropdowntree-item expanded="true" text="images" sprite-css-class="folder">
                        <items>
                            <dropdowntree-item text="logo.png" sprite-css-class="image">
                            </dropdowntree-item>
                        </items>
                    </dropdowntree-item>
                </items>
            </dropdowntree-item>
            <dropdowntree-item expanded="true" text="resources" sprite-css-class="folder">
                <items>
                    <dropdowntree-item expanded="true" text="pdf" sprite-css-class="folder">
                        <items>
                            <dropdowntree-item text="prices.pdf" sprite-css-class="pdf">
                            </dropdowntree-item>
                            <dropdowntree-item text="zip" sprite-css-class="folder">
                                <items>
                                    <dropdowntree-item  text="about.html" sprite-css-class="html">
                                    </dropdowntree-item>
                                    <dropdowntree-item text="index.html" sprite-css-class="html">
                                    </dropdowntree-item>
                                </items>
                            </dropdowntree-item>
                        </items>
                    </dropdowntree-item>
                </items>
            </dropdowntree-item>
        </items>
    </kendo-dropdowntree>

Functionality and Features

Events

The following example demonstrates the available DropDownTree events and how an event handler could be implemented for each of them.

For an example on basic DropDownTree events, refer to the demo on using the events of the DropDownTree.

    @(Html.Kendo().DropDownTree()
        .Name("dropdowntree")
        .DataTextField("Name")
        .DataSource(dataSource => dataSource
            .Read(read => read
                .Action("Employees", "Home")
            )
        )
        .Events(events => events
            .DataBound("onDataBound")
            .Change("onChange")
            .Select("onSelect")
            .Close("onClose")
            .Open("onOpen")
            .Filtering("onFiltering")
        )
    )

        <kendo-dropdowntree datatextfield="Name" datavaluefield="id" name="dropdowntree" on-data-bound="onDataBound" on-open="onOpen" on-close="onClose" on-change="onChange" on-filtering="onFiltering" on-select="onSelect" style="width: 100%">
            <hierarchical-datasource>
                <schema>
                    <hierarchical-model id="id"></hierarchical-model>
                </schema>
                <transport>
                    <read url="@Url.Action("Employees", "Home")" />
                </transport>
            </hierarchical-datasource>
        </kendo-dropdowntree>
    <script type="text/javascript">
        function onDataBound(e) {
            console.log('DropDownTree instance:', e.sender);
        }

        function onChange(e) {
            console.log('Selected node changed to:', e.sender.select());
        }

        function onSelect(e) {
            console.log('Selected node:', e.node);
        }

        function onClose(e) {
            console.log('DropDownTree instance:', e.sender);
        }

        function onOpen(e) {
            console.log('DropDownTree instance:', e.sender);
        }

        function onFiltering(e) {
            console.log('Folter:', e.filter);
        }
    </script>

Referencing Existing Instances

To reference an existing DropDownTree instance, use the jQuery.data() configuration option. Once a reference is established, use the DropDownTree client-side API to control its behavior.

    // Place the following after your Telerik UI DropDownTree for ASP.NET Core declaration.
    <script>
        $(document).ready(function() {
            // The Name() of the DropDownTree is used to get its client-side instance.
            var dropdowntree = $("#dropdowntree").data("kendoDropDownTree");
        });
    </script>

See Also

In this article