[英]jsTree : How to get all nodes from jstree?
如何讓所有節點都出現在jsTree中?
我正在用xml構建jsTree
Root
-----A
-----A1
-----A1.1
-----A1.2
-----A2
-----`A2.1`
-----A2.2
-----B
-----B1
-----B2
-----C
-----C1
-----C1.1
-----C2.2
我想要jsTree中存在的所有節點(ID)的數組如下
預期產出:[Root,A,A1,A1.1,A1.2,A2,A2.1,A2.2,B,B1,B2,C,C1,C1.1,C2.2]
解決方案示例:)
var xmlString = $("#tree").jstree("get_xml");
var xmlDOM = $.parseXML(xmlString);
var IDList =[];
var items = $(xmlDOM).find('root item');
$.each (items, function(key, val){
IDList.push($(val).attr('id'));
})
IDList.pop();
xmlString =
<root>
<item id="A" parent_id="0" state="close">
<content><name>Charles Madigen</name></content>
</item>
<item id="A1" parent_id="A" state="close">
<content><name>Charles Madigen</name></content>
</item>
.
.
</root>
輸出:Root,A,A1,A1.1,A1.2,A2,A2.1,A2.2,B,B1,B2,C,C1,C1.1,C2.2
:)
您可以遍歷每個節點元素並通過以下方式將其ID放入數組中:
var idList = [];
var jsonNodes = $('#tree').jstree(true).get_json('#', { flat: true });
$.each(jsonNodes, function (i, val) {
idList.push($(val).attr('id'));
})
var treeData = $('#MyTree').jstree(true).get_json('#', {flat:false})
// set flat:true to get all nodes in 1-level json
var jsonData = JSON.stringify(treeData );
我需要相同的東西並提出以下解決方案,因為jstree3中不再提供get_xml
function get_jstree_order(root_ul_selector, children) {
var output = [];
var _this = this;
if (typeof children === 'undefined') {
children = $(root_ul_selector).find('> li');
}
children.each(function() {
if ($(this).find('ul').length > 0) {
output.push({
id: $(this).attr('id'),
children: get_jstree_order(root_ul_selector, $(this).find('ul > li'))
});
return;
}
output.push({
id: $(this).attr('id'),
children: false
})
});
return output;
}
console.log(get_jstree_order('#mytree > ul'));
輸出(轉換為JSON以提高可讀性):
[
{
"id": "1",
"children": false
},
{
"id": "2",
"children": false
},
{
"id": "5",
"children": [
{
"id": "6",
"children": false
},
{
"id": "7",
"children": false
}
]
},
{
"id": "8",
"children": false
},
{
"id": "9",
"children": false
},
{
"id": "10",
"children": false
},
{
"id": "11",
"children": false
}
]
根據需要修改; 包括什么需要,但我的目的只是為服務器端處理獲得正確的項目順序。
如果延遲加載,孩子的id獨立於父母(例如,父母的第一個孩子總是從1開始),這很好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.