0

I have a requirement to perform a recursive action whenever a node is checked/unchecked in JStree. I tried to do the following, but the function is never invoked. How should I call a function from JStree on checking/unchecking a node?

 $('#jstree').on("uncheck_node.jstree", function (e, data)
{
subfunction(selectednodetouncheck, data); 
};

function subfunction(para1, para2)
{
//some operation; 
}; 

The subfunction is never called.... Please let me know how to call this function.

1 Answers1

0

You should use select_node and deselect_node as below. If you want to check and select a node separately, you need to set the tie_selection param of checkbox plugin in the tree config as false and select/deselect a node manually.

Check demo - Fiddle Demo

$("#jstree")
    .jstree({
        core: {
            data: coredata,
            check_callback: true
        },
        plugins: ["checkbox"],
        checkbox: {
            tie_selection: false
        }
    })
    .on("select_node.jstree deselect_node.jstree", function(e, data) {
        subfunction(data);
    })
    .on("check_node.jstree uncheck_node.jstree", function(e, data) {
        subfunction2(data);
    });

function subfunction(data) {
    //some operation;
    alert('You got me selected: ' + data.node.state.selected)
};

function subfunction2(data) {
    //some operation;
    alert('You got me checked: ' + data.node.state.checked);

   //  now you need to decide what you want to select or not
   // and do it manually, e.g. like below
    var selectFlag = true; 

    if (selectFlag) {
        var action = data.node.state.checked ? 'select_node' : 'deselect_node';
        $("#jstree").jstree(action, data.node);
    }

};
Nikolay Ermakov
  • 5,031
  • 2
  • 11
  • 18
  • But is it an ideal way? select_node is different from check_node. what if there is separate function for select_node i.e. on selecting a node - it should perform something and check_node it should perform something else. – Aiswarya Rajagopalan Feb 01 '17 at 10:14
  • If you want to have it separately, you will have to manually select/deselect a node after it was checked/unchecked. And you will need to specify that you want to untie selecting a node from checking in the checkbox plugin config - I updated my answer and demo. – Nikolay Ermakov Feb 01 '17 at 12:14
  • Thanks. I found that I was doing a mistake with the parameters to invoke the function. $('#jstree').on("uncheck_node.jstree", function (e, data) { subfunction(data.node.id); }; //invokes the function – Aiswarya Rajagopalan Feb 02 '17 at 09:19