Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 430 posts 1023 karma points
    Aug 21, 2017 @ 15:25
    Ayo Adesina
    0

    How to render Child node in custom tree

    I'm creating a custom section with a custom tree in Umbraco 7.6

    I have managed to create the nodes I want to see, however the child nodes are not showing up under the correct parent node, they are all showing up in the root.

    How do you tell umbraco to render the node in the correct place?

    When using the CreateTreeNode() to create the nodes one of the paras is parentId I'm pretty sure I am setting this correctly but yet the nodes still all show up on one level.

    Any ideas? This is what I have got so far.

    if (id == Constants.System.Root.ToInvariantString())
            {
                var nodes = new TreeNodeCollection();
    
                foreach (var opco in opCoService.GetAll(c => c.Brands))
                {
                    var node = CreateTreeNode(opco.Id.ToString(), "-1", queryStrings, opco.CompanyName, "icon-folder", false);
    
                    nodes.Add(node);
    
                    if (opco.Brands.Any())
                    {
                        foreach (var brand in opco.Brands)
                        {
                            var childNode = CreateTreeNode(brand.Id.ToString(), opco.Id.ToString(), queryStrings, brand.BrandName);
                            nodes.Add(childNode);
                        }
    
                    }
    
                }
                return nodes;
            }
    
  • Tobias Klika 101 posts 570 karma points c-trib
    Aug 21, 2017 @ 22:59
    Tobias Klika
    101

    Hi.

    The root ID is always "-1" (that's the Constants.System.Root).

    If you open a sub-tree, it isn't already available in the backoffice but is requested via an async request (just look in the dev tools).

    The request calls your GetTreeNodes() method with the id parameter you have clicked on.

    So every time GetTreeNodes() is called, you can only return ONE level of hierarchy. That's why your CreateTreeNodes() aren't creating a nested tree.

    What you have to do is the following. Divide your method into ID groups, so you can return the correct trees.

    I have altered your code like this:

    protected override TreeNodeCollection GetTreeNodes(string id, System.Net.Http.Formatting.FormDataCollection queryStrings)
    {
      var nodes = new TreeNodeCollection();
    
      // render top-level tree (for id = -1)
      if (id == Constants.System.Root.ToInvariantString())
      {
          foreach (var opco in opCoService.GetAll(c => c.Brands))
          {
              var node = CreateTreeNode(opco.Id.ToString(), Constants.System.Root.ToString(), queryStrings, opco.CompanyName, "icon-folder", false);
              nodes.Add(node);
          }
      }
      // render sub-tree if id != -1
      else 
      {
        // this line is pseudo-code, as I don't know how your service looks like
        // but you'll get the idea ;-)
        var opco = opCoService.GetById(id);
    
        if (opco != null && opco.Brands.Any())
        {
            foreach (var brand in opco.Brands)
            {
                var childNode = CreateTreeNode(brand.Id.ToString(), opco.Id.ToString(), queryStrings, brand.BrandName);
                nodes.Add(childNode);
            }
        }
      }
    
      return nodes;
    }
    

    If you have more than two levels in your tree you could (just an idea) also add a prefix to the id, like so:

    var node = CreateTreeNode("opco-" + opco.Id.ToString(), Constants.System.Root.ToString(), queryStrings, opco.CompanyName, "icon-folder", false);
    
    var childNode = CreateTreeNode("brand-" + brand.Id.ToString(), "opco-" + opco.Id.ToString(), queryStrings, brand.BrandName);
    

    This will give you the ability to check if the ID is from a brand, opco and so on.

    if (id.StartsWith("opco-")) ... // output brand tree
    else if (id.StartsWith("brand-")) ... // output sub-nodes of a brand
    // ... and so on
    
  • Ayo Adesina 430 posts 1023 karma points
    Aug 22, 2017 @ 06:56
    Ayo Adesina
    0

    I didn't actually realise that thats how it works.... I have a pretty complex tree to build this was just a test... good thing I know this now!

    I have got it working now, thanks.

Please Sign in or register to post replies

Write your reply to:

Draft