jsTree - 不要 select 禁用父节点的子节点?
jsTree - don't select disabled child nodes with parent?
使用 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]);
}
}
}
不过我有两个问题。
我无法在 fiddle 中使用它,但它在我的环境中可以使用
- 如果父节点折叠,则在选择父节点时选择禁用节点,即我的代码仅在禁用的子节点对用户可见时才有效。
我认为解决方案是 jQuery 而不是 jsTree,所以有什么想法吗?
编辑:我的代码示例现在可以在 fiddle 中运行:https://jsfiddle.net/Lf55r7qt/3/
我的解决方案在一棵深度为 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);
}
});
工作fiddle:https://jsfiddle.net/Lf55r7qt/6/
你可以试试这个(对我有用):
'checkbox': {
'cascade_to_disabled': false,
}
使用 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]);
}
}
}
不过我有两个问题。
我无法在 fiddle 中使用它,但它在我的环境中可以使用- 如果父节点折叠,则在选择父节点时选择禁用节点,即我的代码仅在禁用的子节点对用户可见时才有效。
我认为解决方案是 jQuery 而不是 jsTree,所以有什么想法吗?
编辑:我的代码示例现在可以在 fiddle 中运行:https://jsfiddle.net/Lf55r7qt/3/
我的解决方案在一棵深度为 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);
}
});
工作fiddle:https://jsfiddle.net/Lf55r7qt/6/
你可以试试这个(对我有用):
'checkbox': {
'cascade_to_disabled': false,
}