Select to view content in your preferred language

replace Null function

1111
1
12-05-2011 05:14 AM
fabryvincent
New Contributor
Hi,
I'm using an InfoTemplate to display some data in a Popup :

  
 var template = new esri.InfoTemplate();
    template.setTitle("${NOM}");
    template.setContent("<b>Adresse</b>: ${ADR_POST} - ${COMMUNE} <br/> <b>Permanence</b> : ${JOURS_PERM} - ${HEURE} <br/> <b>Services</b> : ${SERVICE:ReplaceNull} - ${SERVICE2}");
    feature.setInfoTemplate(template);


Yet if the field SERVICE is empty, null is written in the popup. How can I replace null value by something else (for instance just a "") ?

Thanks,
Vincent
0 Kudos
1 Reply
KellyHutchins
Esri Notable Contributor
You can define the content for the info window or popup by providing a function that can calculate the value. The 'Format info window content' help topic has details on the various options.

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jshelp/intro_formatinfowindow.htm

Here's an example of how you can use a custom formatting function to determine if a field is null. If its not null you can display a value and if null you can display different text.  This code snippet defines an info template with a custom formatting function called calculateDeaths that determines if the Num_Deaths field has a value.

        var  infoTemplate = new esri.InfoTemplate("${Name}");
        infoTemplate.setTitle("${Name}");
        infoTemplate.setContent("Magnitude ${Magnitude:NumberFormat(round:2)} earthquake occurred on ${Date_:DateFormat(datePattern:'MMMM d, yyyy.', selector:'date')}<br/>Deaths: ${Num_Deaths:calculateDeaths}" );


Here's what the function looks like:
      function calculateDeaths(value,key,data){
        return data.Num_Deaths !== null ? data.Num_Deaths : 'No Deaths';
      
      }


Alternatively you can use a function to define the entire window's content so if you like you can not display null values. Here's a code snippet showing how you can define an info template that uses a function to display content:
     
  var  infoTemplate = new esri.InfoTemplate();
        infoTemplate.setTitle("${Name}");
        infoTemplate.setContent(getContent);


And here's the function that defines the content:
      
      function getContent(graphic){
        var numInjured = graphic.attributes.Num_Injured;
        var numDeaths = graphic.attributes.Num_Deaths !== null ? graphic.attributes.Num_Deaths : 'No Deaths';
        var content = "<b>Magnitude: </b>" + graphic.attributes.Magnitude + "<br/><b>Deaths: </b> " + numDeaths ;
        if(numInjured !== null){
          content += "<br/><b>Injured: </b>" + numInjured;
        }
        return content;
      }
      

0 Kudos