jsTree:如何从jstree所有叶子节点?(jsTree : How to get all le

2019-09-16 16:50发布

我想从jsTree所有叶子节点(ID和节点的文字)?

我不使用复选框UI:jsTree。

   Root
      -----A
           -----A1
                  -----A1.1

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

      -----B

           -----B2

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

我想从jsTree叶节点的列表。

预期输出:[A1.1,A2.1,B2,C1.1]

在这个问题上的任何帮助或指导,将不胜感激。

Answer 1:

像这样的事情?

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


Answer 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 


文章来源: jsTree : How to get all leaf nodes from jstree?