当前位置: 首页 > 知识库问答 >
问题:

jstree记住打开/关闭的树节点

汪安宁
2023-03-14

我正在使用jstree来显示/管理一个类别树。我正在使用Ajax调用获取树,并返回以下响应:

0: {id:35, name:amrs trade, parent_id:null}
1: {id:36, name:trips, parent_id:35}
2: {id:37, name:tribute, parent_id:null}
3: {id:38, name:music, parent_id:null}
4: {id:39, name:recordings, parent_id:38}
5: {id:40, name:live shows, parent_id:38}
6: {id:41, name:others, parent_id:null}
7: {id:42, name:investments, parent_id:null}
8: {id:43, name:gold & silver, parent_id:42}
9: {id:44, name:debentures, parent_id:42}
10: {id:46, name:production, parent_id:35}
11: {id:54, name:real estate, parent_id:42}

我正在使用json_data选项来呈现jstree:

$("#incomeCategoryTree").jstree({
    "json_data" : {
        "data" : income,
        "progressive_render" : true
    },
    "plugins" : [ "themes", "json_data" ]
})
.bind("open_node.jstree close_node.jstree", function (e) {
    console.log(e);
});

我想记住用户所做的每个操作(打开或关闭任何树节点),因此我绑定了open_nodeclose_node操作。我遇到的问题是事件没有关于刚刚单击的节点的信息:

.Event {type: "open_node", timeStamp: 1365192438814, jQuery183029903660411946476: true, isTrigger: true, exclusive: undefined…}

currentTarget: div#incomeCategoryTree.jstree jstree-0 jstree-default jstree-focused
data: null
delegateTarget: div#incomeCategoryTree.jstree jstree-0 jstree-default jstree-focused
exclusive: undefined
handleObj: Object
isTrigger: true
jQuery183029903660411946476: true
namespace: "jstree"
namespace_re: /(^|\.)jstree(\.|$)/
result: undefined
target: div#incomeCategoryTree.jstree jstree-0 jstree-default jstree-focused
timeStamp: 1365192438814
type: "open_node"
__proto__: Object

我想我错过了,但是jstree docs是相当糟糕的imho和大多数选项甚至没有提到...

共有1个答案

阎功
2023-03-14

我设法做到了以下几点:

$("#incomeCategoryTree").jstree({
    "json_data" : {
        "data" : income,
        "progressive_render" : true
    },
    "plugins" : [ "themes", "json_data" ]
})
.bind("open_node.jstree close_node.jstree", function (event, data) {
    var state = event.type == "open_node" ? "open" : "closed";
    IncomeCategoryControl.setState(data.rslt.obj.attr("id"), state)
});

这是管理树数据的对象

var IncomeCategoryControl = {
    data: null,
    fetchData: function() {
        $.ajax({
            type: "GET",
            dataType: "json",
            context: this,
            async: false,
            url: "../php/client/json.php",
            data: {
                type: "incomeCategories"
            }
        }).done(function(response) {
            this.data = response;
        });
    },
    getData: function() {
        if (this.data == null) {
            this.fetchData();
        }
        return this.data;
    },
    setState: function(id, value) {
        var found = $(this.getData()).map(function() {
            return (this.id == id) ? this : null;
        });
        if (found.length) {
            found[0].state = value;
            return true;
        } else {
            return false;
        }
    }
};
 类似资料:
  • 数组有一个ID列表,需要在检查之前首先打开该列表。 当我运行上面的代码时,节点甚至在子节点展开之前就被检查了。 在我们执行其他步骤(如检查节点等)之前,jstree中是否存在等待子节点扩展完成的功能,

  • 问题内容: 情况是一个字符串,它导致如下所示: 因为该函数返回文本的摘要(摘要),所以它在某些单词之后停止。在这种情况下,强标签没有关闭。但是整个字符串都包裹在一个段落中。 是否可以将上述结果/输出转换为以下内容: 我不知道从哪里开始。问题是..我在网上找到一个执行正则表达式的函数,但是它将结束标记放在字符串后面..因此它无法验证,因为我要在段落标记中使用所有打开/关闭标记。我发现的功能这样做是错

  • 本文向大家介绍Android 打开关闭DrawerLayout,包括了Android 打开关闭DrawerLayout的使用技巧和注意事项,需要的朋友参考一下 示例            

  • 打开 M600 长按侧边电源按钮直至屏幕上出现 Polar 标志。 关闭 M600 导航至 Settings(设定)应用程式,滚动菜单找到并轻触 System(系统)。 轻触关机。 通过轻触核取标记图标确认关闭电源。

  • 本章讨论的是如何进入和退出CGDB。有如下几种方法: 在命令行下输入 'cgdb' 运行CGDB 在GDB窗口输入 'quit' 或者按下 'Ctrl+D' 退出CGDB 在源代码窗口输入 ':quit' 也可以退出CGDB。这在GDB挂起或者运行一条耗时很长的指令时也同样起作用

  • 我正在使用jsTree,到目前为止看起来还不错。 我有一个节点列表,它的id随着每一个新节点而增加,比如(g1,g2,g3……和一些其他节点,比如k1,k2,k3) 我可以通过使用 但是我想打开所有以'g'开头而不是'k'开头的节点,是不是可以使用$(id^=g)之类的东西? 更新: 这些节点是通过Web服务(如 从Web服务返回的数据被分配给jstree,因此我只需要打开id以'g'开头而不是'