简体   繁体   English

从fancytree插件获取所有节点

[英]Get All node from fancytree plugin

I am trying to got list of all node from tree, when click button outside tree. 当树外单击按钮时,我试图从树中获取所有节点的列表。 I got list of all selected node from tree, but having problem with got list of all node from tree. 我从树中得到了所有选定节点的列表,但是从树中得到了所有节点的列表有问题。

Here is my code: 这是我的代码:

$(document).on('click', '.del', function () {
    var selKeys = $.map($('#tree1111').fancytree('getTree').getSelectedNodes(), function (node) {
        return node;
    });
});

I try this but not working 我尝试了这个但是没用

var allKeys = $.map($('#tree1111').fancytree('getTree'), function (node) {
    return node;
});

$.each(allKeys, function (event, data) {
alert(data.key)
});

You simply have to access the source option. 您只需要访问source选项。

If the tree is modified dynamically, you need to recover the children of the root node, and get only the properties you need. 如果动态修改树,则需要恢复根节点的子节点,并仅获取所需的属性。 You have a lot of possible ways to do it . 您有很多可能的方法可以做到这一点

 var source = [ {title: "Node 1", key: "1"}, {title: "Folder 2", key: "2", folder: true, children: [ {title: "Node 2.1", key: "3"}, {title: "Node 2.2", key: "4"} ]} ]; $('#tree').fancytree({ source: source }); // Get the tree var tree = $('#tree').fancytree('getTree'); // Dynamically add new node var folder2 = tree.findFirst('Folder 2'); folder2.addNode({title:'New node'}); // original data var source = tree.options.source; $('#original').html(JSON.stringify(source,null,2)); // get the tree nodes var root = tree.getRootNode(); var nodes = root.children; console.log(root); console.log(nodes); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.fancytree/2.15.0/skin-win8/ui.fancytree.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.fancytree/2.15.0/jquery.fancytree-all.js"></script> <div id="tree"></div> <b>Original data</b> <pre id="original"></pre> <b>Data recovered from modified tree:</b> Please, see the console. It cannot be directly converted to JSON because it has circular references (child to parent) 

You can try like this 你可以这样尝试

var allKeys = $.map($('#tree1111').fancytree('getRootNode').getChildren(), function (node) {
        return node;
    });

$.each(allKeys, function (event, data) {
alert(data.key)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM