在JSTree中获取子节点ID

时间:2019-02-04 15:00:49

标签: javascript jstree

我需要能够获得子节点ID。我的JS树在加载时已完全扩展,但是我无法获得子节点,因为它似乎在一段时间后加载了它们。

通过AJAX检索示例JSON:

[{"id":"L2","text":"L2","state": {"opened":true},"children":[{"id":"L1711","state":{"opened":true},"text":"cat"},{"id":"L1712","state":{"opened":true},"text":"dog"},{"id":"L1743","state":{"opened":true},"text":"bird"}]}]

我需要获取IDS,例如L1712,L1743等。

JS:

if ($('#features-tree').is(':empty')) {
                    $('#features-tree').jstree({
                        'core': {
                            'themes': {
                                'name': 'proton',
                                'icons': false
                            },
                            'data': {
                                'url': function (node) {
                                    return 'retrieveNodes?imageId=' + _this.imageId;
                                },
                                'data': function (node) {
                                    return { 'id': node.id };
                                },
                            }
                        },
                    });

但是,如果我执行以下控制台日志:

console.log($('div#features-tree > ul > li').html());

我只是回来:

<i class="jstree-icon jstree-ocl"></i><a class="jstree-anchor" href="#"><i class="jstree-icon jstree-themeicon-hidden"></i>Loading ...</a>

如何获取替换Loading...部分的节点?我需要获取它们,以便可以与页面的另一部分进行交互。

1 个答案:

答案 0 :(得分:0)

您可以通过流程输入json直接获取子级ID列表

let a=[{"id":"L2","text":"L2","state":  {"opened":true},"children":[{"id":"L1711","state":{"opened":true},"text":"cat"},{"id":"L1712","state":{"opened":true},"text":"dog"},{"id":"L1743","state":{"opened":true},"text":"bird"}]}];

let r= a.flatMap(x=>x.children.map(y=>y.id));

console.log(r);