Search code examples
javascriptc#umbraco7

Get parent node in custom section tree


I have a multi level tree in my custom-section of Umbraco 7, where the root node is always the country.

  • Denmark
    • Item 1
    • Item 2
  • Sweeden
    • Item 1
    • Item 2
  • Norway
    • Item 1
    • Item 2

Now, when working with Item 2 of Denmark, i would like to get the node-ID of the root-node (Denmark) so that i know what Country ID i'm working under.

Is there any JavaScript API to recurse back over the node levels, and get the top-most expanded?

Thanks in advance. Jonas


Solution

  • What I did was to use editorState
    (I'm running Umbraco 7.5.7)

    angular.module("umbraco").controller("Client.Controller",
      function ($scope, editorState) {
    
        // Get the rootNode out of the path.
        var rootNode = editorState.current.path.split(",")[1];
    });
    

    Since the path contains the entire structure where the current node is located, the second first node would be the root node.


    Another solution might be this:
    (Haven't tested this myself)
    https://our.umbraco.org/forum/developers/extending-umbraco/73796-how-to-get-the-parentid-of-the-current-node-in-custom-tree-using-angularjs

    Which in a simple way states:

    inject appState and treeService into your angular controller

    This will get the top most node of the current tree:
    var rootNode = appState.getTreeState("currentRootNode");

    This will get the current selected node in the custom tree
    var selectedNode = appState.getTreeState("selectedNode");