【问题标题】:Deleting root nodes in jstree detaches event listeners删除 jstree 中的根节点会分离事件侦听器
【发布时间】:2015-01-07 17:26:54
【问题描述】:

我想通过删除所有根节点并使用 ajax 读回结构来重新创建 jstree。如果我使用:

$("#tree").jstree("destroy");

然后重新创建 jstree 实例:

$("#tree").jstree({ "core": {"check_callback" : true} });

它绘制了一个新的 jstree。我添加了来自 ajax 的 json 信息,但是当我点击根节点时,事件监听器不再工作。

这是有效的解决方法:

var ref = $("#tree").jstree(true);          // get an existing instance
var nodes = ref.get_node("#").children; // -1 didn't work, so I used "#"
while (nodes.length > 0){
  ref.delete_node(nodes[0]);
}

“destroy”方法是否缺少任何参数,允许事件侦听器与新的 jstree 实例连接回来?

【问题讨论】:

    标签: javascript root jstree children destroy


    【解决方案1】:

    不,这是不可能的,因为destroy 函数“从 DOM 中删除所有 jstree 的痕迹并销毁所有实例”(jstree documentation):这意味着构成 jstree 的 jquery 对象实际上已被删除,并且它们占用所有的事件监听器都和他们在一起。

    我不知道你想要完成什么,但如果你需要使用destroy,你必须在重新创建树后再次重新添加所有监听器。

    否则你应该尝试找到一种非破坏性的方式来做你的事情,通常有一种方式,除了极少数情况外,nuke 选项从来都不是一个好主意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 2014-07-16
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多