I have a feature layer that I've constructed from a url, and have applied a feature template to it to display a popup when a feature is clicked. I also have a feature layer that I've constructed from a feature collection object (not a url) that I have applied a separate infotemplate to. For the latter, the infotemplate displays data in a popup window for, let's say, fields X, Y, and Z; this looks something like: featureLayer.setInfoTemplate(new InfoTemplate('$X','Name: $Y <br/>Type: $Z')); This displays a popup where the value for X is displayed as the title, and the contents are values for Y and Z. However, sometimes the value for Y is an empty string, and when this occurs, I'd like to not display field Y in my popup. How can I check for the value of Y when the feature is clicked so that I can dynamically alter the infotemplate to not show Y if it is an empty string?
Thanks,
Jason
Solved! Go to Solution.
You need to use a function for setting the content of your infoTemplate. If you search the forum for infotemplate.setcontent, I'm sure you'll find several examples. I end up using a function over half the time. Instead of using ${fieldName}, you'll use graphic.attributes.fieldName. The function can get quite long, depending on the number of attributes and how many you're checking for empty values.
var myInfoTemplate = new InfoTemplate("your Title");
myInfoTemplate.setContent(generateInfoContent);
function generateInfoContent(graphic) {
var formatString = "myFieldName:" + graphic.attributes.X;
var checkY = graphic.attributes.Y;
//only appends if there is a value for attribute 'Y'
if (checkY} {
formatString += checkY;
}
formatString += graphic.attributes.Z;
return formatString;
}
You need to use a function for setting the content of your infoTemplate. If you search the forum for infotemplate.setcontent, I'm sure you'll find several examples. I end up using a function over half the time. Instead of using ${fieldName}, you'll use graphic.attributes.fieldName. The function can get quite long, depending on the number of attributes and how many you're checking for empty values.
var myInfoTemplate = new InfoTemplate("your Title");
myInfoTemplate.setContent(generateInfoContent);
function generateInfoContent(graphic) {
var formatString = "myFieldName:" + graphic.attributes.X;
var checkY = graphic.attributes.Y;
//only appends if there is a value for attribute 'Y'
if (checkY} {
formatString += checkY;
}
formatString += graphic.attributes.Z;
return formatString;
}
Thanks Tracy, that's exactly what I needed! I didn't realize you could feed a function into setContent.
-Jason
I don't think it's well documented at all, for something so versatile. It makes me wonder what else we're overlooking!