1

我在检索 jstree 中选定节点的状态时遇到了困难(使用 JSON 数据)。这是我尝试实现的方式:

$(function () {
    $("#tree").jstree({ 
        "json_data" : {
            "data" : [
                { 
                    data : "/", 
                    attr : { "id" : "root"},
                    state : "closed",
                    "children" : [ { "data" : "child1",
                                    "attr" : { "id" : "child1.id" },
                                    "children" : [ ] }
                                 ]
                },
            ]
        },
        "plugins" : [ "themes", "json_data", "crrm", "ui" ]
    })
    .bind("select_node.jstree",function(event, data) { . . . }

根据我的研究,我发现data.rslt.obj.attr("state")应该返回state("open" or "closed")但它返回未定义。你能帮我确定我在这里缺少什么吗?

4

3 回答 3

1

如前所述@digringo

$('#tree').jstree(true).get_state()

这将返回一个对象,该对象包括一个<li>打开的 id 数组和一个<li>选定的 id 数组。

然后,您可以遍历数组以查看特定子项是否打开和/或选择。

于 2021-08-24T20:27:07.940 回答
0

AFAICT,jsTree 不会将状态存储在任何地方(尤其是在查阅源代码之后),而是使用节点的类来确定它。

因此,我最好的建议是执行以下操作:

(...)
.bind("select_node.jstree", function (e, data) {
    var isOpen = data.rslt.obj.hasClass("jstree-open");
    console.log(data, 'selection is ' + (isOpen ? 'open' : 'closed'));
});
于 2013-09-17T22:41:23.940 回答
0

利用

$('#tree').jstree(true).get_state()
于 2021-06-23T10:07:55.587 回答