Len Smith
Len Smith

Reputation: 197

Get child name of all sub nodes

I have to get the name of each child nodes under a clicked node. Suppose I send the clicked node in a function as:

getNodes(node){
$(node).children().each(function () {
  //want child nodes name under the sent node(node)
});
}

How do I get this working?

Upvotes: 0

Views: 1051

Answers (1)

Micha Roon
Micha Roon

Reputation: 4007

I've struggled to find a way. Here is what I came up with:

Instead of using jsTree, I used JQuery selectors. This works fine because the metadata of every node is stored in the jQuery data.

Here is how I went about it:

    $( "#treeDiv" ).jstree( {
       "json_data":{data:jsTree},
       "themes":{
          "theme":"classic",
          "dots":false,
          "icons":false
       },
       "plugins":[ "themes", "json_data", "ui" ]
    } ).bind( "select_node.jstree",
                function ( e, data ) {
                   $(data.rslt.obj).find("li").each( function( idx, listItem ) {
                      console.log( $(listItem).data("name") );
                   });
                } );

this will print all the names in the console. Provided you have added them as metadata. Please refer to the jsTree documentation for details on metadata: http://www.jstree.com/documentation/json_data

a second option is to replace the $(listItem).data("name") by $(listItem ).find("a" ).text()

This works nicely only for the last level as the leafs have their names in the tag, but parents will print a bit more.

hope this helped.

Upvotes: 1

Related Questions