To explain how to implement the drag and drop functionality in the TreeView of kendo UI, I’m going to use the following API's,
- GET: api/Employees/Empdetails
- GET: api/Employees/Empdetails/{EmployeeID}
- PUT: api/Employees/ChangeNode
The above GET API’s are used to bind the employees details and the PUT API is used to update the Employee order in the TreeView
If you are new to ASP.NET WEB API, please go through my previous article to know how to create a RESTful API using ASP.NET WEB API with Entity Framework.
- public class Employee
- {
- [Key]
- public int EmployeeId { get; set; }
- public string FullName { get; set; }
- public bool HasEmployees { get; set; }
- public int? ReportsTo { get; set; }
- }
The Employees Controller class
- [RoutePrefix("api/Employees")]
- public class EmployeesController: ApiController
- {
- private EmployeeContext db = new EmployeeContext();
- // GET: api/Employees
- [HttpGet]
- [ActionName("Empdetails")]
- public IQueryable < Employee > GetEmployees()
- {
- return db.Employees.Where(c => !c.ReportsTo.HasValue);
- }
- // GET: api/Employees/5
- [HttpGet]
- [ActionName("Empdetails")]
- [ResponseType(typeof(Employee))]
- public IHttpActionResult GetEmployee(int EmployeeId)
- {
- var employee = db.Employees.Where(c => c.ReportsTo == EmployeeId);
- if (employee == null)
- {
- return NotFound();
- }
- return Ok(employee);
- }
- [HttpPut]
- [ActionName("ChangeNode")]
- public IHttpActionResult PutEmployee(NodeChange Node)
- {
- if (Node.StartNode != null && Node.DestinationNode != null)
- {
- var personals = db.Employees.Where(m => m.FullName == Node.StartNode).First();
- var personalsParent = db.Employees.Where(m => m.FullName == Node.DestinationNode).First();
- if ((personalsParent.ReportsTo != personals.EmployeeId) && (personalsParent.ReportsTo != null))
- {
- personals.ReportsTo = Convert.ToInt16(personalsParent.ReportsTo);
- db.SaveChanges();
- return Ok("Sucess");
- }
- if (personalsParent.ReportsTo == null)
- {
- personals.ReportsTo = null;
- // personals.HasEmployees = true;
- db.SaveChanges();
- return Ok("Sucess");
- }
- }
- return Ok("Not Valid Start and End Node");
- }
- }
SQL Table
From the above image it is clear that the api/Employees/Empdetails API gives you the details of employees who doesn’t have the report to value,
From the above image it is clear that the api/Employees/Empdetails/{EmployeeId} API gives you the details of employee who have the report to value by filtering it with EmployeeID which is send through the request.
Now we are going to see how to implement the Kendo TreeView using the dynamic data binding to the data source as well as the drag and drop functionality in it, create HTML page in the project, in my case I named it TreeView.html.
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" />
- <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" />
- <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.min.css" />
- <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.default.min.css" />
- <script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script>
- <script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>
- <script src="http://cdn.kendostatic.com/2014.3.1029/js/jszip.min.js"></script>
- <meta charset="utf-8" />
- </head>
- <body>
- <h3>Kendo Tree View</h3>
- <br />
- <div id="treeview" class="demo-section"></div>
- <script>
- homogeneous = new kendo.data.HierarchicalDataSource({
- transport: {
- read: {
- url: "api/Employees/Empdetails",
- dataType: "json"
- }
- },
- schema: {
- model: {
- id: "EmployeeId",
- hasChildren: "HasEmployees"
- }
- }
- });
- $("#treeview").kendoTreeView({
- dataSource: homogeneous,
- dataTextField: "FullName",
- dragAndDrop: "true",
- drop:ondrop,
- });
- function ondrop(e)
- {
- var putdata = {
- 'StartNode': this.text(e.sourceNode),
- 'DestinationNode': this.text(e.destinationNode)
- }
- var datajson = JSON.stringify(putdata);
- console.log(datajson);
- $("#myconsole").text('');
- kendoConsole.log(this.text(e.sourceNode) + "," + this.text(e.destinationNode) )
- var putdata = $("#myconsole").text();
- var nodeearr = putdata.split(',');
- var StartNode = nodeearr[0];
- alert(nodeearr[0]);
- var DestinationNode = nodeearr[1];
- // alert(StartNode);
- $.ajax({
- type: 'PUT',
- url: "api/Employees/ChangeNode",
- data: { 'StartNode': StartNode, 'DestinationNode': DestinationNode },
- dataType: 'json',
- success:function(){
- alert("sucess")
- },
- error:function(){
- alert("OOPS")
- }
- })
- }
- </script>
- </body>
- </html>
From the script you can observe that the ondrop functionality is used to fetch the start and destination node and sent it through the PUT API (api/Employees/ChangeNode) request.
The ChangeNode Action method in the controller is responsible for manipulating the nodes position in the TreeView.

Change in SQL Table

Shweta ShuklaPosted May 26, 2017, 2:10 AM
Where you defined node changes in velow code: [HttpPut] [ActionName("ChangeNode")] public IHttpActionResult PutEmployee(NodeChange Node) { }
Arul RPosted Jan 17, 2016, 10:46 AM
Thanks for nice article
Sanjay KumarPosted Jan 6, 2016, 2:35 AM
Nice
Ankit BansalPosted Jan 6, 2016, 2:15 AM
Thanks for sharing
Gowtham RajamanickamPosted Jan 6, 2016, 1:20 AM
good article..
Gowtham RajamanickamPosted Jan 6, 2016, 1:20 AM
good article..
Shridhar SharmaPosted Jan 5, 2016, 11:58 PM
nice share
Santhakumar MunuswamyPosted Jan 5, 2016, 11:03 PM
Thanks for nice article
Raja TPosted Jan 5, 2016, 10:42 PM
Nice, Thanks for sharing
Kumaresh RajalingamPosted Jan 5, 2016, 8:53 PM
Good one sir