/// <summary> /// 在节点及其子节点下找到指定ID的节点 /// </summary> /// <param name="pID"></param> /// <returns></returns> public ExtJSTreeNode Find(string pID) { if (this.ID == pID) { return(this); } // ExtJSTreeNode node = null; if (this.Children != null && this.Children.Count > 0) { foreach (var child in this.Children) { if (child != null) { node = child.Find(pID); } if (node != null) { break; } } } // return(node); }
/// <summary> /// 转换成ExtJS的TreeStore的JSON /// </summary> /// <param name="pIsMultiSelect">是否为多选,如果是,则所有节点都自动带checkbox</param> /// <param name="pIsSelectLeafOnly">是否为只能选择叶子节点,如果为是,则多选时的非叶节点不带checkbox</param> /// <param name="pSelectedNodes">选中的值,该项仅当多选模式时才会起作用</param> /// <returns></returns> public string ToTreeStoreJSON(bool pIsMultiSelect, bool pIsSelectLeafOnly, TreeNode[] pSelectedNodes) { List <ExtJSTreeNode> nodes = new List <ExtJSTreeNode>(); foreach (var item in this.InnerList) { var node = new ExtJSTreeNode() { ID = item.ID, Text = item.Text, ParentID = item.ParentID, IsLeaf = item.IsLeaf, IsPleaseSelectItem = item.IsPleaseSelectItem, create_time = item.create_time, ImageUrl = item.ImageUrl, PromotionItemCount = item.PromotionItemCount, Status = item.Status, NodeLevel = item.NodeLevel, DisplayIndex = item.DisplayIndex, CommissionRate = item.CommissionRate, ItemGroupType = item.ItemGroupType, Color = item.Color, IsPreference = item.IsPreference }; if (pIsMultiSelect) { node.IsChecked = false;//只要值不为null则就会带checkbox,但树项默认是不选中的 if (pSelectedNodes != null && pSelectedNodes.Length > 0) { foreach (var n in pSelectedNodes) { if (n != null && node.ID == n.ID) { node.IsChecked = true; break; } } } } // nodes.Add(node); } //根据节点的父节点,构建对象关系 for (int i = 0; i < nodes.Count; i++) { var node = nodes[i]; if (!string.IsNullOrEmpty(node.ParentID)) //如果有父节点 { //找到父亲节点 ExtJSTreeNode parent = null; for (int j = 0; j < nodes.Count; j++) { parent = nodes[j].Find(node.ParentID); if (parent != null) { break; } } if (parent != null) { if (parent.Children == null) { parent.Children = new List <ExtJSTreeNode>(); } parent.Children.Add(node); parent.IsLeaf = false; nodes.RemoveAt(i); i--; } } } //移除非叶节点的checkbox if (pIsMultiSelect && pIsSelectLeafOnly) { foreach (var node in nodes) { var notLeafNodes = node.FindNotLeafNodes(); if (notLeafNodes != null && notLeafNodes.Length > 0) { foreach (var notLeafNode in notLeafNodes) { notLeafNode.IsChecked = null; //非叶子节点不带checkbox } } } } //序列化 return(nodes.ToJSON()); }