21

I'm using the jsTree jQuery plugin with the checkbox theme. Does anyone know how to get the selected values with a form post?

Thank you!

dzm
  • 22,844
  • 47
  • 146
  • 226

11 Answers11

31

In the last version (3.0), the API was changed.

If you need just array of selected IDs (like in examples in this node), it is now very easy:

var selectedElmsIds = $('#tree').jstree("get_selected");

If you need to iterate over the selected elements, you just need to pass additional "true" parameter.

var selectedElmsIds = [];
var selectedElms = $('#tree').jstree("get_selected", true);
$.each(selectedElms, function() {
    selectedElmsIds.push(this.id);
});
Lukas Jelinek
  • 2,337
  • 1
  • 19
  • 12
  • 1
    For me, on 3.3.4 I needed to use get_checked, not get_selected. – chip Nov 20 '17 at 16:01
  • Using 3.3.8 version of jsTree, it is get_selected. For complete and working source code, you can have a look at https://everyething.com/Example-of-jsTree-to-get-all-checked-nodes – Asif Nowaj Nov 02 '19 at 18:06
18

Have you got your answer ? If not, here is one that appears in the jstree google groups

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

soumya
  • 278
  • 3
  • 8
15

Everyone, who worked with Jstree’s may face to this question: How to get the checked Ids of Jstree in form submit? here is the solution:

function submitMe() {
    var checked_ids = [];
    $('#your-tree-id').jstree("get_checked",null,true).each(function(){
        checked_ids.push(this.id);
    });
    //setting to hidden field
    document.getElementById('jsfields').value = checked_ids.join(",");
}

Now, we set it in a hidden field:

<input type="hidden" name="jsfields" id="jsfields" value="" />
scrowler
  • 24,273
  • 9
  • 60
  • 92
Jamshid Hashimi
  • 7,639
  • 2
  • 28
  • 27
3

The suggested solution from google groups however didn't work for partially checked nodes, in my case. I had to leave the get_checked out and do the following to get fully selected and partially selected nodes.


$(".sector-tree").find(".jstree-undetermined").each(function(i,element){            
    checked_ids.push($(element).attr("id"));

    if ($(this).find(".jstree-undetermined").length == 0) {  
        $(this).find(".jstree-checked").each(function(i, element){          
            checked_ids.push({$(element).attr("id"));
        });         
    }
});

// collect the rest of the checked nodes that exist under checked parents
$(".sector-tree").find(".jstree-checked").each(function(i, element){ //also includes the ones below 'undetermined' parent

    var selectedElement = $(element).attr("id");
    if ( hasItem(selectedElement, checked_ids ) < 0 ) {             
        checked_ids.push(selectedElement);
    }

}); 
soumya
  • 278
  • 3
  • 8
3

With jQuery you can simply do:

$('.jstree-checked,.jstree-undetermined').each(function(){
    var rawCheckedID = $(this).find('a').attr('id');
});

This will get the undetermined and checked at the same time. soumya's solution above may be more efficient.

Fostah
  • 2,947
  • 4
  • 56
  • 78
2

You can use this:

var result = $('#your_tree').jstree('get_selected');

https://stackoverflow.com/a/22499278/1883345

Community
  • 1
  • 1
Majid Basirati
  • 2,665
  • 3
  • 24
  • 46
1

i did this to get id,parentid and text of selected checkbox. hopefully it will help someone :)

function myFunction(elmnt,clr){
         elmnt.style.color =clr;
         var selectedElmsinfo = [];

         var selectedElms = $('#SimpleJSTree').jstree("get_selected", true);
            $.each(selectedElms, function() {
                selectedElmsinfo.push(this.id,this.text,this.parent);

            });
             alert(selectedElmsinfo);
        }
0
//click button show nodes checked
$(document).on('click','#showme',function() {
    var checked_ids = [];
    var checked_ids_meta = [];
    $('#demo_0').jstree("get_checked",null,true).each(function(i, e){
        checked_ids.push($(this).data("param")); // json metadata
        checked_ids_meta.push($(this).attr("href")); // json attr
    });     
    console.log(checked_ids)
    console.log(checked_ids_meta)       
}); 
Malcon
  • 71
  • 1
  • 1
  • 5
0

var selectedElmsIds = [];
$("#jstree2").find("li").each(function(i, element) { //also includes the ones below 'undetermined' parent
  if ($(this).attr("aria-selected") == "true") {
    selectedElmsIds.push($(this).attr('id'));
  }
});
console.log(selectedElmsIds);
Qiol Noon
  • 11
  • 1
0

This I did:

function getSelectedItems()
{
    var checked_ids = [];

    checkedNodes = $("#MyTree").jstree("get_checked", null, true); 

    for(var i = 0; i < checkedNodes.length; i++)
    {
        var id = $(checkedNodes[i].outerHTML)[0].id;

        checked_ids.push(id);
    }

     // Do whatever you want with the checked_ids 
}

This will give you an array of all selected node and their sub nodes and leafs; as well as single leafs selected under other nodes.

t_plusplus
  • 4,079
  • 5
  • 45
  • 60
0
$(document).ready(function(){
var jsfields = $('#myTree').jstree('get_selected');
$('.jsfields').val(JSON.stringify([jsfields]));
})

<input type="hidden" class="jsfields" value=""/>

Change the value of $('#myTree') to you respective tree, this is best working for me in ajax call. slight modification may be needed to populate input field of simple form.

Syed Waqas Bukhary
  • 5,130
  • 5
  • 47
  • 59