Javascript “如何”;id";使用JSON填充jsTree
我使用替代的JSON格式来填充我的jsTree,如下图所示 问题是我找不到jsTree中的“id”属性是如何工作的?Javascript “如何”;id";使用JSON填充jsTree,javascript,jquery,json,jstree,Javascript,Jquery,Json,Jstree,我使用替代的JSON格式来填充我的jsTree,如下图所示 问题是我找不到jsTree中的“id”属性是如何工作的? [ { "id": "3", //Any value except 3 and 5 works "text": "Ford", "parent": "3", "icon": "fa fa-circle-o" }, { "id": "5", //Any value except 3 and 5 wor
[
{
"id": "3", //Any value except 3 and 5 works
"text": "Ford",
"parent": "3",
"icon": "fa fa-circle-o"
},
{
"id": "5", //Any value except 3 and 5 works
"text": "Fiat",
"parent": "5",
"icon": "fa fa-circle-o"
},
{
"id": "3",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
},
{
"id": "5",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
}
]
在我的情况下,父母有时可以有相同的名字。(这并不理想,但这是一个罕见的情况,弹出,所以我必须处理它)。所以我在树中使用它们唯一的“id”。现在,如果父节点的子节点与父节点具有相同的“id”,那么jsTree在单击按钮时不知何故不起作用,并继续显示“Loading…”消息
我的JSON:
[
{
"id": "3", //Any value except 3 and 5 works
"text": "Ford",
"parent": "3",
"icon": "fa fa-circle-o"
},
{
"id": "5", //Any value except 3 and 5 works
"text": "Fiat",
"parent": "5",
"icon": "fa fa-circle-o"
},
{
"id": "3",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
},
{
"id": "5",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
}
]
我的html代码:
<div id="car-list" class="collapse">
#{MyBean.jsonString}
</div>
<h:outputScript>
$(function() {
var data1= JSON.parse(document.getElementById("car-list").innerHTML);
$('#car-list').jstree({
'core': {
'data': data1
}
});
});
</h:outputScript>
#{MyBean.jsonString}
$(函数(){
var data1=JSON.parse(document.getElementById(“car list”).innerHTML);
$(“#车辆列表”).jstree({
“核心”:{
“数据”:数据1
}
});
});
其中,
jsonString
是一个字符串对象,在jstree中包含上述json,每次我有2个相同的键时,我只能看到一个元素呈现,而不是2个(谈论具有相同键的元素)。在您的例子中,我认为问题在于节点中存在循环,因此渲染永远不会结束。我通过从串联(parentKey\u childKey)创建键克服了我的问题。因此,在您的情况下,这将是:
[
{
"id": "3_3",
"text": "Ford",
"parent": "3",
"icon": "fa fa-circle-o"
},
{
"id": "5_5",
"text": "Fiat",
"parent": "5",
"icon": "fa fa-circle-o"
},
{
"id": "3",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
},
{
"id": "5",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
}
]
您还可以看到,不支持具有相同键的多个节点。在jstree中,每次我有两个相同的键时,我只能看到一个元素渲染,而不是2个(谈论具有相同键的元素)。在您的例子中,我认为问题在于节点中存在循环,因此渲染永远不会结束。我通过从串联(parentKey\u childKey)创建键克服了我的问题。因此,在您的情况下,这将是:
[
{
"id": "3_3",
"text": "Ford",
"parent": "3",
"icon": "fa fa-circle-o"
},
{
"id": "5_5",
"text": "Fiat",
"parent": "5",
"icon": "fa fa-circle-o"
},
{
"id": "3",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
},
{
"id": "5",
"text": "Cars",
"parent": "#",
"icon": "glyphicon glyphicon-triangle-right"
}
]
您还可以看到,不支持具有相同键的多个节点。节点中的循环是什么意思?您提供的parentId等于id。因此,这会试图同时将同一节点呈现为父节点和子节点。您可以参考阅读此行为的文档吗。正如我在问题中提到的,我在这里找不到描述“id”如何工作的文档。旧文档说jstree使用id作为jquery选择器。Html ID应该是唯一的。至于呈现过程,我所说的是一种怀疑,因为我没有参与jstree的实现。你说节点中的循环是什么意思?你提供的parentId等于id。因此,不知何故,这试图同时呈现同一节点作为父节点和子节点。你能参考你阅读此行为的文档吗。正如我在问题中提到的,我在这里找不到描述“id”如何工作的文档。旧文档说jstree使用id作为jquery选择器。Html ID应该是唯一的。至于呈现过程,我所说的是一种怀疑,因为我没有参与jstree的实现。