can't get list of checked items

1,474 views
Skip to first unread message

Angus Beare

unread,
Oct 15, 2014, 8:14:26 PM10/15/14
to jst...@googlegroups.com
Hi

I've been struggling all night and can't seem to get a list of checked checkbox items back from jstree.
The tree works fine and loads the data from my web service and the selected items from the data are selected but when I come to save the checked ID's into the DB I am finding that only the child ID's are getting returned. My data looks something like this:

{"id":"2","parent":"#","text":"Trade Policy Group","state":{"opened":false,"selected":true},"li_attr":{"ID":"2"}},{"id":"3","parent":"#","text":"Environmental Policy Group","state":{"opened":false,"selected":false},"li_attr":{"ID":"3"}},{"id":"4","parent":"#","text":"Digital Economy Policy Group","state":{"opened":false,"selected":false},"li_attr":{"ID":"4"}},

And my js code:

 $( document ).ready(function () {
          $('#jstree').on("changed.jstree", function (e, data) {
              $('[name$="CategoryListHidden"]').val(data.selected);
              console.log(data.selected);
          });
     
        
          
      		var cl = '[[CategoryList]]';
        	if(cl==''){cl=0}; // if no categories then use a zero
		var url = 'http://de_webservices.mybox.me/api/JsonCategories?NewsItemIds=' + cl + '&formMode=EDIT';
          $('#jstree').jstree({
              "checkbox": {
                  "keep_selected_style": false,
                   "visible" : true,
                   "three_state": true,
                   "whole_node" : true,
              },
              "plugins": ["checkbox"],
              'core' : {
                   'dataType': "jsonp",
                   'data': {
                   'url': url,
                   'data': function(node) {
                          return { 'id': node.id };
                      }
                  }
              }
          });
        
      
        $("#getChecked").click(function () {
        $("#jstree").jstree("get_checked", null, true).each(function (i, e) {
        console.log($(this).attr("ID"))
       });
    });
       
      });

I am using the latest jstree code. I am wondering if it's to do with the li_attr or something. Can anyone spot anything obvious?

thanks
Gus

Ivan Bozhanov

unread,
Oct 16, 2014, 1:32:39 AM10/16/14
to jst...@googlegroups.com
What are you trying to achieve with this line:
jstree("get_checked", null, true)

get_checked (which in your case is the same as get_selected, so I recommend you use get_selected) accepts only one parameter - whether to return a list of IDs or a list of full nodes.

Keep in mind undetermined nodes will not be returned, as they are not selected, only some of their children are.

Could you please clarify what the problem is? Here is a fiddle - if all children are selected, get_selected will return all 3 IDs (along with the root node).
http://jsfiddle.net/DGAF4/126/

Maybe you are looking for get_top_selected? Anyway - please clarify what the problem is.

Oh, and dataType has no effect where you have put it - it needs to be inside core.data

Best regards,
Ivan

Angus Beare

unread,
Oct 16, 2014, 3:44:10 AM10/16/14
to jst...@googlegroups.com
Hi Ivan

thanks for responding so quickly.  thankfully I solved the issue with this single line:

"three_state": false,

After that I can select multiple check boxes but I have to select them individually and when you do that this bit works as expected:

 $('#jstree').on("changed.jstree", function (e, data) {
              $('[name$="CategoryListHidden"]').val(data.selected);
              console.log(data.selected);
          });

What I found really confusing is that when "three_state" is set to true even though checkboxes appear to be selected (checked) they are not returned in "data.selected".
 The #getchecked bit was an attempt to have button that when clicked got the checked values.

I found the docs limited and confusing in this area and I was led in all sorts of directions with lots of old code I was looking at on the net.

Gus

Angus Beare

unread,
Oct 16, 2014, 3:54:25 AM10/16/14
to jst...@googlegroups.com
BTW, that fiddle was a great help thanks. 
In future I will try and create fiddles first so I can demonstrate more clearly what i want to achieve.
But it's not so easy when you are using data from a web service.

Gus

On Thursday, October 16, 2014 6:32:39 AM UTC+1, Ivan Bozhanov wrote:
Reply all
Reply to author
Forward
0 new messages