Populate KendoUi Treeview with RavenDB documents

时间秒杀一切 提交于 2019-12-13 22:14:36

问题


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.



来源:https://stackoverflow.com/questions/13848228/populate-kendoui-treeview-with-ravendb-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!