3

I have created one jquery jstree and it's working fine. Now the problem is how to get the the checked nodes details.

For Creating JStree The code is:

$(function () {
$("#tree").jstree({ 
    "json_data" : {
        "data" : [
            {"data":"pe_opensourcescanning","id":0,"pId":-1,"children":  [{"data":"tags","id":30,"pid":0},{"data":"branches","id":29,"pid":0},{"data":"trunk","id":1,"pid":0,"children":[{"data":"import-export","id":28,"pid":1},{"data":"custom_development","id":12,"pid":1},{"data":"Connectors","id":7,"pid":1},{"data":"support","id":6,"pid":1},{"data":"Installation-Configuration","id":5,"pid":1},{"data":"backup","id":2,"pid":1}]}]}
        ]
    },
    "plugins" : [ "themes", "json_data", "checkbox", "ui" ]
}).bind("select_node.jstree", function (e, data) { alert(data.rslt.obj.data("id")); });

Now while getting checked nodes i need all the attributes values for those checked elements. Say like for "tags" the json object looks like {"data":"tags","id":30,"pid":0}, so if user select tag i need the value of "data" And "id". i have tried to write some code but unfortunately that is not working.

Getting Checked Nodes.

$("#" +div2.childNodes[i].id).jstree("get_checked",null,true).each 
      (function () { 
         alert(this.data);
         alert(this.id);

 }); 

Kindly give me a solution.

Shibankar
  • 806
  • 3
  • 16
  • 40

5 Answers5

16

As the Author of jstree (Ivan Bozhanov) points out on google-Groups Discussion regarding get_checked, it can also be achieved using the following:

$('#tree').jstree(true).get_selected();

This returns a List of the IDs, e.g. ["j1_2"] or ["j1_2", "j1_3", "j1_1"]

Check out the fiddle by Ivan Bozhanov himself on: jsfiddle-Example get_selected

leole
  • 439
  • 9
  • 16
7
  function submitMe(){ 
        var checked_ids = []; 
        $("#server_tree").jstree("get_checked",null,true).each 
            (function () { 
                checked_ids.push(this.id); 
            }); 
           doStuff(checked_ids); 

Go through this once jstree google groups

Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61
  • or do check this question http://stackoverflow.com/questions/11049874/how-to-get-all-checked-nodes-in-jstree – Vaibs_Cool Aug 16 '13 at 07:56
  • The above code is working fine as far as push is concerned. but how do get the contents of 'checked_ids'. please give me an example. – Shibankar Aug 16 '13 at 09:43
  • document.getElementById('jsfields').value = checked_ids.join(","); – Vaibs_Cool Aug 16 '13 at 10:03
  • Thank you for the follow up. Your last comment is fine. Now i get to know that the problem is with "this.id", here i am not getting checked elements id. Kindly help me. – Shibankar Aug 16 '13 at 10:33
  • 1
    Actually for creating jstree, my json object is looks like.... {"data":"ca_administration","id":0,"pId":-1,"path":"/ca_administration","children":[{"data":"trunk","id":3,"pid":0,"path":"ca_administration/trunk"},{"data":"branches","id":2,"pid":0,"path":"ca_administration/branches"},{"data":"tags","id":1,"pid":0,"path":"ca_administration/tags"}]} Now for each checked node, i need the "data" and "Path" value. Kindly help on how to do that. – Shibankar Aug 16 '13 at 11:01
7
$.each($("#jstree_demo_div").jstree("get_checked",true),function(){alert(this.id);});
wtf512
  • 4,487
  • 9
  • 33
  • 55
5
$('#dvTreeStructure').on('changed.jstree', function (e, data) {
                var i, j, r = [];
                for (i = 0, j = data.selected.length; i < j; i++) {
                    r.push(data.instance.get_node(data.selected[i]).text.trim());
                }
                alert('Selected: ' + r.join(', '));

            }
hrishikesh
  • 51
  • 1
  • 1
1

While using get_checked or get_selected pass the boolean as false to get the whole node where if you send as true, it will return only node Ids.

You have a look at https://www.jstree.com/api/#/?q=checkbox&f=get_checked([full])

You can also have a look at https://everyething.com/Example-of-jsTree-to-get-all-checked-nodes to get an idea of different kind of selected.

Asif Nowaj
  • 346
  • 2
  • 9