private C_ORGANIZE_TREE GetUnitTree(List <C_ORGANIZE_TREE> mtmList, string Pid) { //根节点 C_ORGANIZE_TREE root = mtmList.Single(item => item.id == Pid); root.children = new List <C_ORGANIZE_TREE>(); //子节点 List <C_ORGANIZE_TREE> listParent = mtmList.Where(item => item.ParentID == Pid).ToList(); foreach (C_ORGANIZE_TREE t in listParent) { //C_ORGANIZE_TREE tm = new C_ORGANIZE_TREE(); //tm.id = t.id; //tm.text = t.text; //tm.ParentID = t.ParentID; //tm.Type = "Org"; //tm.iconCls = t.iconCls; if (mtmList.Where(item => item.ParentID == t.id).Any()) { root.children.Add(GetUnitTree(mtmList, t.id)); } else { root.children.Add(t); } } return(root); }
//这里借用下 C_ORGANIZE_TREE 对象 public List <C_ORGANIZE_TREE> GetUnitTree(List <C_ORGANIZE_TREE> mtmList, string Pid) { List <C_ORGANIZE_TREE> listTree = new List <C_ORGANIZE_TREE>(); List <C_ORGANIZE_TREE> listParent = mtmList.Where(item => item.ParentID == Pid).ToList(); if (!listParent.Any()) { return(null); } foreach (C_ORGANIZE_TREE t in listParent) { C_ORGANIZE_TREE tm = new C_ORGANIZE_TREE(); tm.id = t.id; tm.text = t.text; tm.ParentID = t.ParentID; tm.Type = type; tm.iconCls = iconCls;//若需要级别 的图标 上层只好改下图标样式了,这个默认本机到下级都是同一图标 tm.children = GetUnitTree(mtmList, t.id); listTree.Add(tm); } return(listTree); }
/// <summary> /// 遍历父节点添加子节点 /// </summary> /// <param name="listParentNode">父节点集合/param> /// <param name="parentNodeType">父节点类型</param> /// <param name="listChildNode">子节点集合</param> /// <param name="childNode">子节点样式</param> public void AddNode(List <C_ORGANIZE_TREE> listParentNode, string parentNodeType, List <C_ORGANIZE_TREE> listChildNode, string childNodeStyle) { if (listParentNode == null || listParentNode.Count == 0) { return; } foreach (C_ORGANIZE_TREE u in listParentNode) { List <C_ORGANIZE_TREE> listChildren = new List <C_ORGANIZE_TREE>(); foreach (C_ORGANIZE_TREE w in listChildNode.Where(t => t.ParentID == u.id && u.Type == parentNodeType).ToList()) { C_ORGANIZE_TREE tc = new C_ORGANIZE_TREE(); tc.id = w.id; tc.text = w.text; tc.ParentID = w.ParentID; tc.Type = w.Type; tc.iconCls = childNodeStyle; tc.children = w.children; listChildren.Add(tc); } if (listChildren.Count > 0) { if (u.children == null) { u.children = listChildren; } else { u.children.AddRange(listChildren); } } AddNode(u.children, parentNodeType, listChildNode, childNodeStyle); } }