Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Mar 24, 2010 @ 16:32
    Simon Dingley
    0

    Problems adding child nodes to custom tree node

    Thanks to those on Twitter that pointed me towards Dirks post on this subject matter however I am not really understanding how to achieve my goal, perhaps I am being particularly thick and missing something obvious but if anyone can shed some light on the solution I would appreciate it. I have "borrowed" some code from the Umbraco source to try and achieve my goal and here are my the two methods that are not currently rendering the required results:

            /// <summary>
    /// Renders the specified tree.
    /// </summary>
    /// <param name="tree">The tree.</param>
    public override void Render(ref XmlTree tree)
    {
        var customersNode = XmlTreeNode.Create(this);
        customersNode.NodeID = "1";
        customersNode.Text = "Customers";
        customersNode.Icon = FolderIcon;
        customersNode.OpenIcon = FolderIconOpen;
        customersNode.Action = "javascript:openCustomers();";
        customersNode.HasChildren = true;
        tree.Add(customersNode);
    
        var companiesNode = XmlTreeNode.Create(this);
        companiesNode.NodeID = "2";
        companiesNode.Text = "Companies";
        companiesNode.Icon = FolderIcon;
        companiesNode.OpenIcon = FolderIconOpen;
        companiesNode.Action = "javascript:openCompanies();";
        companiesNode.HasChildren = true;
        tree.Add(companiesNode);
    
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(tree.ToString());
        PopulateRolodex(ref xDoc, customersNode.NodeID);
        PopulateRolodex(ref xDoc, companiesNode.NodeID);
        tree = SerializableData.Deserialize(xDoc.OuterXml, typeof(XmlTree)) as XmlTree;           
    }
    
    /// <summary>
    /// Populates the rolodex.
    /// </summary>
    /// <param name="Tree">The tree.</param>
    /// <param name="ParentNode">The parent node.</param>
    private void PopulateRolodex(ref XmlDocument Tree, string ParentNode)
    {
        XmlNode root = Tree.SelectSingleNode(string.Format("/tree/tree[@nodeID='{0}']", ParentNode));
    
        for (int i = 97; i < 123; i++)
        {
            XmlElement treeElement = Tree.CreateElement("tree");
            treeElement.SetAttribute("menu", "");
            treeElement.SetAttribute("nodeID", i.ToString());
            treeElement.SetAttribute("text", ((char)i).ToString());
            treeElement.SetAttribute("action", "javascript:viewMembers('" + ((char)i).ToString() + "');");
    
            treeElement.SetAttribute("src", "");
    
            treeElement.SetAttribute("icon", FolderIcon);
            treeElement.SetAttribute("openIcon", FolderIcon);
            treeElement.SetAttribute("nodeType", "member");
    
    
            treeElement.SetAttribute("src",
                "tree.aspx?letter=" + ((char)i) + "&app=" + m_app + "&treeType=" +
                HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid());
    
            root.AppendChild(treeElement);
        }
    }

    When my tree is rendered I end up with a Customers node and then nodes representing each letter of the alphabet also in the root. What I actually need is as follows:

    Companies
     - a
     - b
     - c
     - ...
     - z

    Customers
     - a
     - b
     - c
     - ...
     - z

    Also what currently appears to happen is when I expand a node such as "a" I get a copy of all the root nodes as children.

    Thanks in advance.

    Simon

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 24, 2010 @ 16:43
    Dirk De Grave
    0

    Simon,

    Render() method gets hit for each node that is expandable, so your code fires each time meaning you get the duplicate structure

    Must first add a check to find out what node you'll be rendering. As per my article, I've done this by checking the NodeKey property. If that's empty, then I'm dealing with "top level" nodes. And all other nodes do get a "special" NodeKey property so I can differentiate between the different nodes.

    Also, should be using TreeService instead of setting the url yourself (some sample code can also be found on that specific article)

     

    Let me know if you need more help

     

    Cheers,

    /Dirk

     

  • Paul Sterling 718 posts 1534 karma points MVP 9x admin c-trib
    Mar 24, 2010 @ 16:50
    Paul Sterling
    2

    Simon -

    FWIW, here is what I use in the Commerce for Umbraco tree to create the same structure you describe:

    namespace CommerceForUmbraco.adminSection.trees
    {
        /// <summary>
        /// Renders a Tree containing all Products
        /// </summary>
        public class loadProductTree : BaseTree
            {
                public loadProductTree(string application) : base(application) { }

                protected override void CreateRootNode(ref XmlTreeNode rootNode)
                {
                    rootNode.Icon = FolderIcon;
                    rootNode.OpenIcon = FolderIconOpen;
                    rootNode.NodeType = "init" + TreeAlias;
                    rootNode.NodeID = "init";
                }

                public override void RenderJS(ref StringBuilder Javascript)
                {
                    Javascript.Append(
                        @"
                        function openProduct(id) {
                            parent.right.document.location.href = 'commerce/editProduct.aspx?p=' + id;
                        }
                    ");
                }

                public override void Render(ref XmlTree tree)
                {

                    string letter = string.Empty;
                    if (HttpContext.Current.Request.QueryString.ToString().IndexOf("letter") >= 0)
                    {
                        letter = HttpContext.Current.Request.QueryString.Get("letter");
                    }

                    if (letter != string.Empty)
                    {
                        // get collection values
                        Commerce.Common.ProductCollection products = null;


                        if (letter == "Others")
                        {
                            SubSonic.InlineQuery qry = new SubSonic.InlineQuery();
                            products = new ProductCollection();
                            products = qry.ExecuteAsCollection<ProductCollection>("select * from dbo.CSK_Store_Product where substring(productname, 1, 1) in ('1','2','3','4','5','6','7','8','9','0')");
                        }
                        else
                            products = new ProductCollection().Where(Product.Columns.ProductName, SubSonic.Comparison.Like, letter + "%").OrderByAsc("ProductName").Load();

                        foreach (Product thisProd in products)
                        {
                            XmlTreeNode xNode = XmlTreeNode.Create(this);
                            xNode.NodeID = thisProd.ProductID.ToString();
                            xNode.Text = thisProd.ProductName;
                            xNode.Action = "javascript:openProduct(" + thisProd.ProductID + ");";
                            xNode.Icon = "statistik.gif";
                            xNode.OpenIcon = "statistik.gif";

                            tree.Add(xNode);
                        }
                    }
                    else
                    {
                     
                        for (int i = 97; i < 123; i++)
                        {

                            XmlTreeNode xNode = XmlTreeNode.Create(this);
                            xNode.NodeID = i.ToString();
                            xNode.Text = ((char)i).ToString();
                            xNode.Source = "tree.aspx?letter=" + ((char)i) + "&app=" + m_app + "&treeType=" +
                                HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid();
                            xNode.Icon = FolderIcon;
                            xNode.OpenIcon = FolderIcon;

                            tree.Add(xNode);

            
                        }

                        //Add folder named "Others"
                        XmlTreeNode otherNode = XmlTreeNode.Create(this);
                        otherNode.NodeID = "0";
                        otherNode.Text = "Others";
                        otherNode.Source = "tree.aspx?letter=Others&app=" + m_app + "&treeType=" +
                            HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid();
                        otherNode.Icon = FolderIcon;
                        otherNode.OpenIcon = FolderIcon;

                        tree.Add(otherNode);

                    }
                }
            }

        }
  • Simon Dingley 1474 posts 3431 karma points c-trib
    Mar 24, 2010 @ 17:42
    Simon Dingley
    0

    Paul/Dirk thanks for your patience and assistance your help has been invaluable and I now have my tree structured exactly as required. Sometimes I just need a little nudge in the right direction and the light comes on ;)

    Paul, with respect, as you are already on the Core team I will mark Dirk's answer as the solution if thats ok.

Please Sign in or register to post replies

Write your reply to:

Draft