gpt4 book ai didi

tree - Meteor:如何从集合创建响应式(Reactive)树结构

转载 作者:行者123 更新时间:2023-12-02 10:15:18 24 4
gpt4 key购买 nike

我使用的是最新的meteor版本,这是本地部署。我有一个包含树结构的集合(文件夹),其中子节点将父节点 id 作为属性。我想在 UI 树小部件中显示树。我已经研究了递归模板主题,但是,我很难显示子节点。这里是相关的模板和代码。

<template name="sideTreeTemplate">
<div id="tree" style="height: 200px">
<h2 class="panel">My Data</h2>
<ul id="treeData" style="display: none;">
{{#each treeItems }}
{{> treeNodeTemplate}}
{{/each }}
</ul>
</div>
</template>


<template name="treeNodeTemplate" >
<li id="{{id}}" title="{{name}}" class="{{type}}">
{{name}}
{{#if hasChildren}}
<ul>
{{#each children}}
{{> treeNodeTemplate}}
{{/each}}
</ul>
{{/if}}
</li>
</template>

client.js 代码:

Template.sideTreeTemplate.treeItems = function() {

var items = Folders.find({"parent" : null});
console.log("treeItems length=" + items.count());
items.forEach(function(item){
item.newAtt = "Item";
getChildren(item);
});
return items;

};


var getChildren = function(parent) {
console.log("sidetree.getChildren called");
var items = Folders.find({"parent" : parent._id});
if (items.count() > 0) {
parent.hasChildren = true;
parent.children = items;
console.log(
"children count for folder " + parent.name +
"=" + items.count() + ",
hasChildren=" + parent.hasChildren
);
items.forEach(function(item) {
getChildren(item);
});
}
};

树的顶层显示良好,并且是 react 性的,但没有显示任何子节点,即使为具有子节点的节点调用了 getChildren 函数也是如此。我怀疑服务器同步实际上删除了每个节点动态添加的属性(即 hasChildrenchildren)。在这种情况下,我怎样才能使 react 树发挥作用?或者我的实现可能还有其他问题?

感谢您的帮助。

最佳答案

简单的方法是不将子对象添加为父对象的属性。相反,使用助手:

Template.treeNodeTemplate.hasChildren = function() {
return Folders.find({parent: this._id}).count() > 0;
};

Template.treeNodeTemplate.children = function() {
return Folders.find({parent: this._id});
};

关于tree - Meteor:如何从集合创建响应式(Reactive)树结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18223118/

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