如何在 ExtJs 中清除一棵树?

发布于 2024-07-06 17:02:14 字数 179 浏览 7 评论 0原文

我已经成功创建了一个 Ext.tree .TreePanel 动态加载子节点,但我很难清除树并加载新数据。 有人可以帮我编写代码来执行此操作吗?

I have managed to create an Ext.tree.TreePanel that loads child nodes dynamically, but I'm having a difficult time clearing the tree and loading it with new data. Can someone help me with the code to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烂柯人 2024-07-13 17:02:14

来自 ExtJS 大师 Saki 的精彩博客。

while (node.firstChild) {
    node.removeChild(node.firstChild);
}

http://blog .extjs.eu/know-how/how-to-remove-all-children-of-a-tree-node/

From the wonderful blog of Saki an ExtJS guru.

while (node.firstChild) {
    node.removeChild(node.firstChild);
}

http://blog.extjs.eu/know-how/how-to-remove-all-children-of-a-tree-node/

鸵鸟症 2024-07-13 17:02:14

在Ext JS 4中:

如果要重新加载树面板的数据,则需要重新加载树存储:

getCmp('treeId').getStore().load();

其中treeId是树的id。 如果您有商店 id,则可以直接对商店 id 使用 load() 。

删除所有子节点:

getCmp('treeId').getRootNode().removeAll();

但是,从存储中重新加载树节点时,不需要删除子节点。

In Ext JS 4:

if you want to reload the data of the tree panel, you need to reload the tree store:

getCmp('treeId').getStore().load();

where treeId is the id of the tree. If you have a store id, you may directly use load() on store id.

to remove all child nodes:

getCmp('treeId').getRootNode().removeAll();

However, removing child nodes is not necessary for reloading the tree nodes from its store.

蓝礼 2024-07-13 17:02:14

就我而言,我的 Ext 树有一个 AsyncTreeNode 类型的隐藏根节点。 如果我想清除树并从服务器重新填充,这非常简单:

tree.getRootNode().reload();

In my case, my Ext tree has a hidden root node of type AsyncTreeNode. If I want to clear the tree and repopulate from the server, it's pretty simple:

tree.getRootNode().reload();
小嗲 2024-07-13 17:02:14

我终于在他们的论坛中找到了答案。 对于任何感兴趣的人来说,它在这里:

if (tree)
{
    var delNode;
    while (delNode = tree.root.childNodes[0])
        tree.root.removeChild(delNode);
}

I finally found an answer in their forums. For anyone interested it is here:

if (tree)
{
    var delNode;
    while (delNode = tree.root.childNodes[0])
        tree.root.removeChild(delNode);
}
如梦 2024-07-13 17:02:14

您可以简单地使用node.removeAll()来删除该节点的所有子节点。

http://docs.sencha. com/extjs/4.2.1/#!/api/Ext.data.NodeInterface-method-removeAll

you can simply use node.removeAll() to remove all child nodes from this node.

http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.NodeInterface-method-removeAll

小红帽 2024-07-13 17:02:14

我遇到了类似的问题,我想出的解决方案是“标记”节点在折叠时尚未加载,从而在重新展开时强制重新加载。

listeners: {
   collapsenode: function(node){
   node.loaded = false;
},

I ran into a similar problem and the solution i came up with was to 'tag' the node has having not loaded when it was collapsed thus forcing a reload when it was re-expanded.

listeners: {
   collapsenode: function(node){
   node.loaded = false;
},
宛菡 2024-07-13 17:02:14
if (树) { var delNode;   while (delNode = tree.root.childNodes[0]) tree.root.removeChild(delNode);   } 
  

我不知道 Ext,但我猜他们有 DOM 抽象,这可能会让这件事变得更容易。 Prototype 中的等效项类似于:

tree.root.immediateDescendants().invoke('remove'); // or
tree.root.select('> *').invoke('remove');

除非 tree.root 引用集合对象而不是树的根 DOM 节点,但是否借用 DOM API 方法名称? 这似乎不太可能,特别是对于现代 JS 库而言。

if (tree) { var delNode; while (delNode = tree.root.childNodes[0]) tree.root.removeChild(delNode); }

I don't know Ext, but I'm guessing that they have DOM abstraction that might make that easier. An equivalent in Prototype would be something like:

tree.root.immediateDescendants().invoke('remove'); // or
tree.root.select('> *').invoke('remove');

Unless tree.root refers to a collection object rather than the tree's root DOM node, but is borrowing DOM API method names? That seems really unlikely, especially for a modern JS library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文