Checking a node checkbox programmatically with jsTree

In a tree built with jsTree, I have text inside a tag <a>

sitting in a variable. I would like to check that node. How can i do this?

I am currently finding node using jQuery and modifying its class. However, this does not fix the parent node, creating an undefined parent in its class. I tried doing $('.colors').jstree("checkbox_repair")

it but it didn't do anything.

It would be great if someone could answer both of these questions as they relate to the same problem.

Here is a jsFiddle illustrating the problem -> http://jsfiddle.net/thapar/5XAjU/

+3


source to share


2 answers


There are functions in js_tree .check_node ( node )

and .uncheck_node ( node )

I think this is what you are asking for. Here is the documentation here: http://www.jstree.com/documentation/checkbox

This is an excerpt from the documentation from the link above, "how to perform an operation":

/* METHOD ONE */
jQuery("some-selector-to-container-node-here")
    .jstree("operation_name" [, argument_1, argument_2, ...]);

/* METHOD TWO */
jQuery.jstree._reference(needle)
    /* NEEDLE can be a DOM node or selector for the container or a node within the container */
    .operation_name([ argument_1, argument_2, ...]);

      

So I think this syntax should work



$.jstree._reference(".colors").check_node('li#tree_3');

      

Also I'm not sure if you should be using a class to refer to your tree. Probably use id to refer to your tree and then use this syntax:

$.jstree._reference("#colors").check_node('li#tree_3');

      

// EDIT: Keep in mind that the new version of jsTree no longer has a function called _reference. It has been renamed to link (no main underscore). (Last checked on 08/24/2015 15:45 by @ mkli90) Link: https://www.jstree.com/api/#/?f= $ .jstree.reference (needle)

+5


source


If you want to check jsTree nodes on load, for example like this:

$(document).ready(function()
{
  $.jstree._reference('#menu').check_node('#pih2');
});

      

he does not work. The following works for me:



$(function () {
    $('#mainMenu1').bind('loaded.jstree', function(e, data){ //waiting for loading
          $.jstree._reference('#menu').check_node('#pih2');  //check node with id pih2
        $.jstree._reference('#menu').check_node('#pih6');  //check node with id pih6
  }); 
});

      

I am using jsTree 1.0-rc3 and JQuery 1.7.1. Aloe

+5


source







All Articles