Feature Layer layer as a variable

1373
15
Jump to solution
11-20-2013 12:27 PM
ionarawilson1
Occasional Contributor III
Depending on what is selected on a combobox, the layer of a feature layer should change. How can I use a variable to do that?
For example, that 0 as one layer in the feature layer would be replaced by a variable, but I am not sure how to do that! Thanks

var layernumber  = 0   var stewardship = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/Stewardship_SA3/FeatureServer/0",      {           mode: FeatureLayer.MODE_SELECTION, id: 'stewardship',           outFields: ['*']         }); 
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Occasional Contributor III
Here is the revision.

// assume cboLayer is a dijit/form/ComboBox type control var layerId = cboLayer.get("value");  // assume all the layer are coming from the same feature service. var featLayer = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/Stewardship_SA3/FeatureServer/" + layerId, {           mode: FeatureLayer.MODE_SELECTION,            id: 'stewardship_' + layerId,           outFields: ['*'] });

View solution in original post

0 Kudos
15 Replies
JasonZou
Occasional Contributor III
Here is the revision.

// assume cboLayer is a dijit/form/ComboBox type control var layerId = cboLayer.get("value");  // assume all the layer are coming from the same feature service. var featLayer = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/Stewardship_SA3/FeatureServer/" + layerId, {           mode: FeatureLayer.MODE_SELECTION,            id: 'stewardship_' + layerId,           outFields: ['*'] });
0 Kudos
ionarawilson1
Occasional Contributor III
Hi Jason,

I tried this but it is not working. What am I doing wrong? Thanks



FeatureServiceStewAAreaAPoint (FeatureServer)
View In:   ArcGIS.com Map

View Footprint In:   ArcGIS.com Map

Service Description:

Has Versioned Data: false

Has Versioned Data: false

MaxRecordCount: 1000

Supported Query Formats: JSON, AMF

Layers:

    Stewardship (0)
    ActivityArea (1)
    ActivityPoint (2)



var layerId = "Stewardship";

// assume all the layer are coming from the same feature service.
var featLayer = new FeatureLayer("http://tfsgis-iisd01:6080/arcgis/rest/services/FeatureServiceStewAAreaAPoint/FeatureServer/" + layerId, {
          mode: FeatureLayer.MODE_SELECTION, 
          id: 'stewardship_' + layerId,
          outFields: ['*']
});
  
   

   map.addLayers([stewardship_Stewardship]);
0 Kudos
JasonZou
Occasional Contributor III
var layerId = "Stewardship";

layerId must be a number, not a text string. Change it to
 var layerId = "0";
and try again.
0 Kudos
ionarawilson1
Occasional Contributor III
It worked! Thank you so much. The only issue now is to get the value from the combobox to pass that value to the layerID variable. My values are strings. Is there a way to create another property in the combobox and get that value, that would be a number, to use as the layerID value? Here is the code snippet of the functionality of my comboboxes. Thanks!!!

