|
| 1 | +--- |
| 2 | +title: Expand Selected Nodes Asynchronously |
| 3 | +page_title: Expand Selected Nodes Asynchronously | Kendo UI TreeView HtmlHelper |
| 4 | +description: "Learn how to asynchronously expand the selected node of the Kendo UI TreeView in ASP.NET MVC." |
| 5 | +slug: howto_expandselectednodeasync_treeviewaspnetmvc |
| 6 | +--- |
| 7 | + |
| 8 | +# Expand Selected Nodes Asynchronously |
| 9 | + |
| 10 | +The TreeView for ASP.NET MVC enables you to do Ajax binding by using Entity Framework and to asynchronously expand the selected nodes when `loadOnDemand` is set to `true`. |
| 11 | + |
| 12 | +To see the complete implementation of the approach, refer to the GitHub project on [asynchronously expanding the selected nodes of the TreeView in ASP.NET MVC applications](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/treeview/ExpandSelectedItemAsync). |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +* [Creating the application](#create-the-application) |
| 17 | +* [Adding the new item](#add-the-new-item) |
| 18 | +* [Setting the Entity Data model](#set-the-entity-data-model) |
| 19 | +* [Configuring the new action method](#configure-the-new-action-method) |
| 20 | + |
| 21 | +### Create the Application |
| 22 | + |
| 23 | +1. Create a new ASP.NET MVC application. If you have installed the [Telerik UI for ASP.NET MVC Visual Studio Extensions]({% slug overview_aspnetmvc %}#kendo-ui-for-asp.net-mvc-visual-studio-extensions), create a Telerik UI for ASP.NET MVC application. |
| 24 | + |
| 25 | +1. If you decide not to use the Visual Studio extensions, follow the procedure for adding Telerik UI for ASP.NET MVC to existing applications: |
| 26 | + |
| 27 | + * [ASP.NET MVC 4]({% slug aspnetmvc4_aspnetmvc %}) |
| 28 | + * [ASP.NET MVC 5]({% slug aspnetmvc5_aspnetmvc %}) |
| 29 | + |
| 30 | +### Add the New Item |
| 31 | + |
| 32 | +1. In the Visual Studio solution explorer, right-click **Models**. |
| 33 | +1. Select **Add** > **New Item**. |
| 34 | +1. On the **Add New Item** dialog, select **Data** > **ADO.NET Data Model**. |
| 35 | +1. Provide the `Northwind.edmx` name to the model and click **Next**. As a result, the **Entity Data Model Wizard** starts. |
| 36 | + |
| 37 | +### Set the Entity Data Model |
| 38 | + |
| 39 | +1. Select **Generate from database** and click **Next**. |
| 40 | +1. Configure a connection to the Northwind database and click **Next**. |
| 41 | + |
| 42 | + **Figure 1. A new entity model** |
| 43 | +  |
| 44 | + |
| 45 | +1. Select all tables and click **Finish**. |
| 46 | + |
| 47 | +### Configure the New Action Method |
| 48 | + |
| 49 | +1. Open `Controllers/HomeController.cs` and add a new action method which will return JSON. Each time the user expands a parent node, the TreeView makes an Ajax request to this action method. The action method returns only the child nodes of the expanded parent node. The TreeView provides the unique identifier of the parent node or, when it makes the initial request, `null`. |
| 50 | + |
| 51 | + ```cs |
| 52 | + public JsonResult Employees_Read(int? id) |
| 53 | + { |
| 54 | + using (var northwind = new NORTHWNDEntities()) |
| 55 | + { |
| 56 | + var employeesQuery = northwind.Employees.Select(c => new HierarchicalViewModel |
| 57 | + { |
| 58 | + ID = c.EmployeeID, |
| 59 | + Name = c.FirstName, |
| 60 | + ParentID = null, |
| 61 | + HasChildren = c.Orders.Any() |
| 62 | + }) |
| 63 | + .Union(northwind.Orders.Select(c => new HierarchicalViewModel |
| 64 | + { |
| 65 | + ID = c.OrderID, |
| 66 | + Name = c.ShipAddress, |
| 67 | + ParentID = c.EmployeeID, |
| 68 | + HasChildren = c.Order_Details.Any() |
| 69 | + })) |
| 70 | + .Union(northwind.Order_Details.Select(c => new HierarchicalViewModel |
| 71 | + { |
| 72 | + ID = c.OrderID, |
| 73 | + Name = c.Product.ProductName, |
| 74 | + ParentID = c.Order.OrderID, |
| 75 | + HasChildren = false |
| 76 | + })); |
| 77 | + |
| 78 | + var result = employeesQuery.ToList() |
| 79 | + .Where(x => id.HasValue ? x.ParentID == id : x.ParentID == null) |
| 80 | + .Select(item => new { |
| 81 | + id = item.ID, |
| 82 | + Name = item.Name, |
| 83 | + expanded = item.Expanded, |
| 84 | + hasChildren = item.HasChildren |
| 85 | + |
| 86 | + }); |
| 87 | + |
| 88 | + return Json(result, JsonRequestBehavior.AllowGet); |
| 89 | + } |
| 90 | + } |
| 91 | + ``` |
| 92 | + |
| 93 | +1. Open `Views/Index.cshtml` and add a TreeView. |
| 94 | + |
| 95 | + ```cs |
| 96 | + @(Html.Kendo().TreeView() |
| 97 | + .Name("treeview") |
| 98 | + .DataTextField("Name") |
| 99 | + .DataSource(dataSource => dataSource |
| 100 | + .Read(read => read |
| 101 | + .Action("Employees_Read", "Home") |
| 102 | + ) |
| 103 | + ) |
| 104 | + ) |
| 105 | + ``` |
| 106 | + |
| 107 | +1. Add a button which will asynchronously load child nodes with the [`load()`](https://docs.telerik.com/kendo-ui/api/javascript/data/node/methods/load) method in the child data source and, therefore, asynchronously expand the currently selected node. |
| 108 | +
|
| 109 | + ```cs |
| 110 | + @(Html.Kendo().Button() |
| 111 | + .Name("expandNode") |
| 112 | + .Content("Expand selected node") |
| 113 | + .Events(e => e.Click("onExpandClick")) |
| 114 | + ) |
| 115 | + ``` |
| 116 | + |
| 117 | + ```js |
| 118 | + <script> |
| 119 | + function onExpandClick(e) { |
| 120 | + var tree = $("#treeview").data("kendoTreeView"), |
| 121 | + selected = tree.select(), |
| 122 | + dataItem = tree.dataItem(selected); |
| 123 | + |
| 124 | + var load = function (item) { |
| 125 | + var that = this, |
| 126 | + chain = $.Deferred().resolve(); |
| 127 | + |
| 128 | + chain = chain.then(function () { |
| 129 | + that.expand(that.findByUid(item.uid)); |
| 130 | + return item.load(); |
| 131 | + }).then(function () { |
| 132 | + if (item.hasChildren) { |
| 133 | + var childrenChain = $.Deferred().resolve(); |
| 134 | + |
| 135 | + item.children.data().forEach(function (child) { |
| 136 | + (function (child) { |
| 137 | + childrenChain = childrenChain.then(function () { |
| 138 | + return load.bind(that)(child); |
| 139 | + }) |
| 140 | + })(child) |
| 141 | + }) |
| 142 | + |
| 143 | + return childrenChain; |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + return chain; |
| 148 | + } |
| 149 | + |
| 150 | + if (selected.length == 0) { |
| 151 | + alert("Select item first!"); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + load.bind(tree)(dataItem); |
| 156 | + } |
| 157 | + </script> |
| 158 | + ``` |
| 159 | + |
| 160 | +1. Press `CTRL`+`F5` to build and run the application. Select a node and click the **Expand selected node** button. |
| 161 | + |
| 162 | +## See Also |
| 163 | + |
| 164 | +* [Overview of the TreeView HtmlHelper]({% slug overview_treeviewhelper_aspnetmvc %}) |
| 165 | +* [How to Bind TreeViews to XML]({% slug howto_bindtoaml_treeviewaspnetmvc %}) |
| 166 | +* [How to Integrate with Sharepoint]({% slug howto_integratewithsharepoint_treeviewaspnetmvc %}) |
| 167 | +* [How to Save the State of TreeView Items]({% slug howto_savetreeviewitemsstate_treeviewaspnetmvc %}) |
| 168 | +* [Overview of the Kendo UI TreeView Widget](http://docs.telerik.com/kendo-ui/controls/navigation/treeview/overview) |
| 169 | +* [Overview of Telerik UI for ASP.NET MVC]({% slug overview_aspnetmvc %}) |
| 170 | +* [Fundamentals of Telerik UI for ASP.NET MVC]({% slug fundamentals_aspnetmvc %}) |
| 171 | +* [Scaffolding in Telerik UI for ASP.NET MVC]({% slug scaffolding_aspnetmvc %}) |
| 172 | +* [Telerik UI for ASP.NET MVC API Reference Folder](http://docs.telerik.com/aspnet-mvc/api/Kendo.Mvc/AggregateFunction) |
| 173 | +* [Telerik UI for ASP.NET MVC HtmlHelpers Folder]({% slug overview_barcodehelper_aspnetmvc %}) |
| 174 | +* [Tutorials on Telerik UI for ASP.NET MVC]({% slug overview_timeefficiencyapp_aspnetmvc6 %}) |
| 175 | +* [Telerik UI for ASP.NET MVC Troubleshooting]({% slug troubleshooting_aspnetmvc %}) |
0 commit comments