Search code examples
javascriptjstree

Get checked and undetermined nodes


I have been selected a node in jstree. I can get a json value of the selected nodes. But the problem is, I want the parent nodes of the child node , upto root


Solution

  • You can do this with the code as below. Check demo - Fiddle.

       function getParentNode(node) {
           return $('#tree').jstree().get_parent(node);
       }
    
       var nodes = $('#tree').jstree().get_selected('full'), parentNodes = [], parentTexts = [];
    
       nodes.forEach( function(node) {
          var parentNode = getParentNode(node);
          while (parentNode && parentNode !=='#') {
                if (parentNodes.indexOf(parentNode) === -1 ) { 
                  parentNodes.push( parentNode );
                  parentTexts.push( $('#tree').jstree().get_node(parentNode).text );
              }
              parentNode = getParentNode(parentNode);
          }
     })
    

    UPDATE

    The code you use could look like:

    var checked_ids = [], checked_ids1 = [];
    $("#temporary1").find(".jstree-undetermined").each(
        function(i, element) {
            var nodeId = $(element).closest('.jstree-node').attr("id");
            // alert( nodeId );
            checked_ids.push( nodeId );
            checked_ids1.push( $('#temporary1').jstree().get_node( nodeId ).text );
        }
    );