public void AddChildNode(TreeNodeModel parentNode, IList<TreeNode> nodeList) { foreach (var node in nodeList.Where(n => n.ParentID == parentNode.ID).ToArray()) { TreeNodeModel childrenNode = new TreeNodeModel() { ID = node.ID, attr = new { id = node.ID, href = "#", check = !string.IsNullOrEmpty(node.Checked) ? true : false, type = string.IsNullOrEmpty(node.Type) ? "root" : node.Type }, data = node.Name }; parentNode.children.Add(childrenNode); AddChildNode(childrenNode, nodeList); } }
public JsonResult GetTreeInfo() { string dataSource = Request.Form["DataSource"]; string nodeImg = Request.Form["NodeImg"]; if (!string.IsNullOrEmpty(dataSource)) { IList<TreeNodeModel> roots = new List<TreeNodeModel>(); try { IList<TreeNode> nodeList = repository.ExecuteDataTable<eForm>(dataSource).ToList<TreeNode>(); // IList<TreeNode> rootLists = nodeList.Where(n => nodeList.FirstOrDefault(l => l.ID == n.ParentID) == null).ToList(); foreach (var node in nodeList.Where(n => string.IsNullOrEmpty(n.ParentID) || n.ParentID == "-1").ToArray()) { TreeNodeModel parentNode = new TreeNodeModel() { ID = node.ID, attr = new { id = node.ID, href = "#", check = !string.IsNullOrEmpty(node.Checked) ? true : false, type = string.IsNullOrEmpty(node.Type) ? "root" : node.Type }, data = new { title = node.Name, icon = "/Plugins/eCloud/Content/Themes/Default/Images/resource.png" }, state = "open" }; roots.Add(parentNode); AddChildNode(parentNode, nodeList); } return Json(roots); } catch (Exception ex) { log.Error(string.Format("执行SQL语句{0}失败,无法获取树形控件的数据源", dataSource), ex); } } return Json(null); }