Search code examples
javascriptyui

How to access the node being traversed in the YUI tree


I have created a tree data structure using YUI 3's tree.

I can traverse the tree can call a callback function.

I cannot figure out how to access the node being traversed from inside the callback function.

FWIW, I am new to YUI.

Any help is greatly appreciated.


Solution

  • The node that you're currently traversing will be passed in as the first parameter in the callback function. For example:

    var tree = new Y.Tree({
        nodes: [
            /* Add your nodes here */
        ]
    });
    
    var root = tree.rootNode;
    
    tree.traverseNode(root, function (node) {
        /* `node` will be the currently traversed node */
        console.log(node);
    });
    

    Here's an JSBin with working code, if you're still having any trouble getting this to work:

    http://jsbin.com/ekilom/2/edit

    Hope this helps!