[Users] Keeping Selected Layers When Replacing Child Nodes

Matt Priour mpriour at kestrelcomputer.com
Wed Oct 20 20:29:07 CEST 2010


You are using some very fragile methods there.
Rather than depending on lastChild & firstChild and the fact that they may or may not only contain leaf nodes, etc..., you should be using the standard ExtJS TreeNode walking functions.

node.bubble
http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreeNode#Ext.data.Node-bubble

node.cascade
http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreeNode#Ext.data.Node-cascade

node.eachChild
http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreeNode#Ext.data.Node-eachChild

Bubble walks up the tree toward the root node, starting with the node it is called on
Cascade walks down the tree (through each child branch as well) starting with the node it is called on
EachChild iterates over ONLY the child nodes of the node it is called on. It will NOT continue any further down the child branches and will not include the node it is called on

You should be able to use these functions to collect the information you need from your layer tree (node.getUI().isChecked(), etc..) and then reapply it to the tree formed with the different layer store.

If you want to start at a certain node other than the Root Node, then assign that node an id and find it in the tree panel using the TreePanel.getNodeById function
http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreePanel#Ext.data.Tree-getNodeById

Matt Priour
Kestrel Computer Consulting



From: IT Intern 
Sent: Wednesday, October 20, 2010 12:02 PM
To: users at geoext.org 
Subject: Re: [Users] Keeping Selected Layers When Replacing Child Nodes


Ok so far I have been able to find a way to access the properties of nodes...now I am struggling to find out if there is anyway to access the properties of the node's leafs?  If anyone know please help me, I think if I have that I will be able to carry out the desired task :)

Some code of my approach:

var i;
                            var j = 0;
                            var selectedLayers = new Array();
                            while(layerRoot.lastChild.hasChildNodes()){
                                alert(layerRoot.lastChild.firstChild.childNodes.length); //This returns 0, but the childNodes here are leafs :(
                                for(i = 0; i < layerRoot.lastChild.firstChild.childNodes.length; i++){
                                    if(layerRoot.lastChild.firstChild.childNodes[i].isSelected()){
                                        selectedLayers[j] = layerRoot.lastChild.firstChild.childNodes[i].attributes.text;
                                        alert(selectedLayers[j]);
                                        j++;
                                    } 
                                }
                                
                                layerRoot.lastChild.removeChild(layerRoot.lastChild.firstChild);
                            }
                            layerRoot.removeChild(layerRoot.lastChild);    
                        }

Thanks for taking the time to consider my questions,

elshae


