Search code examples
javascriptjqueryajaxjstree

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. 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.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. Not able make a jsFiddle of it.


Solution

  • 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();
          }
     })