首页 > 解决方案 > 获取 JSTree 中的子节点 ID

问题描述

我需要能够获取子节点 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...零件的节点?我需要获取它们,以便与页面的另一部分进行交互。

标签: javascriptjstree

解决方案


您可以直接通过处理输入 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);


推荐阅读