Select to view content in your preferred language

infoTemplate if field is Null display "n/a"

941
2
Jump to solution
06-11-2014 06:35 AM
AndrewL
Frequent Contributor
Hi I have the following code that is very similar to the JS sample https://developers.arcgis.com/javascript/jssamples/find_popup.html

Sometimes the CITY_NAME field will be null. When that occurs I would like it to say "n/a" rather than "Null." It shows up as "Null" capitalized.

 if (layerName === 'County_City_Py') {                   var CityCountyTemplate = new InfoTemplate("",                     "City: ${CITY_NAME} <br/> County: ${COUNTY} <br/> State: ${STATE_NAME}");        CityCountyTemplate.setTitle("Search Result")                   feature.setInfoTemplate(CityCountyTemplate);  }


I have tried a combination of if/then statements including:

if (${CITY_NAME} != null) {
   Unexpected token { error

and
if ("${CITY_NAME"} != null) {
  No error but doesn't work.

Thank you!
0 Kudos
1 Solution

Accepted Solutions
AndrewL
Frequent Contributor
The InfoTemplate does give you the ability to execute a function that returns the template. You can view the documentation here, but using your code, you could try this:

if (layerName === 'County_City_Py') {     var CityCountyTemplate = new InfoTemplate(function (graphic) {         var attr = graphic.attributes;         var template = "City: ";         template += (attr.CITY_NAME === null) ? 'n/a' : attr.CITY_NAME;         template += '<br />County: ' + attr.COUNTY;         template += '<br />State: ' + attr.STATE_NAME;         return template;     });     CityCountyTemplate.setTitle("Search Result")     feature.setInfoTemplate(CityCountyTemplate); }


Thanks for your response. I checked out that link and tweaked your code. This is working now. I am not sure why I had to put quotes around Null. In the table preview in ArcCatalog it states <Null> for null values.

    if (layerName === 'County_City_Py') {      if (feature.attributes.CITY_NAME === "Null") {       var CityCountyTemplate = new InfoTemplate("",       "City: n/a <br/> County: ${COUNTY} <br/> State: ${STATE_NAME}");       CityCountyTemplate.setTitle("Search Result")       feature.setInfoTemplate(CityCountyTemplate);      }      else {       var CityCountyTemplate = new InfoTemplate("",       "City: ${CITY_NAME} <br/> County: ${COUNTY} <br/> State: ${STATE_NAME}");       CityCountyTemplate.setTitle("Search Result")       feature.setInfoTemplate(CityCountyTemplate);      }     }

View solution in original post

0 Kudos
2 Replies
JustinChmura
Deactivated User
The InfoTemplate does give you the ability to execute a function that returns the template. You can view the documentation here, but using your code, you could try this:

if (layerName === 'County_City_Py') {
    var CityCountyTemplate = new InfoTemplate(function (graphic) {
        var attr = graphic.attributes;
        var template = "City: ";
        template += (attr.CITY_NAME === null) ? 'n/a' : attr.CITY_NAME;
        template += '<br />County: ' + attr.COUNTY;
        template += '<br />State: ' + attr.STATE_NAME;
        return template;
    });
    CityCountyTemplate.setTitle("Search Result")
    feature.setInfoTemplate(CityCountyTemplate);
}
0 Kudos
AndrewL
Frequent Contributor
The InfoTemplate does give you the ability to execute a function that returns the template. You can view the documentation here, but using your code, you could try this:

if (layerName === 'County_City_Py') {     var CityCountyTemplate = new InfoTemplate(function (graphic) {         var attr = graphic.attributes;         var template = "City: ";         template += (attr.CITY_NAME === null) ? 'n/a' : attr.CITY_NAME;         template += '<br />County: ' + attr.COUNTY;         template += '<br />State: ' + attr.STATE_NAME;         return template;     });     CityCountyTemplate.setTitle("Search Result")     feature.setInfoTemplate(CityCountyTemplate); }


Thanks for your response. I checked out that link and tweaked your code. This is working now. I am not sure why I had to put quotes around Null. In the table preview in ArcCatalog it states <Null> for null values.

    if (layerName === 'County_City_Py') {      if (feature.attributes.CITY_NAME === "Null") {       var CityCountyTemplate = new InfoTemplate("",       "City: n/a <br/> County: ${COUNTY} <br/> State: ${STATE_NAME}");       CityCountyTemplate.setTitle("Search Result")       feature.setInfoTemplate(CityCountyTemplate);      }      else {       var CityCountyTemplate = new InfoTemplate("",       "City: ${CITY_NAME} <br/> County: ${COUNTY} <br/> State: ${STATE_NAME}");       CityCountyTemplate.setTitle("Search Result")       feature.setInfoTemplate(CityCountyTemplate);      }     }
0 Kudos