Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTREE - refresh jstree such that all code within 'bind("loaded.jstree")' is re-run

Tags:

jquery

jstree

On my page, i have a button, which on clicked, refreshes a jstree found on that page.

<input type="button" value="Test" onclick='$j("#demo2").jstree("refresh");'/>

Now, when the jstree loads, data is read from a 2D array and the node icons are changed according to the data found in that array. I need a function which will refresh or reload the jstree and at the same time display the proper icons for the nodes based on data read from the 2D array.

If i use '$j("#demo2").jstree("refresh");', the tree is reloaded but it does not retain its previous open state.

jstree code

$j("#demo2").jstree({
          "ui" : {
            "select_limit" : -1,
            "select_multiple_modifier" : "ctrl",
            "select_range_modifier" :"shift",
          },

          "json_data" : {
            "progressive_render" : false,
            "selected_parent_open": true,
            "ajax" : {
              "url" : "/json/test1.json"
            }
          },

          "plugins" : [ "themes", "json_data", "ui", "crrm", "contextmenu" ],

          'contextmenu' : {
          'items' : customMenu
          }

      })//end of jstree function


      /***************************************************************************
      When the jstree is first loaded, loop through the data retrieved
      from the database (stored previously in a 2d array called 'status_from_db') and
      select all jstree nodes found in that array.
      ***************************************************************************/
     $j("#demo2").bind("loaded.jstree", function (e, data) {    
      var inst = data.inst;
      var i;
      for (i = 0; i < status_from_db.length; ++i) {
          var node_name = status_from_db[i][0];
          $j("#demo2").find($j("li[name='"+node_name+"']")).each(function (k, v) {
            inst.select_node(v);

      /***************************************************************************
      Based on the retrieved data, assign the correct class to the variable 
      'selected_class and then modify the class of the <li> tag of the respective 
      node.
      ***************************************************************************/
            var node_strength = status_from_db[i][1];
            var node_add_strength = status_from_db[i][2];
            var selected_class;
            if (node_strength == "present" && node_add_strength == ""){
              selected_class = "jstree-icon4";
            }
            else if (node_strength == "present" && node_add_strength == "strong")  {
              selected_class = "jstree-icon3";
            }


            $j("li[name='"+node_name+"'] > ins").attr("class",selected_class);// set class to display new icon
            $j("li[name='"+node_name+"'] > a ins").attr("class",selected_class);// set class to display new icon    
          });
      }

   });

});

Is there a way of reloading/refreshing the tree such that all code within '$j("#demo2").bind("loaded.jstree", function (e, data) {' function is re-run?

like image 417
tanya Avatar asked Dec 20 '11 11:12

tanya


1 Answers

you could trigger the event yourself

$j("#demo2").trigger("loaded.jstree");
like image 93
Manuel van Rijn Avatar answered Sep 30 '22 20:09

Manuel van Rijn