Menu

How to know a node's depth level

Alexandre
2016-11-25
2016-11-30
  • Alexandre

    Alexandre - 2016-11-25

    Hi,

    I am looping all the nodes in a map with c.findAll().each { n -> ... } and I would like to know if it is possible to know what is the current node's depth level?

    Thanks,

    Alexandre

     
  • Alexandre

    Alexandre - 2016-11-28

    I did a function that returns a nodes' depth:

            def getNodeDepth(pNode, level) {
                if (pNode.parent == null)
                    return level
                else
                    getNodeDepth(pNode.parent, ++level)
                }
    

    Usage:

    myDepth = getNodeDepth(node, 0)
    

    Always call with '0' as the second parameter.

    Alexandre

     
  • Volker Börchers

    You could also use the builtin node.getNodeLevel or node.pathToRoot. This is especially handy in formulas:

    = "this node's level is " + node.getNodeLevel(false)
    = "this node's level is " + (node.pathToRoot.size() - 1)
    
     

    Last edit: Volker Börchers 2016-11-29
  • Alexandre

    Alexandre - 2016-11-29

    Thanks Volker I didn't know that function, I replaced my code.