我正在使用来自http://www.jeasyui.com的名为“tree”的控件。但在我的情况下,我想在第一个最高级别加载,然后单击其中一个节点加载它的子节点。
<ul id="tt" checkbox="true" animate="true"></ul>
$(function() {
$('#tt').tree({
data: @Html.Raw(Model.Tree)
});
});
在此函数上,我从 DB 中获取所选节点的子节点:
$(function() {
$('#tt').tree({
onBeforeExpand: function(node) {
var hospitalId = node.id;
$.getJSON('@Url.Action("LoadDepartments")', { hospitalId: hospitalId }, function () {
});
}});});
[HttpGet]
public ActionResult LoadDepartments(Guid hospitalId)
{
LoadHospitals();
var departments = _templateAccessor.GetDepartments(hospitalId);
var hospital = tree.Where(obj => obj.id == hospitalId.ToString()).FirstOrDefault();
if (hospital != null)
{
foreach (var department in departments)
{
DataTreeModel dep = new DataTreeModel();
dep.id = department.Id.ToString();
dep.text = department.Name;
dep.state = "closed";
hospital.children.Add(dep);
hospital.state = "open";
}
}
var result = SerializeToJsonString(tree);
return Json(result, JsonRequestBehavior.AllowGet);
}
在方法 LoadDepartments 我有正确的结构,但树没有显示新元素。问题是如何清理树的先前内容并用新内容填充它,也许我做错了什么?