2

Using jsTree (pre1.0_fix_1), I would like to obtain a list of id's for all the checked items (or better, a JSON object with id AND text of each checked item). I will then make an ajax call with this. Additionally, this should happen any time there is a state change of something getting checked or unchecked.

This is what I currently have:

$(function(){

  n = $('#colors').jstree({
    "plugins": ["themes", "html_data", "checkbox"]
  }).bind("change_state.jstree", function(e, data){
  console.log($('#colors').jstree('get_selected').attr('id'));
  });
});

This is just returning 'colors', from the container'sid: <div id="colors">. I fished around the data object, but didn't find it in there (perhaps I missed it?)

Raj
  • 1,479
  • 3
  • 15
  • 29

1 Answers1

3

In order to get the checked nodes' JSON you should be using get_checked and not get_selected. Try this:

    var checked = $("#colors").jstree("get_checked",null,true);
    var checked_json = [];
    $(checked).each(function (i,node) {
        var id = $(node).attr("id");
        var text = $(node).attr("text");
        var node_json = { 
            "id" : id, 
            "text" : text 
        };
        checked_json.push(node_json);
    });

After that checked_json will contain an array of of id+text objects which you could send to the server.

Amir
  • 1,882
  • 17
  • 22