我很难理解 TreeView
的内部工作原理小部件。我指的是Kendo code library example ,特别是 Ajax 加载片段:
//Ajax binding data
public JsonResult Employees(string id)
{
XElement element = XElement.Load((Server.MapPath("~/App_Data/employees.xml")));
IEnumerable<Employee> result;
if (!string.IsNullOrEmpty(id))
{
//search for id and return it's children
result = FindByID(id, element.Element("Employee")).Element("items").Elements("Employee").Select(e => ToEmployee(e));
}
else
{
//return first level nodes
result = element.Elements("Employee").Select(e => ToEmployee(e)) ;
}
return Json(result, JsonRequestBehavior.AllowGet);
}
//Find the XML element by Id
private XElement FindByID(string id, XElement element)
{...}
//Convert XML element to Object
private Employee ToEmployee(XElement element)
{
return new Employee()
{
id = int.Parse(element.Element("employeeId").Value),
name = element.Element("name").Value,
hasChildren = element.Element("items") != null
};
}
这是使用的模型,它对应于实际的 XML 结构:
public class Employee
{
public int id { get; set; }
public string name { get; set; }
public bool hasChildren { get; set; }
public List<Employee> items { get; set; }
}
View 执行以下代码:
@(Html.Kendo().TreeView()
.Name("ajaxTree")
.DataTextField("name")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("Employees", "Home");
});
})
)
困扰我的是模型需要完全按照这个例子来实现。具体来说,需要以这种方式准确指定“id”和“hasChildren”属性。例如,将“id”修改为“Id”会导致此示例无效,
TreeView
不会加载。有人可以帮我解决以下问题吗?最佳答案
不确定您是否得到了问题的答案。
这个链接http://demos.kendoui.com/web/treeview/remote-data.html将帮助您了解模型数据如何绑定(bind)到 Treeview 。请通过以下链接。
对于问题 #2、#3
是的,可以如下配置您的模型属性:
schema: {
model: {
id: "EmployeeId",
hasChildren: "HasEmployees",
children: "EmployeeArray"
}
}
关于kendo-ui - Kendo UI Treeview 数据绑定(bind)到 XML : how does it bind to "id"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17043357/