[英]How to fire a select node event of a jsTree after re-populting it
I have a requirement of populating my jsTree and firing few ajax calls on selection of any node of jsTree which I am able to it. 我需要填充我的jsTree并在选择我能够做到的jsTree的任何节点时触发一些ajax调用。 But the problem is I have to re-populate the same tree and firing that ajax call again. 但是问题是我必须重新填充同一棵树,并再次触发该ajax调用。 After re-populating, I am not able to firing that SELECT or CLICK NODE event on selection of any tree node. 重新填充后,我无法在选择任何树节点时触发该SELECT或CLICK NODE事件。 Below is the code. 下面是代码。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript"
src="//static.jstree.com/3.2.1/assets/dist/jstree.min.js"></script>
<link rel="stylesheet"
href="//static.jstree.com/3.2.1/assets/dist/themes/default/style.min.css" />
<script>
$(document).ready(function(){
$("#tree").on("select_node.jstree", function(event, node) {
$("#para").text("");
var selected = node.instance.get_selected();
if(selected.length === 1) {
$("#para").text(selected[0]); // Here goes my ajax call.
} else if(selected.length > 1) {
alert("hi");
}
});
});
function getData(){
$('#tree').jstree("destroy").empty();
$('#tree').jstree({
'core' : {
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Root node1" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
}
});
}
</script>
</head>
<body>
<a id="go" href="#" onclick="getData()">Get Tree</a><br><br><br><br>
<div id="tree"></div>
<p id="para"></p>
</body>
</html>
You can paste this code directly in w3school site and test. 您可以将此代码直接粘贴到w3school网站并进行测试。 Not able make a jsFiddle of it. 无法做到这一点。
You can select a node programmatically by following line of code. 您可以通过以下代码行以编程方式选择节点。
$('#treeSelector').jstree().select_node('node_id');
It will trigger select_node & change event too. 它也会触发select_node和change事件。
Make a seperate function to add event listener.. then call that function from Doc ready, as well at the end of getData(). 制作一个单独的函数以添加事件侦听器。然后从Doc ready以及getData()的末尾调用该函数。 like 喜欢
$(document).ready(function(){
function addEventListener(){
$("#tree").on("select_node.jstree", function(event, node) {
$("#para").text("");
var selected = node.instance.get_selected();
if(selected.length === 1) {
$("#para").text(selected[0]); // Here goes my ajax call.
} else if(selected.length > 1) {
alert("hi");
}
});
}
addEventListener();
function getData(){
$('#tree').jstree("destroy").empty();
$('#tree').jstree({
'core' : {
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Root node1" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
}
});
addEventListener();
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.