Populate KendoUi Treeview with RavenDB documents

2019-09-12 06:17发布

问题:

I am using MVC4 and C#. I have a KendoUI Treeview and I'd like to populate it with data from RavenDB.

In the demo they use this:

public JsonResult Employees(int? id)
    {
        var dataContext = new NorthwindDataContext();

        var employees = from e in dataContext.Employees
                        where (id.HasValue ? e.ReportsTo == id : e.ReportsTo == null)
                        select new {
                            id = e.EmployeeID,
                            Name = e.FirstName + " " + e.LastName,
                            hasChildren = e.Employees.Any()
                        };

        return Json(employees, JsonRequestBehavior.AllowGet);
}

Notice the argument "id" - it's an int. In RavenDb document ID's are strings eg. myDco/231...

Imagine this is a standard document:

{
  "CreationDate": "12/12/2012 8:07:59 PM",
  "Name": "jkljklk",
  "Type": "kljkljkljkljkl",
  "Description": "ljkljklj",
  "CreatedByUser": "ljklklkljklj",
  "Deleted": false,
  "Status": "NEW",
  "Parent": "product/546"
}

How would I populate the treeview?

回答1:

There should not be any problem to use string instead of that int. You just need to fetch the needed records depending on that string parameter passed to the Action method.



回答2:

I figured this out.

I changed my RavenDB Document to include a list of all its children:

{
  "CreationDate": "12/12/2012 9:33:34 PM",
  "Name": "hohoho",
  "Type": "hohohoh",
  "Description": "hohohoh",
  "CreatedByUser": "hohohoh",
  "Deleted": false,
  "Status": "NEW",
  "Parent": "ohohohoh",
  "Children": [
  "item/1",
  "item/2",
  "item/3"
  ]
}

I then returned a list of my items to the View via the controller and then iterated through them, appending all the children to their correct parent nodes:

@(Html.Kendo().TreeView().Name("TreeView").DragAndDrop(true)
.Items(treeview =>
           {
               foreach (var myItem in Model)
               {
                   var myItemName = myItem.Name;
                   var children = myItem.Children;
                   treeview.Add().Text(myItemName ).Expanded(false).Items(branch =>
                                                                             {
                                                                                  if (children != null)
                                                                                     {
                                                                                         foreach (var child in children)
                                                                                         {
                                                                                             branch.Add().Text(child);
                                                                                         }
                                                                                     }
                                                                             });
               }
           }
   ))

Anyway, thanks for the responses :)

Hope this helps someone else one day.