鉴于 get_xml 在 jstree3 中不再可用,我需要同样的东西并提出以下解决方案
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 开始),那么延迟加载就可以了