7

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?

blong
  • 2,815
  • 8
  • 44
  • 110
slmcmahon
  • 81
  • 1
  • 1
  • 5

7 Answers7

6

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/

George G
  • 7,443
  • 12
  • 45
  • 59
jDempster
  • 106
  • 1
  • 2
4

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.

Farish
  • 608
  • 7
  • 13
3

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();
Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
1

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

George G
  • 7,443
  • 12
  • 45
  • 59
zzg
  • 31
  • 2
1

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);
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
slmcmahon
  • 81
  • 1
  • 1
  • 5
0

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;
},
J Sidhu
  • 191
  • 2
-1
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.

eyelidlessness
  • 62,413
  • 11
  • 90
  • 94