PDA

View Full Version : Select node in treepanel when loaded



nicobarten
12 Nov 2009, 2:58 AM
Hi all.

I create a treepanel like this:



xtype: 'treepanel',
id: 'UrenTreePanel',
title: 'Week',
region: 'west',
collapsible: true,
animCollapse: true,
split: true,
border: true,
width: 200,
minSize: 100,
maxSize: 300,
loader: new Ext.tree.TreeLoader(),
root: new Ext.tree.AsyncTreeNode(
{
expanded: true,
children: function()
{
var myDate = new Date().add(Date.DAY, -10 * 7);
var nodes = new Array();
for(var i = 0; i < 10; i++)
{
myDate = myDate.add(Date.DAY, 7);
nodes[i] =
{
//text: 'Week ' + myDate.getWeekOfYear(),
text: myDate.getFullYear() + ' - ' + myDate.getWeekOfYear(),
leaf: true,
listeners:
{
click: function(node, e)
{
// get week of node
var array = node.text.split(' - ');
var year = array[0];
var week = array[1];

dsHoursTotals.proxy = new Ext.data.HttpProxy({url: './data/json.php?sort=hours&action=get-totals&year=' + year + '&week=' + week});
dsHoursTotals.reload();
}
}
};
}

return nodes;
}()
}),


When the treepanel is loaded and rendered, i want it to automatically select the latest childNode. So i used this code:



listeners:
{
render: function(self)
{
// selecteer de laatste week
self.root.lastChild.select();
}
}


However, when it executes the code in the render event, lastChild is still null, so the nodes aren't loaded yet.

So how can i select automatically the latest childNode in the treepanel after it has finished loading?

chrismarx
21 Apr 2010, 7:23 AM
any answer to this one?

CrazyEnigma
21 Apr 2010, 10:28 AM
Have you tried using "afterrender", and instead of using:
self.root, try self.getRootNode()

chrismarx
21 Apr 2010, 11:49 AM
I got this to work for a TreePanel:


listeners: {
"load":function(node){
var me = this;
node.eachChild(function(childNode){
me.removeListener("load",me.initialConfig.listeners.load);
setTimeout(function(){
childNode.expand();
},1000);
});
}
},

this runs at the correct time (once the child node has loaded), and then removes itself after the first time, since I only need to expand one level for my app.

However, for some reason, the "childNode.expand()" call won't work without a timeout. There's no error at execution time, but if you try to open and close nodes afterward, you get an odd DOM exception-