-
8 Nov 2012 1:04 AM #1
Answered: Problems expanding Tree nodes until certain level
Answered: Problems expanding Tree nodes until certain level
Hi there,
I have a tree with this (basic) Store configuration:
In my tree panel I have a toolbar with a button to expand the tree up to some level, for that I calculate in the server the nodes that have to be returned and I return them all at once in an object like this:Code:this.store = Ext.create('Ext.data.TreeStore', { proxy : { url: 'some/url', type : 'ajax' } });
I hope it is more or less clear... anyway, the point is that for loading this I just set a parameter in my request, catch it in the server and return the tree I want to see, then I want to load these nodes in the tree, as I already have them in my response, without going each time to the server to get them, what's the best approach for doing this?Code:[{ 'text' : 'Corporate', children : [{ text : 'SA 001', children : [{ text: 'P 001', children : [{ text : 'RSK 001' },{ text : 'RSK 002' }] }] }] }]
I started by:
This expands correctly the tree with only one request but then it tries to expand 'RSK 001' and 'RSK 002', so it throws two more requests and it gets the children of these nodes and load them (RSK 001 and RSK 002 are not leaf nodes).Code:treePanel.getRootNode().expand(true);
Then I tried another approach that was expanding not recursively but doing the recursion manually, this is my approach:
But I have a problem here and it is that when I append the child (at n.appendChild(c)Code:panel.getRootNode().firstChild.expand(false, recursive); var recursive = function(nodes) { Ext.Array.each(nodes, function(n) { var ch = n.childNodes; if (ch.length > 0) { if (!n.isExpanded()) { n.set('expanded', true); } Ext.Array.each(ch, function(c) { console.log('child : ' + c.get('text') + '\tappended to: ' + n.get('text')); n.appendChild(c); }); var str1 = ''; Ext.Array.each(ch, function(c) {str1+=c.get('text')+', ';}); console.log('recursive for ' + str1); recursive(ch); } }); }
, when I get more than one node c has always the same value, say I see in the console (and when I debug it) this:
child : RSK 001 appended to: P 001
child : RSK 001 appended to: P 001
which is extremely strange. Also only one node gets appended and it appends on top of the root.
node_incorrectly.png
This is the full console.log for this problem:
child : SA 001 appended to: Corporate
recursive for SA 001,
child : P 001 appended to: SA 001
recursive for P 001,
child : RSK 001 appended to: P 001
child : RSK 001 appended to: P 001
recursive for RSK 002, RSK 001,
And here there is an screenshot of what should happen:
correct.png
Does anyone have any idea why is this happening? I couldn't find a good reason for it...
Any help is highly appreciated, not only for solving the recursive function problem, but just to load the nodes all at once without trying to expand RSK 001 and RSK 002, other approaches are also welcomed, I just created the recursive function for this problem, so I can just remove it if it is not needeed.
Thanks!
-
Best Answer Posted by tvanzoelenThat I do not understand. I think you only can append non exting nodes.Yes, I append nodes to nodes that are appended, is not that the way to create the tree lazily?
Thats probably because you havent set the leaf property on the last nodes. It will try to load them if you expand these nodes.and extra requests are thrown.
But all nodes are there. You could first collect the nodes you want to expand. (The lower ones). For each node you want to expand you can call expandNode or expandPath on the tree.
-
8 Nov 2012 1:55 AM #2Ext JS Premium Member
- Join Date
- Apr 2008
- Location
- Groningen - Netherlands
- Posts
- 1,022
- Vote Rating
- 23
- Answers
- 75
First for the nodes that are leafs set
in your json return dataCode:leaf: true
-
8 Nov 2012 1:58 AM #3
Thanks for the reply.
Unfortunately I have no leaf nodes in this particular case. I mean, the RSK 001 and RSK 002 nodes are not leafs, they have children (that children are leafs), it is just they don't have to be expanded in this function. If I say that they are leafs probably cannot be expanded manually, right? But they should...
-
8 Nov 2012 2:03 AM #4
To clarify a bit, this is the full tree expanded, but I only want to expand until the RSK level and in one request:
-
8 Nov 2012 2:10 AM #5Ext JS Premium Member
- Join Date
- Apr 2008
- Location
- Groningen - Netherlands
- Posts
- 1,022
- Vote Rating
- 23
- Answers
- 75
But I do not quite understand , you append nodes to other nodes wich are already appended in the tree.
But first to tackle this problem and check if all is well properly loaded. Expoand the root and on callback call tree.expandAll(). Just to check if all is there.Code:Ext.Array.each(ch, function(c) { console.log('child : ' + c.get('text') + '\tappended to: ' + n.get('text')); n.appendChild(c); });
-
8 Nov 2012 2:27 AM #6
Yes, I append nodes to nodes that are appended, is not that the way to create the tree lazily?
If with the current code I expandAll in the callback:
I get my tree fully expanded (also leaf nodes like CNT 001, CNT 002, etc), as shown in the previous picture and extra requests are thrown.Code:panel.getRootNode().firstChild.expand(false, function(){panel.expandAll();});Last edited by Albareto; 8 Nov 2012 at 2:28 AM. Reason: add extra info
-
8 Nov 2012 2:38 AM #7Ext JS Premium Member
- Join Date
- Apr 2008
- Location
- Groningen - Netherlands
- Posts
- 1,022
- Vote Rating
- 23
- Answers
- 75
That I do not understand. I think you only can append non exting nodes.Yes, I append nodes to nodes that are appended, is not that the way to create the tree lazily?
Thats probably because you havent set the leaf property on the last nodes. It will try to load them if you expand these nodes.and extra requests are thrown.
But all nodes are there. You could first collect the nodes you want to expand. (The lower ones). For each node you want to expand you can call expandNode or expandPath on the tree.
-
8 Nov 2012 4:34 AM #8
Van haarte bedankt voor uw tijd en moeite.
Van haarte bedankt voor uw tijd en moeite.
You nailed it! I was looking in the incorrect place but after reading this sentence I started to look to the big picture and fixed the problem. As you said the nodes already existed and actually they were loaded, but not expanded! I just had to expand them manually. Here is my solution:
Thanks a lot man!Code:var openRecursive = function(node) { var ch = node.childNodes; if (ch.length > 0) { if (!node.isExpanded()) { node.expand(); } Ext.Array.each(ch, function(c) { openRecursive(c); }); } }; panel.store.load( { node : panel.getRootNode().firstChild, callback : function() { openRecursive(panel.getRootNode()); } });
-
8 Nov 2012 4:37 AM #9Ext JS Premium Member
- Join Date
- Apr 2008
- Location
- Groningen - Netherlands
- Posts
- 1,022
- Vote Rating
- 23
- Answers
- 75
well nice it is fixed!


Reply With Quote