简体   繁体   English

你如何清除ExtJs中的树?

[英]How do you clear a tree in ExtJs?

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. 我设法创建了一个动态加载子节点的Ext.tree.TreePanel ,但是我很难清除树并用新数据加载它。 Can someone help me with the code to do this? 有人可以帮我解决这个问题吗?

From the wonderful blog of Saki an ExtJS guru. 来自Saki和ExtJS大师的精彩博客。

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

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

In Ext JS 4: 在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. 其中treeId是树的id。 If you have a store id, you may directly use load() on store id. 如果您有商店ID,则可以直接在商店ID上使用load()。

to remove all child nodes: 删除所有子节点:

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

However, removing child nodes is not necessary for reloading the tree nodes from its store. 但是,从存储中重新加载树节点不需要删除子节点。

In my case, my Ext tree has a hidden root node of type AsyncTreeNode. 在我的例子中,我的Ext树有一个隐藏的AsyncTreeNode类型的根节点。 If I want to clear the tree and repopulate from the server, it's pretty simple: 如果我想清除树并从服务器重新填充,那很简单:

tree.getRootNode().reload();

you can simply use node.removeAll() to remove all child nodes from this node. 您只需使用node.removeAll()从此节点中删除所有子节点。

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

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);
}

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;
},
 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. 我不知道Ext,但我猜他们有DOM抽象可能会让它更容易。 An equivalent in Prototype would be something like: Prototype中的等价物将类似于:

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? 除非tree.root引用一个集合对象而不是树的根DOM节点,否则是借用DOM API方法名称? That seems really unlikely, especially for a modern JS library. 这似乎不太可能,特别是对于现代JS库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM