Achyuth23
Achyuth23

Reputation: 411

Jstree - How to check child nodes if parent node is checked in Jstree?

If i am not wrong, when using jstree if parent node is checked then its child nodes would also get checked. Like thisenter image description here

What i am doing is, i am making an ajax call and loading only parent nodes. I I checked parent node and when i expand parent node, I am making another ajax call and loading only subsequent child nodes. After the call, parent node remain checked but its child nodes are not in checked state. just like in the following image.

enter image description here

Here is the code I am using:

$("#mytree").tree({
        ui : {
            theme_name : "checkbox"
        },
        plugins : { 
            checkbox : {
                real_checkboxes : true,
                real_checkboxes_names : function (n) { return [("check_" + (n[0].id || Math.ceil(Math.random() * 10000))), 1]; },
            }
        },
        data : {
            type : "json",              
            async : true,
            opts : { 
                method:"GET",
                url: "controller/getNodes"
            }   
        },
        callback : {
            beforedata: function(NODE,TREE_OBJ){
                return loadData(NODE,TREE_OBJ);
            }
        },
        selected : false,
        opened : "1"
    });

What i am trying to do is, When i make ajax call to load child nodes, after the call return, If parent node is checked then its child nodes should also be checked. Please help me, how to do it?

here is the code for loadData:

function loadData(Node,treeModule){
var treeNode = treeModule.get(Node,"json","");  
var data="";
    try{
        data= treeNode.data.title;
    }catch(err){
        data="";
    }
var Code = null;
if(data == "" || data == "Loading ..."){
     Code = "";
}else{
try{
        Code = Node[0].attributes.code.value;
    }catch(e){}
}
return {"code":Code,"isdialog":"false"};

}

I am looking for an even or something to do so.

Upvotes: 0

Views: 2901

Answers (2)

Jahirul Islam Bhuiyan
Jahirul Islam Bhuiyan

Reputation: 799

Add a attribule selected into your json object and set 'true' from server side if parent node is checked and

use the load_node.jstree event to set checkbox

.bind("load_node.jstree", function (event, data) { 
      data.inst.change_state('li[selected=selected]', false);
 })

Upvotes: 0

vakata
vakata

Reputation: 3886

Keep in mind you are using a very old jstree version which has not been supported for years. Migrating to v.3 will resolve all your issues (newly loaded nodes will be checked in your case), it will however take some time and reading to setup, as v.3 is a bit different from v.1.

Upvotes: 1

Related Questions