Update the layer of the feature layer according to the combobox value

417
3
11-21-2013 11:34 AM
ionarawilson1
Occasional Contributor III
I have two comboboxes. The first filters the contents on the second. I want a specific layer of the  feature layer to be activated depending on the selection of the second combobox. I created a global variable for the feature layer (stewardship), and also used that variable inside the change function of the combobox. I can see in the console that the layer id changes inside the function, but the layer in the map does not change, because it is still looking at the global variable value. Any ideas on what to do ? Thank you!!
   
 var stewardship = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/0" , {
          mode: FeatureLayer.MODE_SELECTION, 
         
          outFields: ['*']
});
  
 map.addLayers([stewardship]);

   dojo.connect(dijit.byId("Activity"), 'onChange', function(value){ 
             //alert('ok ' + event);
    activity = dijit.byId('Activity');
           var layerId = activity.item["layerId"]

            console.log(layerId)
   activityGroupValue = dijit.byId('ActivityGroup').get('value')
   
   if (activityGroupValue == " " && value in oc(forestrylist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Forestry")
    
   }
   
   else if (activityGroupValue == " " && value in oc(medialist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Media")
    
   }
   else if (activityGroupValue == " " && value in oc(conservationeducationlist))
   {
    
    dijit.byId('ActivityGroup').set('value', "Conservation Education")
    
   }
   
   else if  (activityGroupValue == " " && value in oc(firelist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Fire")
    
   }
   
   
   else if (activityGroupValue == " " && value in oc(urbanforestrylist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Urban Forestry")
    
   }


   else if  (activityGroupValue == " " && value in oc(professionaldevelopmentlist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Professional Development")
    
   }
   
   else if (activityGroupValue == " " && value in oc(oakwiltlist))
   {
   
    dijit.byId('ActivityGroup').set('value', "Oak Wilt")
    
   }
  
       
stewardship = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/" + layerId, {
          mode: FeatureLayer.MODE_SELECTION, 
          id: 'stewardship_' + layerId,
          outFields: ['*']
    
});
  
   

   map.addLayers([stewardship]);
   console.log(stewardship)

0 Kudos
3 Replies
StephenLead
Regular Contributor III
During your onChange event, you update the variable for the layer, but don't actually do anything with it. Within this function, you need to remove the existing layer from the map, recreate it, then re-add it:

            
var stewardship = new FeatureLayer("..../FeatureServer/0" , {...});
map.addLayers([stewardship]);

dojo.connect(dijit.byId("Activity"), 'onChange', function(value){
  //remove the existing stewardship layer
  map.removeLayer(stewardship);

  //re-create it using the new ID
  stewardship = new FeatureLayer("..../FeatureServer/" + layerId, {
          id: 'stewardship_' + layerId....
  });

  //add it to the map again
  map.addLayers([stewardship]);
});

0 Kudos
ionarawilson1
Occasional Contributor III
I tried that Stephen and it does not work, the feature layer created as a global variable does not get removed.
0 Kudos
JasonZou
Occasional Contributor III
ionara, I have replied to you in another post: http://forums.arcgis.com/threads/97254-Feature-Layer-layer-as-a-variable. Please remember that once a feature layer is added to the map, it will stay there until you specifically remove it or hide it. In the above reply, I haven't specified how you will remove or hide the loaded layers. That should be an easy task to accomplish though. Otherwise, let me know.
0 Kudos