Skip to content Skip to sidebar Skip to footer

Jstree : How To Get All Leaf Nodes From Jstree?

I want to get all leaf-nodes(ID & text of node) from jsTree ? I am not using checkbox ui : jsTree. Root -----A -----A1 -----A1.1

Solution 1:

Something like this?

$('.jstree-leaf').each(function(){
  var id   = $(this).attr('id');
  var text = $(this).children('a').text();
});

Solution 2:

////////////////////////////////////////////////////////////////////
// @name        :
// @description :   function
// @params      :
// @return      :
////////////////////////////////////////////////////////////////////
function jstreeIterateNodes(
    treeOwner ,
    node , 
    fnCallbackCondition ,
    bRecursive ,
    arrCollector
){

    var 
        childNodes = node.children ,
        arrCollector = ( arrCollector ) ? arrCollector : [] ,
        bUseCallback = ( typeof fnCallbackCondition === "function" ) ? true : false ,
        nodeItreator = null;

    for(
        var i = 0;
        i < childNodes.length;
        ++i
    ){

        nodeItreator = treeOwner.get_node( childNodes[i] );

        if( bUseCallback ){

            if( fnCallbackCondition( nodeItreator , node ) ){

                arrCollector.push({
                    node : nodeItreator,
                    parent : node
                });

            }

        }
        if( bRecursive ){
            jstreeIterateNodes(
                treeOwner ,
                nodeItreator ,
                fnCallbackCondition ,
                arrCollector
            ); 
        }
    }

    return arrCollector;
}
var 
    _your_jstree_ = $("#YOUR_TREE_ELEMENT").jstree( true ) ,
    bRecursiveItreation = false , // could be true for recursive
    arrCollected = null;

arrCollected = jstreeIterateNodes( 
    _your_jstree_ , 
    _your_jstree_.get_node( "some_node_id" ) , 
    function _your_filter_callback_( itrated_node , parent_itrated_node ){
      // Do what ever and return true to collect
    } ,
    bRecursiveItreation 
);

// DO SOMTHING WITH arrCollected which contains all collected nodes 

Post a Comment for "Jstree : How To Get All Leaf Nodes From Jstree?"