Javascript 敲除js,从树视图中选择的项目中查找所有父节点id
我们正在创建一个具有n(任意)个级别的树视图 我接受了,但现在我遇到了一个问题 我们希望从所选节点获取所有父节点Javascript 敲除js,从树视图中选择的项目中查找所有父节点id,javascript,html,knockout.js,Javascript,Html,Knockout.js,我们正在创建一个具有n(任意)个级别的树视图 我接受了,但现在我遇到了一个问题 我们希望从所选节点获取所有父节点id 有人能帮我们解决这个问题吗?据我所见,您可以获得当前选定节点的id,它已经是父id的串联 替换 <div data-bind="with: selected"> Selected Node: <span data-bind="text: name"></span> </div> 这(
id
有人能帮我们解决这个问题吗?据我所见,您可以获得当前选定节点的id,它已经是父id的串联 替换
<div data-bind="with: selected">
Selected Node: <span data-bind="text: name"></span>
</div>
这(3)条是:
函数setParents(rootNode){
if(ko.isObservable(rootNode.children)和&rootNode.children().length)
for(var i=0;i
您可以查看谢谢您的回复,我想要所有父节点id。在这种情况下,如果我单击id为“1.1.2”的节点,我想要一个类似于1>>1.1>>1.1.2的结果,是否有办法按相反顺序遍历并获取所选项目的所有父节点id?
<div data-bind="with: selected">
Selected Node: <span data-bind="text: name"></span>
Ids: <span data-bind="text: id"></span>
</div>
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();
})
}
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);