JQuery 函数,用于查找 jstree 中节点的状态

JQuery function to find state of a node in jstree?

本文关键字:节点 状态 jstree 查找 函数 用于 JQuery      更新时间:2023-09-26

我在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")但它返回未定义。你能帮我确定我在这里缺少什么吗?

@digringo所述

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

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

然后,您可以循环访问数组,以查看特定子项是否已打开和/或选中。

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'));
});

使用

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