Copied to clipboard

Flag this post as spam?

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


  • Daniel Draper 36 posts 57 karma points
    Apr 15, 2010 @ 13:51
    Daniel Draper
    0

    Custom Section Tree Child Node

    Hi Everyone,

    I know this has had some discussion in the past but all the posts I have read over the last few hours have all pretty much showed the solution with no explanation, in turn has left me with a failed solution.

    The code below is what I have for a sample but so far I am getting the following structure:

    Root
      - Parent 1
      - Parent 2

    What I want is:

    Root
      - Parent 1
        - Child 1

    public override void Render(ref XmlTree tree) {
    var node = XmlTreeNode.Create(this);
    node.NodeID = Guid.NewGuid().ToString();
    node.Text = "Parent";
    node.Icon = FolderIcon;
    node.OpenIcon = FolderIconOpen;
    node.Source = this.GetTreeServiceUrl(node.NodeID);
    tree.Add(node);

    var zNode = XmlTreeNode.Create(this);
    zNode.NodeID = Guid.NewGuid().ToString();
    zNode.Text = "Child";
    zNode.Icon = FolderIcon;
    zNode.OpenIcon = FolderIconOpen;
    tree.Add(zNode);
    }

    Any help would be appreciated, I assume there is something basic I am missing and have a feeling it's the source that is the problem?

  • Dirk De Grave 4539 posts 6017 karma points MVP 3x admin c-trib
    Apr 15, 2010 @ 14:24
    Dirk De Grave
    0

    Hi Daniel,

    It seems you're adding both nodes to the same tree, resulting in two parent nodes instead of a parent and child node.

    Render() method gets hit for each node (regardless of its level), meaning you'll have to add the zNode (from your example) when the "Parent" node is clicked on, and not adding the two at the same time (when "Root" is clicked)

    Another thing to be aware of: using Guid.NewGuid() as a value for NodeId is a bad idea as they will all evaluate to -1 (the code that sets the NodeId accepts a string but will try to convert to an int (hence the -1 value when TryParse() fails)

    Have a look at my article on how to build multi level navigation trees in a custom section (and avoid the pittfals you're experiencing now)

     

    Hope this helps.

    Regards,

    /Dirk

  • Daniel Draper 36 posts 57 karma points
    Apr 15, 2010 @ 15:00
    Daniel Draper
    0

    Dirk, thanks for your reply I have followed the first part of your tutorial which is great, but still can't get the child node to display. I just realised I am using Umbraco 4.1 does this change anything? The following code displays the parent but doesn't display the child.

    public override void Render(ref XmlTree tree) {
    if (this.id == -1) {
    var node = XmlTreeNode.Create(this);
    node.NodeID = "nParent";
    node.Text = "Parent";
    node.Icon = FolderIcon;
    node.OpenIcon = FolderIconOpen;
    node.Action = string.Format("javascript:openChildren('{0}');", node.NodeID);
    node.Source = GetTreeServiceUrl(node.NodeID);
    tree.Add(node);
    } else {
    var zNode = XmlTreeNode.Create(this);
    zNode.NodeID = "1";
    zNode.Text = "Child";
    zNode.Icon = FolderIcon;
    zNode.Action = string.Format("javascript:openChild('{0}');", zNode.NodeID);
    zNode.Source = GetTreeServiceUrl(zNode.NodeID);
    tree.Add(zNode);
    }
    }
  • Dirk De Grave 4539 posts 6017 karma points MVP 3x admin c-trib
    Apr 15, 2010 @ 15:19
    Dirk De Grave
    0

    Daniel,

    it shouldn't make a difference (*should* as in I haven't tried it yet on a v4.1 install), but still I think you're getting into trouble...

    your statement 

    var node = XmlTreeNode.Create(this);
    node
    .NodeID = "nParent";

    combined with .

    node.Source = GetTreeServiceUrl(node.NodeID);

    will not work as it will always revert to -1 as the NodeId when using GetTreeServiceUrl() (one of the things I'm warning for in my tutorial)

    I presume you're getting something like:

    -Parent
    --Parent
    ---Parent
    ----Parent

    you're going to have to use NodeKey to distinguish between different nodes (and different level of nodes) and use the value of this.NodeKey instead of this.id, unless you can set a unique id for each of the node in the tree (as in the Content tree)

    Let me know if you still don't get it to work.

     

    Cheers,

    /Dirk

     

     

     

     

  • Daniel Draper 36 posts 57 karma points
    Apr 15, 2010 @ 15:51
    Daniel Draper
    0

    I have been able to get the following structure:

    - Root
      - Parent

    I pretty much copied your code to see if I could get this working and still no joy. I assume I am missing something simple here...

    if (this.NodeKey == string.Empty) {
    var node = XmlTreeNode.Create(this);

    node.NodeID = "A";
    node.Text = "A";
    node.Icon = FolderIcon;
    node.Action = string.Format("javascript:openCustomersPage('{0}');", node.NodeID);

    node.Menu.Clear();
    node.Menu.AddRange(new List<IAction> { ActionNew.Instance, ActionRefresh.Instance });

    TreeService treeService = new TreeService(-1, TreeAlias, ShowContextMenu, IsDialog, DialogMode, app, string.Format("Letter-{0}", "A"));

    node.Source = treeService.GetServiceUrl();

    tree.Add(node);
    } else {
    XmlTreeNode node = XmlTreeNode.Create(this);

    node.NodeID = "3432";
    node.Text = "Sample Customer";
    node.Icon = FolderIcon;
    node.Action = string.Format("javascript:openCustomerPage('{0}');", node.NodeID);

    node.Menu.Clear();
    node.Menu.AddRange(new List<IAction> { ActionRefresh.Instance });

    TreeService treeService = new TreeService(-1, TreeAlias, ShowContextMenu, IsDialog, DialogMode, app, string.Format("NorthwindCustomer-{0}", "1"));

    node.Source = treeService.GetServiceUrl();

    tree.Add(node);
    }
  • Dirk De Grave 4539 posts 6017 karma points MVP 3x admin c-trib
    Apr 15, 2010 @ 16:16
    Dirk De Grave
    0

    Hi Daniel,

    Code seems fine to me... I think it's time for a debugging session... I'd suggest to attach debugger and check how the Render() method gets hit for each of the nodes in the content tree.

     

    Let us know what you find out.

     

    Cheers,

    /Dirk

Please Sign in or register to post replies

Write your reply to:

Draft