如何判断jsTree是否已满载? [英] How can I tell if jsTree has fully loaded?
问题描述
我正在尝试编写一个在jsTree上打开特定节点的函数,但我遇到的问题是在从ajax调用加载基础树之前执行该函数。如何判断我的jstree数据是否已加载并等待加载完毕。下面是我尝试使用的函数。
I am trying to write a function that opens specific nodes on a jsTree but I am having a problem where the function is executed before my base tree is loaded from the ajax call. How can I tell if my jstree data has been loaded and wait until it is done loading. Below is the function I am trying to use.
function openNodes(tree, nodes) {
for (i in nodes) {
$('#navigation').jstree("open_node", $(nodes[i]));
}
}
我使用以下命令加载我的初始树
I am loading my initial tree with the following command
$("#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"]
});
推荐答案
我使用了setInterval和clearInterval:
I used setInterval and 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命令创建一个定时循环,在加载所需节点时退出。
JStree's ".loaded" callback only works for root nodes; "._is_loaded" may work instead of checking the node's length, but I haven't tried it. Either way, the animation settings cause nodes that are deeper in the tree to be loaded a few milliseconds later. The setInterval command creates a timed loop that exits when your desired node(s) is loaded.
这篇关于如何判断jsTree是否已满载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!