Search code examples
jstree

Show only checked nodes and parents of it in jstree


I have to show only selected nodes and parents, and hide rest nodes.

There is no get_unchecked in current documentation. My json format is huge so taking it in string and formatting and reloading tree will be inefficient.

Is there a way to get all unchecked nodes and hide it? if there is i can check all parent node of current checked node and then just hide all unchecked nodes, but i cannot find any method to retrive all unchecked nodes.


Solution

  • Work around for the question i made.

    $('#jstree_demo_div').on('loaded.jstree', function(e, data) {
      console.log("loaded");
      checked_ids = $('#jstree_demo_div').jstree('get_selected', true);
      $("#jstree_demo_div").jstree('select_all');
      $.each(checked_ids, function(index, value) {
        $("#jstree_demo_div").jstree('deselect_node', value);
        uiParentsShow(value);
      });
      checked_ids = $('#jstree_demo_div').jstree('get_selected', true);
      $.each(checked_ids, function(index, value) {
        $("#jstree_demo_div").jstree('hide_node', value);
    
      });
    });
    
    function uiParentsShow(node) {
      try {
        var parent = $("#jstree_demo_div").jstree('get_parent', node);
        $("#jstree_demo_div").jstree('deselect_node', parent);
        if (parent != '#') {
          uiParentsShow1(parent);
        }
      } catch (err) {
        console.log('Error in uiGetParents' + err);
      }
    }
    

    Steps:

    • Step 1: Selecting All the elements
    • Step 2: Deselecting the checked node and the parent of checked node
    • Step 3: Hide Selected Nodes