dataArray = [
            {name:"Forest Stewardship Plan", id:"0"},
   {name:"Non Forest Stewardship Plan", id:"1"},
            {name:"Reforestation Acres - TFS", id:"F-R-TFS"},
            {name:"Reforestation Acres - Non TFS", id:"F-R-O"},
            {name:"Reforestation Assist", id:"F-R-A"},
            {name:"Tree Farm Program Inspection - Inspection", id:"F-TFP-I"},
            {name:"Tree Farm Program Inspection - Reinspection", id:"F-TFP-R"},
            {name:"Certified Forest Steward Assist", id:"F-CFS-A"},
            {name:"Timber Theft Damage Appraisal", id:"F-TTDA"},
            {name:"Referral - Consulting Forester", id:"F-R-CF"},
            {name:"Referral - Vendor", id:"F-R-V"},
            {name:"Rural Forestry Incidental Assist", id:"F-IA"},
   {name:"Prevention and Reduction of Pest Losses", id:"F-PRPL"},
   {name:"Forest Health Monitoring", id:"F-FHM"},
   {name:"FIA Assist", id:"F-IA-A"},
   {name:"BMP Assist", id:"F-BMP-A"},
   {name:"OW On-Site Assist", id:"OW-OSA"},
   {name:"OW Not On Site Assist", id:"OW-NOSA"},
   {name:"OW Incidental Assist", id:"OW-IA"},
   {name:"OW Presentation", id:"OW-P"},
   {name:"OW Training Given", id:"OW-TG"},
   {name:"OW Training Received", id:"OW-TR"},
   {name:"UF Individual Assist", id:"UF-A"},
   {name:"UF Governmental Organization Assist", id:"UF-GO-A"},
   {name:"NGO Assist", id:"UF-NGO-A"},
   {name:"Tree City USA Assist", id:"UF-TC"},
   {name:"Arbor Day Program", id:"UF-ADP"},
   {name:"UF Training Given", id:"UF-TG"},
   {name:"UF Training Received", id:"UF-TR"},
   {name:"UF Incidental Assist", id:"UF-IA"},
   {name:"UF Presentation", id:"UF-P"},
   {name:"Referral - Arborist", id:"UF-R-A"},
   {name:"VFD and/or ESD Assist", id:"Fi-VFD-A"},
   {name:"Community Wildfire Protection Plan", id:"Fi-CWPP"},
   {name:"Firewise Communities USA", id:"Fi-FC"},
   {name:"FEPP Support and Assistance", id:"Fi-FEPP"},
   {name:"Fire Prevention", id:"Fi-Pr"},
   {name:"District Fire Work Plans and County Profiles", id:"Fi-Pl"},
   {name:"Fire and Emergency Training Given", id:"Fi-TG"},
   {name:"Resource Protection Technical Development", id:"Fi-TR"},
   {name:"Landowner Workshops", id:"CE-LW"},
   {name:"Emerging Communities", id:"CE-EC"},
   {name:"Landowner Association Assist", id:"CE-LA"},
   {name:"Forest Awareness, Walk in the Forest, PLT Programs", id:"CE-PLT"},
   {name:"Forest Awareness, Walk in the Forest, PLT Programs - Assist", id:"CE-PLT-A"},
   {name:"ConEd Program, Woodland Clinic, Environthon, etc.", id:"CE-ConEd"},
   {name:"ConEd Presentation - Civic Organization", id:"CE-PCO"},
   {name:"Professional Development - Training Received", id:"PD-TR"},
   {name:"Professional Development - Internal Training Given", id:"PD-TG"},
   {name:"Meeting Attended", id:"PD-MAT"},
   {name:"Professional Networking", id:"PD-PDN"},
   {name:"Media Contact - Rural Forestry", id:"M-MC-R"},
   {name:"Media Contact - Urban Forestry", id:"M-MC-U"},
   {name:"Media Contact - Oak Wilt", id:"M-MC-OW"},
        ]
  
  dataArrayForestry = [
            {name:"Forest Stewardship Plan", id:"F-FSP"},
   {name:"Non Forest Stewardship Plan", id:"F-NFSP"},
            {name:"Reforestation Acres - TFS", id:"F-R-TFS"},
            {name:"Reforestation Acres - Non TFS", id:"F-R-O"},
            {name:"Reforestation Assist", id:"F-R-A"},
            {name:"Tree Farm Program Inspection - Inspection", id:"F-TFP-I"},
            {name:"Tree Farm Program Inspection - Reinspection", id:"F-TFP-R"},
            {name:"Certified Forest Steward Assist", id:"F-CFS-A"},
            {name:"Timber Theft Damage Appraisal", id:"F-TTDA"},
            {name:"Referral - Consulting Forester", id:"F-R-CF"},
            {name:"Referral - Vendor", id:"F-R-V"},
            {name:"Rural Forestry Incidental Assist", id:"F-IA"},
   {name:"Prevention and Reduction of Pest Losses", id:"F-PRPL"},
   {name:"Forest Health Monitoring", id:"F-FHM"},
   {name:"FIA Assist", id:"F-IA-A"},
   {name:"BMP Assist", id:"F-BMP-A"},
   
        ]
  
  dataArrayOakWilt = [
            {name:"OW On-Site Assist", id:"OW-OSA"},
   {name:"OW Not On Site Assist", id:"OW-NOSA"},
   {name:"OW Incidental Assist", id:"OW-IA"},
   {name:"OW Presentation", id:"OW-P"},
   {name:"OW Training Given", id:"OW-TG"},
   {name:"OW Training Received", id:"OW-TR"},
   {name:"Alpine", id:"AL"},
        ]
  
  dataArrayUrbanForestry = [
            {name:"UF Individual Assist", id:"UF-A"},
   {name:"UF Governmental Organization Assist", id:"UF-GO-A"},
   {name:"NGO Assist", id:"UF-NGO-A"},
   {name:"Tree City USA Assist", id:"UF-TC"},
   {name:"Arbor Day Program", id:"UF-ADP"},
   {name:"UF Training Given", id:"UF-TG"},
   {name:"UF Training Received", id:"UF-TR"},
   {name:"UF Incidental Assist", id:"UF-IA"},
   {name:"UF Presentation", id:"UF-P"},
   {name:"Referral - Arborist", id:"UF-R-A"},
  
        ]
  
  dataArrayConservationEducation = [
            {name:"Landowner Workshops", id:"CE-LW"},
   {name:"Emerging Communities", id:"CE-EC"},
   {name:"Landowner Association Assist", id:"CE-LA"},
   {name:"Forest Awareness, Walk in the Forest, PLT Programs", id:"CE-PLT"},
   {name:"Forest Awareness, Walk in the Forest, PLT Programs - Assist", id:"CE-PLT-A"},
   {name:"ConEd Program, Woodland Clinic, Environthon, etc.", id:"CE-ConEd"},
   {name:"ConEd Presentation - Civic Organization", id:"CE-PCO"},
   
  
        ]
  
  
  dataArrayFire = [
            {name:"VFD and/or ESD Assist", id:"Fi-VFD-A"},
   {name:"Community Wildfire Protection Plan", id:"Fi-CWPP"},
   {name:"Firewise Communities USA", id:"Fi-FC"},
   {name:"FEPP Support and Assistance", id:"Fi-FEPP"},
   {name:"Fire Prevention", id:"Fi-Pr"},
   {name:"District Fire Work Plans and County Profiles", id:"Fi-Pl"},
   {name:"Fire and Emergency Training Given", id:"Fi-TG"},
   {name:"Resource Protection Technical Development", id:"Fi-TR"},
   
  
        ]
  
  
  dataArrayProfessionalDevelopment = [
            {name:"Professional Development - Training Received", id:"PD-TR"},
   {name:"Professional Development - Internal Training Given", id:"PD-TG"},
   {name:"Meeting Attended", id:"PD-MAT"},
   {name:"Professional Networking", id:"PD-PDN"},
     
  
        ]
  
  
  dataArrayMedia = [
            {name:"Media Contact - Rural Forestry", id:"M-MC-R"},
   {name:"Media Contact - Urban Forestry", id:"M-MC-U"},
   {name:"Media Contact - Oak Wilt", id:"M-MC-OW"},
    
        ]
      var stateStore = new Memory({
    
        data: [
            {name:"Forestry", id:"FO"},
   {name:"Oak Wilt", id:"OW"},
            {name:"Urban Forestry", id:"UF"},
            {name:"Fire", id:"Fi"},
            {name:"Conservation Education", id:"CE"},
            {name:"Professional Development", id:"PD"},
            {name:"Media", id:"ME"},
          
        ]
    });

    var comboBox = new ComboBox({
        id: "ActivityGroup",
        name: "ActivityGroup",
        value: " ",
        store: stateStore,
  intermediateChanges: true,
        searchAttr: "name"
  
    }, "GroupActivity");

        
   var stateStore2 = new Memory({
    
        data: dataArray
    });

    var comboBox = new ComboBox({
        id: "Activity",
        name: "activity",
        value: " ",
  intermediateChanges: true,
        store: stateStore2,
        searchAttr: "name"
    }, "Activity");

    dojo.connect(dijit.byId("ActivityGroup"), 'onChange', function(value){ 
            // alert('ok ' + event);
    if (value == "Forestry") {
    
    stateStore2.data = dataArrayForestry
    activity = dijit.byId('Activity')
    activity.set("value", "Forest Stewardship Plan") 
   }
   
   else if (value == "Oak Wilt") {
   
     stateStore2.data = dataArrayOakWilt
    activity = dijit.byId('Activity')
    activity.set("value", "OW On-Site Assist") 
   }
   
   else if (value == "Urban Forestry") {
   
     stateStore2.data = dataArrayUrbanForestry
    activity = dijit.byId('Activity')
    activity.set("value", "UF Individual Assist") 
   }
   
   else if (value == "Conservation Education") {
   
     stateStore2.data = dataArrayConservationEducation
     activity = dijit.byId('Activity')
        activity.set("value", "Landowner Workshops") 
   }
   else if (value == "Fire") {
   
     stateStore2.data = dataArrayFire
     activity = dijit.byId('Activity')
        activity.set("value", "VFD and/or ESD Assist") 
   }
0 Kudos
JasonZou
Occasional Contributor III
Add one property to each data array, like
dataArray = [
        {name:"Forest Stewardship Plan", id:"0", layerId: 0},
 {name:"Non Forest Stewardship Plan", id:"1", layerId: 1},
        ...
];


Then assume cboLayers is the combobox that built with a store using dataArray.
var layerId = store.getValue(cboLayers.item, "layerId");
0 Kudos
ionarawilson1
Occasional Contributor III
I am getting close. What would be the cboLayers? In my case I am not sure . I tried this, but iI get an error, saying "store is not defined"

var layerId =  store.getValue(ActivityGroup.item, "layerID")
0 Kudos
JasonZou
Occasional Contributor III
The code snippet in my previous post just gives some idea how to get the layer id from the combobox. Ok, in your case, I assume all dataArrayXXXX are the data hosting the layer info. If that is the case, after adding layerId property to each dataArrayXXXX array, then

activity = dijit.byId('Activity');
var layerId = activity.store.getValue(activity.item, "layerId");
0 Kudos
ionarawilson1
Occasional Contributor III
That should work but I get an error. Do  you have any idea why?

activity.store.getValue is not a function
0 Kudos
JasonZou
Occasional Contributor III
My fault. activity.item is the current selected item that should have everything you need. Try
var layerId = activity.item["layerId"];
0 Kudos