0

I'm creating a custom pop-up where I send the attribute table value into a function to reformat it. It's called like so:

var herbicide = '${Herbicide1:herbName}'

It then enters the function like this (it has many more cases than what's shown):

herbName = function(value){
   herb = '';
   switch(value){
       case null:
           herb = 'None';
           break;
       case 1:
           herb = '2,4-D';
           break;
       ....
   };
   return herb;
};

The 'return herb' line should return the new value and I thought that the 'herbicide' variable would be set equal to whatever was returned, but that does not appear to be the case if I actually try to print it's value. When I print it:

console.log(herbicide);

It returns ${Herbicide1:herbName}

This is making it really hard to do conditional statements on the variable. For instance, if herbicide is set to 'None' then I don't want it to show up in the popup, but using this statement:

if (herbicide == 'None'){
    //do something
}

It will never evaluate true because unfortunately, no matter what value was returned from the function, the value being tested against is always the string '${Herbicide1:herbName}'

What's weird to me is that you can't see the value by printing it to the console, yet I know my functions are returning the correct values because it prints them correctly to a popup. I feel like I'm doing something stupid or backwards, anybody know what I need to do?

Using JS API 3.x

0

So it turns out using conditionals isn't possible when formatting popup content as per the documentation:

Placeholders (string substitution)

When graphics or feature layers have an info template, that info template is automatically used by the API to construct info window content when a feature is selected. Within an info template, placeholders denoted by ${} are used to specify attributes to display. At run-time, substitution occurs and the placeholder is replaced with the actual attribute value for the selected feature. In the code below, values inside ${} correspond to attribute field names. In the screen shot, values for the selected feature were substituted for the associated placeholders.

Since it doesn't actually convert the values until run time, that's why it always appears as the placeholder when you console.log() it and never appears to change despite the function returning an actual value. To work around this, it appears I have to write a custom function and set the InfoTemplate content to what gets returned by the function. This means you don't use attribute fields specifiers and instead work with a graphic to get the attributes.

https://developers.arcgis.com/javascript/3/jshelp/intro_formatinfowindow.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.