static void ConfigureTreeViewNode(this GridViewRow row, IHierarchy item, Func <IHierarchy, bool> isItemCollapsed) { var parent = item.GetParent(); var isVisible = true; if (parent != null) { isVisible = !isItemCollapsed(parent); } var isCollapsed = isItemCollapsed(item); var isRoot = parent == null; var isLeaf = item.GetChildren().None(); // setting up grid view row attributes row.Attributes["itemid"] = item.GetId().ToString(); row.CssClass += " {0}{1}{2}{3}".FormatWith( item.GetAllParents().Select(a => a.GetId()).ToString(" "), " treeview-leaf-node".OnlyWhen(isLeaf), " treeview-root-node".OnlyWhen(isRoot), " collapsed".OnlyWhen(isCollapsed && !isLeaf)); if (!isVisible) { row.Style["display"] = "none"; row.Attributes["collapsedfor"] = parent.GetId().ToString(); } // Creating additional controls var spacerSpan = CreateSpacer(item.GetAllParents().Count()); var collapseIcon = CreateLink(item.GetId().ToString()); // putting additional controls in the right place var anchorControl = FindAnchorControl(row); var parentToAdd = anchorControl?.Parent ?? row.Cells[0]; var controlIndex = anchorControl == null ? 0 : parentToAdd.Controls.IndexOf(anchorControl); parentToAdd.Controls.AddAt(controlIndex, collapseIcon); parentToAdd.Controls.AddAt(controlIndex, spacerSpan); }
/// <summary> /// Adds a hierarchy of nodes for a specified hirarchical item. /// </summary> public static void Add(this TreeNodeCollection nodes, IHierarchy item) { var node = new TreeNode(item.Name, item.GetId().ToString()); foreach (var child in item.GetChildren()) { node.ChildNodes.Add(child); } node.CollapseAll(); node.SelectAction = TreeNodeSelectAction.None; nodes.Add(node); }