2017-10-20 113 views
0

使用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. 残疾人节点只能当禁用子节点对用户可见。

我在想,解决方案是jQuery而不是jsTree所以有什么想法?

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

回答

0

我的解决方案在1 - 深树的作品进行测试。我只是说这个功能:

//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/

相关问题