我可以将jstree复选框限制为仅限有孩子的父母吗?

use*_*898 6 javascript checkbox jstree

我想知道我是否只能在没有孩子的孩子的父母那里提供一个复选框.

像这样的东西.我可以选择一个孩子,或者选择所有孩子的孩子的直接父母.

parent01
    parent02
        parent03[]
            child01[]
            child02[]
            child03[]
                parent04[]
                    child04[]
        parent05[]
            child05[]
    parent06
        parent07
            parent08[]
                child06[]
                child07[]
Run Code Online (Sandbox Code Playgroud)

Ola*_*ant 5

这是一个工作示例,遵循我之前的答案的想法 - 设置隐藏有孩子的孩子的父母的可见性.

要防止通过单击节点检查隐藏的复选框,应将复选框插件属性whole_node和tie_selection设置为false.

以下代码正在读取树数据并检查每个树节点,如果父节点具有父节点 - 让我们将其称为祖父节点.如果是这样,祖父节点的复选框的css visibility属性设置为hidden.

var to_hide = []; // ids of nodes that won't have checkboxes

/* get the tree data and find the nodes that have 
   children with children
*/
function getGrandparents() {
  // tree data
  var item = $('#data').jstree(true).get_json('#', {flat:true});
  console.log(JSON.stringify(item, undefined, 2));
  var parent, grandparent;
  // var nodeIds = item.map(x => x.id);  // ids of nodes in object item
  var nodeIds = item.map(function(x){return x.id;});  // ids of nodes in object item
  for (var i = 0; i < item.length; i += 1) {
    if (has_parent(item[i])) {
      parent = item[nodeIds.indexOf(item[i].parent)];
      if (has_parent(parent)) {
        grandparent = parent.parent + '_anchor';  // node with id grandparent is parent of parent
                                                  // and therefore shouldn't have a checkbox
        if (to_hide.indexOf(grandparent) < 0) {
          to_hide[to_hide.length] = grandparent;  // load id into array of checkboxes that will 
                                                  // be hidden
        }
      }
    }
  }
  function has_parent(who) {
    return ((typeof who.parent !== 'undefined') && (who.parent !== '#'));
  }
}

function hideGrandparents() {
  var gpa;
  // set visibility of checkbox nodes in to_hide to hidden
  for (var n = 0; n < to_hide.length; n += 1) {
    gpa = document.getElementById(to_hide[n]);
    if (gpa != null ) {
      gpa.getElementsByClassName('jstree-checkbox')[0].style.visibility = 'hidden';
    }
  }
}
  
$('#data').jstree({
  'core' : {
    'data' : [
      { "text" : "Root node",
        "children" : [
	      { "text" : "Child node 1 1", 
            "children" : [
              { "text" : "Child node 1 1 1" },
              { "text" : "Child node 1 1 2" }
            ] 
          },
          { "text" : "Child node 1 2",
            "children" : [
              { "text" : "Child node 1 2 1",
                "children" : [
                  { "text" : "Child node 1 2 1 1" },
                  { "text" : "Child node 1 2 1 2" }
                ]                
              },
              { "text" : "Child node 1 2 2" }
            ]          
          }
        ]
      },
      { "text" : "Root node 2",
        "children" : [
          { "text" : "Child node 2 1" },
        ]
      }
    ]
  },
  'checkbox': {
    three_state: false,
    whole_node: false,    // should be set to false. otherwise checking the hidden checkbox
                          // could be possible by clicking the node
    tie_selection: false, // necessary for whole_node to work
    cascade: 'up down'
  },
  'plugins': ["checkbox"]
}).on("ready.jstree", function() {
  getGrandparents();
  hideGrandparents();  // hide checkboxes of root nodes that have children with children
}).on("open_node.jstree", hideGrandparents);
Run Code Online (Sandbox Code Playgroud)
<link href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.1.1/themes/default/style.min.css" rel="stylesheet"/>

<div id="data"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.7/jstree.min.js"></script>
Run Code Online (Sandbox Code Playgroud)