PDA

View Full Version : Help with replacing or reloading TreePanel with a new TreeNode



Planet_x
23 Nov 2010, 11:29 AM
I am desperately searching for the best way to do this.. I have tried many methods but they all seem to be giving me javascript errors.

I have a TreeNode declared for a root node, which is added to a TreePanel like so -



var layerRoot_Legend = new Ext.tree.TreeNode({
enableDD: true,
text: "All Legend Items",
loaded: true,
expanded: true
});

var layerTree_Legend = new Ext.tree.TreePanel({
title: '',
root: layerRoot_Legend,
enableDD: true,
applyLoader: true,
rootVisible: false
});


then I create layerlists using GeoExt.tree.LayerNode (ext.tree.treenode) like the following -



//Labels layerlist
layerList_Labels = new GeoExt.tree.LayerNode({
"layer": layer_Labels,
"isLeaf": true,
"checked": false,
"enableDD": true,
"cls": "rootnode",
"singleClickExpand": true,
"listeners":
{
'checkchange' : function(node, checked)
{
// If a parent node is unchecked, uncheck all the children
if (node.getUI().isChecked()) {
node.expand();
}
if (!node.getUI().isChecked())
{
node.collapse();
}
}
},
"loader": {
"param": "LAYERS"
}
});


I then append each layerlist onto my root node -


layerRoot_Legend.appendChild(layerList_Labels);


this all works fine. My tree is rendered correctly and I am able to turn on/off layers initially without any issues. My problem lies in either creating a brand new root and adding that as the new root of my treepanel, or removing all childnodes of my existing root and appending new ones. Every time I try this I just get javascript errors such as enableDD is null, this.map is null etc. I basically remove all map layers from my openlayers map, recreate them and add them and then refresh my map panel. However when I try the following code it doesn't work.. how can I either recreate the layer root with new layerlists that are going to actually work (when I switch from the other layerlist and if any node is expanded it fails) or can I just easily replace my existing tree with a new one? Very much appreciate any help, thanks.

Here is what fails -



//Declare a new layerlist

var layerList_Labels_Assessed = new GeoExt.tree.LayerNode({
"layer": layer_Labels,
"isLeaf": true,
"checked": false,
"enableDD": true,
"cls": "rootnode",
"singleClickExpand": true,
"listeners":
{
'checkchange' : function(node, checked)
{
// If a parent node is unchecked, uncheck all the children
if (node.getUI().isChecked()) {
node.expand();
}
if (!node.getUI().isChecked())
{
node.collapse();
}
}
},
"loader": {
"param": "LAYERS"
}
});

//try to add the new layerlist to the old one after removing all child nodes - it fails here


layerRoot_Legend.beginUpdate();
layerRoot_Legend.removeChild(layerList_Labels);
layerRoot_Legend.appendChild(layerList_Labels_Assessed);
layerRoot_Legend.endUpdate();

25 Nov 2010, 4:54 PM
What is GeoExt?