admin管理员组

文章数量:1023263

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 badges
Add a ment  | 

2 Answers 2

Reset to default 2

Make 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.

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 badges
Add a ment  | 

2 Answers 2

Reset to default 2

Make 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