admin管理员组文章数量:1429118
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. But the problem is I have to re-populate the same tree and firing that ajax call again. After re-populating, I am not able to firing that SELECT or CLICK NODE event on selection of any tree node. Below is the code.
<!DOCTYPE html>
<html>
<head>
<script src=".12.0/jquery.min.js"></script>
<script type="text/javascript"
src="//static.jstree/3.2.1/assets/dist/jstree.min.js"></script>
<link rel="stylesheet"
href="//static.jstree/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. Not able make a jsFiddle of 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. But the problem is I have to re-populate the same tree and firing that ajax call again. After re-populating, I am not able to firing that SELECT or CLICK NODE event on selection of any tree node. Below is the code.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript"
src="//static.jstree./3.2.1/assets/dist/jstree.min.js"></script>
<link rel="stylesheet"
href="//static.jstree./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. Not able make a jsFiddle of it.
Share Improve this question asked Mar 18, 2016 at 13:14 JaikratJaikrat 1,1544 gold badges25 silver badges49 bronze badges2 Answers
Reset to default 2Make a seperate function to add event listener.. then call that function from Doc ready, as well at the end of 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();
}
})
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.
本文标签: javascriptHow to fire a select node event of a jsTree after repopulting itStack Overflow
版权声明:本文标题:javascript - How to fire a select node event of a jsTree after re-populting it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745536062a2662275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论