填充KendoUi树视图与RavenDB文件 [英] Populate KendoUi Treeview with RavenDB documents
问题描述
我使用MVC4和C#。
我有一个KendoUI树形,我想从RavenDB数据来填充它。
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);
}
注意参数ID - 这是一个int。在RavenDb文件的ID都是字符串如。 myDco / 231 ...
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?
推荐答案
我想通了这一点。
我改变了我的RavenDB文件包含所有的儿童名单:
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.
这篇关于填充KendoUi树视图与RavenDB文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!