javascript - 获取 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...
零件的节点?我需要获取它们,以便与页面的另一部分进行交互。
解决方案
您可以直接通过处理输入 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);
推荐阅读
- node.js - 如何通过 mongoose 将文档保存到 mogodb 数据库中,保存选项在 mongoose 中不起作用
- python - 使用 Apache 将文件上传到项目的 Django 获取 SuspiciousFileOperation 异常
- c# - 在引用连接表失败之前检查 null
- visual-studio-code - 如何在 VSCode 中为 JavaScript 的函数参数自动完成?
- python - 正则表达式查找两个 html 标签之间的所有内容
- css - 如何更改下拉菜单的高度?
- alexa - 如何为电子邮件地址创建自定义插槽
- r - 将 DataFrame 子集一列,然后在另一列中赋值
- javascript - 放置 $('#header').load('header.html') 脚本后,导航栏下拉菜单向左移动
- django - django设置中记录器和处理程序中级别的目的是什么?