gpt4 book ai didi

javascript - 当子节点未全部选中时取消选中父节点

转载 作者:行者123 更新时间:2023-11-30 08:58:10 25 4
gpt4 key购买 nike

我是新手 extJs 用户。我使用了带复选框的树面板,我想在子节点未全部选中时取消选中父节点。下面是我的代码,希望你们能帮助我,我不知道该怎么做。

我的树面板结构是这样的:

  • 父节点1
    • sub-parentNode1.1
      • child1.1
      • child1.2
      • child1.3
    • subparentNode1.2
      • child2.1
      • child2.2
  • 父节点2
    • subparentNode2.1
      • child2.1.1


var treeCheck = new Ext.tree.TreePanel({
//some code here
});

//event here

treeCheck.on('checkchange', function(node, checked) {
if(node.hasChildNodes()==true) {

node.eachChild(function(n) {
n.getUI().toggleCheck(checked);
});

} else {

if(!checked) {
var _parentNode = node.parentNode;
//i dont know what to do here...
//specifically, i want to uncheck the parent node and subparent node
//when the children/child node is unchecked
}
}
});

最佳答案

遇到了同样的问题。通过添加此事件处理程序修复它:

treePanel.on('checkchange', function(node, isChecked) {

// Propagate change downwards (for all children of current node).
var setChildrenCheckedStatus = function (current) {
if (current.parentNode) {
var parent = current.parentNode;
current.set('checked', parent.get('checked'));
}

if (current.hasChildNodes()) {
current.eachChild(arguments.callee);
}
};
if (node.hasChildNodes()) {
node.eachChild(setChildrenCheckedStatus);
}

// Propagate change upwards (if all siblings are the same, update parent).
var updateParentCheckedStatus = function (current) {
if (current.parentNode) {
var parent = current.parentNode;

var checkedCount = 0;
parent.eachChild(function(n) {
checkedCount += (n.get('checked') ? 1 : 0);
});

// Children have same value if all of them are checked or none is checked.
var sameValue = (checkedCount == parent.childNodes.length) || (checkedCount == 0);

if (sameValue) {
var checkedValue = (checkedCount == parent.childNodes.length);
parent.set('checked', checkedValue);
} else {
// Not all of the children are checked, so uncheck the parent.
parent.set('checked', false);
}

updateParentCheckedStatus(parent);
}
}
updateParentCheckedStatus(node);
});

以递归方式向下(检查节点的所有子节点)和向上(如果未选中节点则取消选中父节点)双向工作。

关于javascript - 当子节点未全部选中时取消选中父节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11624894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com