使用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]);
}
}
}
我有两个问题。
我认为解决方案是jQuery而不是jsTree,所以任何想法?
编辑:我的代码示例现在适用于小提琴:https://jsfiddle.net/Lf55r7qt/3/
答案 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);
}
});
答案 1 :(得分:-1)
您可以尝试一下(对我有用):
'checkbox': {
'cascade_to_disabled': false,
}