我正在尝试编写一个在jsTree上打开特定节点的函数,但是在从ajax调用加载我的基树之前,我遇到了一个问题。我如何知道我的jstree数据是否已经加载,并等待它加载完成。下面是我正在尝试使用的函数。
function openNodes(tree, nodes) {
for (i in nodes) {
$('#navigation').jstree("open_node", $(nodes[i]));
}
}
我正在使用以下命令加载我的初始树
$("#navigation").jstree({
"json_data": {
"ajax": {
"url": function(node) {
var url;
if (node == -1) {
url = "@Url.Action("BaseTreeItems", "Part")";
} else {
url = node.attr('ajax');
}
return url;
},
"dataType": "text json",
"contentType": "application/json charset=utf-8",
"data": function(n) { return { id: n.attr ? n.attr("id") : 0, ajax: n.attr ? n.attr("ajax") : 0 }; },
"success": function() {
}
}
},
"themes": { "theme": "classic" },
"plugins": ["themes", "json_data", "ui"]
});
发布于 2012-12-18 05:20:56
我使用了setInterval和clearInterval:
var interval_id = setInterval(function(){
// $("li#"+id).length will be zero until the node is loaded
if($("li#"+id).length != 0){
// "exit" the interval loop with clearInterval command
clearInterval(interval_id)
// since the node is loaded, now we can open it without an error
$("#tree").jstree("open_node", $("li#"+id))
}
}, 5);
JStree的".loaded“回调只适用于根节点;"._is_loaded”可能不会检查节点的长度,但我还没有尝试过。无论采用哪种方法,动画设置都会导致树中较深的节点在几毫秒后加载。setInterval命令创建一个计时循环,该循环在加载所需节点时退出。
发布于 2012-07-12 20:57:07
在对元素调用.jstree()之前,可以将回调绑定到before.jstree
和loaded.jstree
事件:
$(selector)
.bind('before.jstree', function(e, data) {
// invoked before jstree starts loading
})
.bind('loaded.jstree', function(e, data) {
// invoked after jstree has loaded
$(this).jstree("open_node", $(nodes[i]));
})
.jstree( ... )
发布于 2014-11-13 04:19:10
在较新版本的jstree中,您可能需要等待所有节点完成加载,然后才能与它们交互。为此,您需要:
ready.jstree
所以:
$(selector)
.bind('ready.jstree', function(e, data) {
// invoked after jstree has loaded
})
...
https://stackoverflow.com/questions/11452205
复制相似问题