Reputation: 159
I have a list of nested objects (parent-child) and I use jstree plugin, to display them hierarchically.
db list:
ID Name ParentId
1 A NULL
2 B NULL
5 a1 1
6 b1 2
9 a11 5
10 a12 5
11 b12 6
converted list as json with their children:
[{"children":[{"children":[{"children":[],"id":9,"text":"a11","ParentId":5},{"children":[],"id":10,"text":"a12","ParentId":5}],"id":5,"text":"a1","ParentId":1}],"id":1,"text":"A","ParentId":null},{"children":[{"children":[{"children":[],"id":11,"text":"b12","ParentId":6}],"id":6,"text":"b1","ParentId":2}],"id":2,"text":"B","ParentId":null},{"children":[{"children":[],"id":9,"text":"a11","ParentId":5},{"children":[],"id":10,"text":"a12","ParentId":5}],"id":5,"text":"a1","ParentId":1},{"children":[{"children":[],"id":11,"text":"b12","ParentId":6}],"id":6,"text":"b1","ParentId":2},{"children":[],"id":9,"text":"a11","ParentId":5},{"children":[],"id":10,"text":"a12","ParentId":5},{"children":[],"id":11,"text":"b12","ParentId":6}]
ajax:
success: function (x) {
$('#jstree').jstree({
'core': {
'data': jQuery.parseJSON(x)
}
}); }
The problem is when the json objects first load to page, jstree displays all nodes even their childs; in the same level. Only If I expend a parent node, then children come to under their parent properly and disappear from first level. Why is this happening? Thanks.
Upvotes: 0
Views: 1086
Reputation: 3994
Although the structure of your JSON data is in the right format, the assigned 'id' values are not unique. Some of the nodes at the root have the same id as some of the nested children. This creates a conflict within the tree data as jsTree requires unique id's. If you can compute unique id values for nodes throughout your tree, it will work.
Upvotes: 0