遍历jsTree树

Traversing jsTree tree

示例数据:

1:A
    2:Aa
    3:Ab
    4:Ac
        5:Aaa
6:B
    7:Ba
    8:Bb
        9:Baa
        10:Bab

我正在尝试遍历 jsTree 树并获取每个项目的路径,包括父项和根,因此完成遍历后,我最终会得到以下输出:

[
    10: ["/6/8/10", "Bab",
    8: ["/6/8", "Bb",
    5: ["/1/4/5", "Aaa"],
    etc...
]

我该怎么做?

目前,我有这个:

$('.jstree-node,.jstree-leaf').each(function(){
    var id   = ($(this).attr('id').split("_"))[0];
    var text = $(this).children('a').text();
    $('#textarea').append(id + " - " + text + "\n");
});

这给了我每个项目的 ID 和文本,但是

我找到了解决方案:

$('.jstree-node,.jstree-leaf').each(function(){
    var id   = $(this).attr('id');
    var text = $(this).children('a').text();
    var path = tree.get_path( tree.get_node($(this)), "/", true);
    console.log( (id.split("_"))[0] + " - " + text + " -> (" + path + ")");
});

你试过get_json了吗?参见 docs