-
9 Jul 2008 10:55 AM #1
Stateful TreePanel
Stateful TreePanel
Hello all!
Since I couldn't find a way to make a TreePanel remember it's state (i.e. remember which nodes were expanded/collapsed), I figured I would post my solution here.
All I had to do was add the following to my TreePanel configuration:
Any comments/suggestions are more than welcome!Code:stateEvents : ['collapsenode', 'expandnode'], stateId : 'tree-panel-state-id', stateful : true, getState : function () { var nodes = []; this.getRootNode().eachChild(function (child) { //function to store state of tree recursively var storeTreeState = function (node, expandedNodes) { if(node.isExpanded() && node.childNodes.length > 0) { expandedNodes.push(node.getPath()); node.eachChild(function (child) { storeTreeState(child, expandedNodes); }); } }; storeTreeState(child, nodes); }); return { expandedNodes : nodes } }, applyState : function (state) { var that = this; this.getLoader().on('load', function () { var nodes = state.expandedNodes; for(var i = 0; i < nodes.length; i++) { if(typeof nodes[i] != 'undefined') { that.expandPath(nodes[i]); } } }); }
Cheers,
-Roger
-
10 Jul 2008 7:38 AM #2
To keep state while refreshing the tree, I added a refresh button to the tree panel tool bar:
However, I was having issues where if I collapsed a node, it would not remember that the node was collapsed when I refreshed the tree. If I reloaded the entire page, however, it would remember that the node was collapsed. With that in mind, I changed the applyState method above to the following:Code:id : 'refresh', qtip : 'Refresh', handler : function (event, toolEl, panel) { var thisTreePanel = panel.getComponent('tree-panel'); //get state from tree var state = thisTreePanel.getState(); panel.body.mask('Loading', 'x-mask-loading'); thisTreePanel.getLoader().load(thisTreePanel.getRootNode(), function () { panel.body.unmask(); thisTreePanel.applyState(state); }); }
Hope this helps someone. Please let me know what you think/anything I could do better/any bugs you find.Code:applyState : function (state) { var that = this; this.getLoader().on('load', function () { //read state in from cookie, not from what is passed in var cookie = Ext.state.Manager.get('content-manager-tree-panel'); var nodes = cookie.expandedNodes; for(var i = 0; i < nodes.length; i++) { if(typeof nodes[i] != 'undefined') { that.expandPath(nodes[i]); } } }); }
Thanks,
-Roger
-
26 Aug 2008 7:11 AM #3
-
26 Aug 2008 10:36 AM #4
-
4 Sep 2008 10:31 PM #5
This looks like something I need for my problem described here: http://www.extjs.com/forum/showthread.php?t=46252
Does this also work with getting & selecting the previous selected Node?I`m from Holland!
-
5 Sep 2008 5:48 AM #6
-
6 Sep 2008 2:30 PM #7
-
8 Oct 2008 9:53 AM #8
Greate code, thanks a lot!
One thing:
var cookie = Ext.state.Manager.get('content-manager-tree-panel');
'content-manager-tree-panel' -> Should be the stateId of the TreePanel. Otherwise it worked right out of the box.
-
9 Dec 2010 2:45 AM #9
Thanks a lot. That was missing.
One problem remained. Due to the event listener you made within the applyState method, the restore process is done everytime a node is opened or closed. Beside the fact, that this is not necessary and consumes client resources it restored previously collapsed node when using the singleExpand property set to true.
I took a while to me seeing the point. If you change the applyState methode as follows (just remove the listener), the restore process is done only once at load.
Code:applyState : function (state) { var nodes = state.expandedNodes; for(var i = 0; i < nodes.length; i++) { if(typeof nodes[i] != 'undefined') { this.expandPath(nodes[i]); } } }
-
26 Jan 2013 3:57 PM #10
ExtJS 4
ExtJS 4
Hi:
Any plan to update it make it work in V4? Lots of objects and methods are changed.


Reply With Quote

