jsTree : How to get all leaf nodes from jstree?

2019-06-25 03:23发布

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

           -----A2
                  -----A2.1

      -----B

           -----B2

       -----C
            -----C1
                  -----C1.1

I want list of leaf nodes from jsTree.

Expected Output : [A1.1, A2.1, B2, C1.1 ]

Any help or guidance in this matter would be appreciated.

2条回答
冷血范
2楼-- · 2019-06-25 03:49

Something like this?

$('.jstree-leaf').each(function(){
  var id   = $(this).attr('id');
  var text = $(this).children('a').text();
});
查看更多
Animai°情兽
3楼-- · 2019-06-25 03:51
////////////////////////////////////////////////////////////////////
// @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 
查看更多
登录 后发表回答