jsTree - 不要选择父节点的禁用子节点?

时间:2017-10-20 01:56:34

标签: jquery jstree

使用jsTree插件,开发人员认为在选择父节点时应该选择禁用的子节点 - 我不同意。为了解决这个问题,我想我可以通过编程方式取消选择已禁用的子节点。我已经这样做了以下内容:

  $("#selectionTree").on("select_node.jstree",
    function(evt, data) {
      var currentNode = data.node;
      var children = $("#selectionTree").jstree("get_children_dom", currentNode);
      for (var i = 0; i < children.length; i++) {
        var obj = $.parseJSON($(children[i]).attr('data-jstree'));
        if (obj['disabled'] == true) {
          $(children[i]).jstree("deselect_node", children[i]);
        }
      }
    }

我有两个问题。

  1. 我无法在小提琴中完成这项工作,但它适用于我的环境
  2. 如果父节点已折叠,则选择父节点时会选择已禁用的节点,即只有当用户可以看到已禁用的子节点时,我的代码才会起作用。
  3. 我认为解决方案是jQuery而不是jsTree,所以任何想法?

    编辑:我的代码示例现在适用于小提琴:https://jsfiddle.net/Lf55r7qt/3/

2 个答案:

答案 0 :(得分:0)

我的解决方案在1-deep树上进行了测试。我刚刚添加了这个功能:

//select parents children except disabled ones
//test on 1-deep
$("#selectionTree").on("select_node.jstree", function(evt, data) {
    var currentNode = data.node;

    //rember opened state for later
    var openedState = currentNode['state']['opened'];

    //need to open node for accruate selection       
    $('#selectionTree').jstree('open_node', currentNode);

        //get child nodes
    var children = $(this).jstree("get_children_dom", currentNode);

        //this allows selections parent nodes to deselect children (without it, there is no three-state)
    if ($("#" + currentNode['a_attr']['id'] + " > i").hasClass("jstree-undetermined")) {
      for (var i = 0; i < children.length; i++) {
        $(children[i]).jstree("deselect_node", children[i]);
      }
    }

        //loop through child nodes and select all except disabled nodes
    for (var i = 0; i < children.length; i++) {
      var obj = $.parseJSON($(children[i]).attr('data-jstree'));
      if (obj['disabled'] == true) {
        $(children[i]).jstree("deselect_node", children[i]);
      }
    }

        //return parent to closed state if was already
    if (openedState == false) {
      $(this).jstree('close_node', currentNode);
    }

});

工作小提琴:https://jsfiddle.net/Lf55r7qt/6/

答案 1 :(得分:-1)

您可以尝试一下(对我有用):

'checkbox': {
  'cascade_to_disabled': false,
}