![](/img/trans.png)
[英]How to mark parent tree node as selected if all the child nodes are selected
[英]knockout js, find all the parent nodes id from a item selected in tree view
从我所看到的,您可以获得当前所选节点的ID,它已经是父ID的串联。
更换
<div data-bind="with: selected">
Selected Node: <span data-bind="text: name"></span>
</div>
同
<div data-bind="with: selected">
Selected Node: <span data-bind="text: name"></span>
Ids: <span data-bind="text: id"></span>
</div>
如果要使用具有所有父代ID的数组,则可以执行以下操作:
parent
)添加一个parent属性,并将其填充到构造函数中 parentIds
) parentIds
数组的函数 检查以下代码(1.和2):
function TreeNode(values) {
var self = this;
ko.mapping.fromJS(values, { children: { create: createNode }}, this);
this.expanded = ko.observable(false);
for (var i = 0; i < this.children().length; i++)
this.children()[i].parent = this;
this.parentIds = [];
this.collapsed = ko.computed(function() {
return !self.expanded();
})
}
而这个(3.):
function setParents(rootNode) {
if (ko.isObservable(rootNode.children) && rootNode.children().length)
for (var i = 0; i < rootNode.children().length; i++) {
if (rootNode.children()[i].parent)
rootNode.children()[i].parentIds = rootNode.children()[i].parent.parentIds.slice(0);
rootNode.children()[i].parentIds.push(rootNode.children()[i].parent.id())
setParents(rootNode.children()[i]);
}
}
setParents(root);
你可以在这里查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.