On Tue, Oct 19, 2010 at 4:17 PM, IT Intern <itintern12 at gmail.com> wrote:

  Hello GeoExt Friends,

  So I have been using the GeoExt and Ext libraries to develop my map browser and now there's a feature I'd like to have, but don't know how to go about it :)

  So far everything is working.  What I have is a tree panel that when a menu is clicked a parent node with layer containers is added to the tree.  The layers in these containers are in different languages, so as the user toggles between the languages in the menu, it be nice if the layers in the previous language correspond to the language that is currently selected.  I hope you will understand what I am asking to do.  I guess a good starting point would be, is there a way to "get" which layers are selected in a layer container?  Thanks for your time, below is some relevant code:

  //So there are many layers, one each per language...

  var chn_townships = new OpenLayers.Layer.WMS(
                              "Townships (Chinese trans)", "http://localhost:8080/geoserver/wms", {layers: 'cite:chn_townships', transparent: true}, {isBaseLayer: false, displayInLayerSwitcher: true, visibility: false}
                         );
                      
              var tib_townships = new OpenLayers.Layer.WMS(
                              "Townships (Tibetan trans)", "http://localhost:8080/geoserver/wms", {layers: 'cite:tib_townships', transparent: true}, {isBaseLayer: false, displayInLayerSwitcher: true, visibility: true} 
                         );  

  //The layers are stored in layer stores...
  var tibLands = new GeoExt.data.LayerStore({
                  map: map,
                  initDir: 0,
                  layers: [tib_villages, tib_townships, tib_farms, tib_countys]
              });

              var chnLands = new GeoExt.data.LayerStore({
                  map: map,
                  initDir: 0,
                  layers: [chn_villages, chn_townships, chn_farms, chn_countys]
              });

  //Menu for users to select language
  var languageMenu = new Ext.menu.Menu({
                  id: 'mainMenu',
                  style: {
                      overflow: 'visible'     // For the Combo popup
                  },
                  items: [
                      // A Field in a Menu
                      {
                          text: 'Chinese',
                          checked: false,
                          group: 'theme',
                          icon: './gif/cn.gif',
                             handler: checkHandler
                      },{
                          text: 'English',
                          checked: true,
                          group: 'theme',
                          handler: checkHandler
                      },{
                          text: 'Tibetan',
                          checked: false,
                          group: 'theme',
                          handler: checkHandler
                          }
                  ]
              });

  //The handler function that listens to the users selection on the menu (This is where I want the magic to happen ;)
  function checkHandler(item, checked) {
                      var tibetContainer, chineseContainer;
                      
                      if(item.text == 'Tibetan'){
                          if(layerRoot.lastChild.id == 'chinese'){
                              layerRoot.removeChild(layerRoot.lastChild);    
                          }
                          
                          tibetContainer = new Ext.tree.TreeNode({
                              text: "Tibetan Labels",
                              expanded: true,
                              id: 'tibet'
                          });

                          tibetContainer.appendChild(new GeoExt.tree.OverlayLayerContainer({
                              text: "Lands (Tibetan trans)",
                              layerStore: tibLands,
                              expanded: false,
                              loader: {param: "LAYERS"}
                          }));

                          tibetContainer.appendChild(new GeoExt.tree.OverlayLayerContainer({
                              text: "Natural Selection (Tibetan trans)",
                              layerStore: tibNaturalLayers,
                              expanded: false,
                              loader: {param: "LAYERS"}
                          }));

                          tibetContainer.appendChild(new GeoExt.tree.OverlayLayerContainer({
                              text: "Points of Interest (Tibetan trans)",
                              layerStore: tibPointOfInterest,
                              expanded: false,
                              loader: {param: "LAYERS"}
                          }));

                          layerRoot.appendChild(tibetContainer);

                      }
                      else if(item.text == 'Chinese'){
                          
                          chineseContainer = new Ext.tree.TreeNode({
                              text: "Chinese Labels",
                              expanded: true,
                              id: 'chinese'
                          });

                          chineseContainer.appendChild(new GeoExt.tree.OverlayLayerContainer({
                              text: "Lands (Chinese trans)",
                              layerStore: chnLands,
                              expanded: false,
                              loader: {param: "LAYERS"}
                          }));

                          chineseContainer.appendChild(new GeoExt.tree.OverlayLayerContainer({
                              text: "Natural Selection (Chinese trans)",
                              layerStore: chnNaturalLayers,
                              expanded: false,
                              loader: {param: "LAYERS"}
                          }));

                          chineseContainer.appendChild(new GeoExt.tree.OverlayLayerContainer({
                              text: "Points of Interest (Chinese trans)",
                              layerStore: chnPointOfInterest,
                              expanded: false,
                              loader: {param: "LAYERS"}
                          }));

                          if(layerRoot.lastChild.id == 'tibet'){
                              //layerRoot.removeChild(layerRoot.lastChild);    
                              layerRoot.replaceChild(chineseContainer, layerRoot.lastChild)  //thought this might have done it, this just replaces the nodes but not the layers selected to the new ones :(
                          }else
                          {layerRoot.appendChild(chineseContainer);}

                      }





--------------------------------------------------------------------------------


_______________________________________________
Users mailing list
Users at geoext.org
http://www.geoext.org/cgi-bin/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.geoext.org/pipermail/users/attachments/20101020/7ecb5f7f/attachment-0001.htm 


More information about the Users mailing list