How to format numeric attribute values in an infoWindow

730
4
11-10-2010 12:42 PM
StephenLead
Regular Contributor III
Is it possible to format attribute values in an infoWindow?

For example, see the sample at http://help.arcgis.com/EN/webapi/javascript/arcgis/demos/query/query_hover.html

The infoWindow template uses population density values from attribute values in a variable:

infoTemplate.setContent("<b>2000 Population: </b>${POP2000}<br/>"
                             + "<b>2000 Population per Sq. Mi.: </b>${POP00_SQMI}<br/>"
                             + "<b>2007 Population: </b>${POP2007}<br/>"
                             + "<b>2007 Population per Sq. Mi.: </b>${POP07_SQMI}");
Is it possible to apply formatting to these values, eg so the POP07_SQMI value is rounded to zero decimal places?

Thanks,
Steve
0 Kudos
4 Replies
BradCochran
New Contributor
not sure if there is a fancy way to do it, but the boring javascript way is to just use Math.round

for example,

Math.round(2232.43534) //returns 2232

so you could do something like,

var popRounded = Math.round(POP07_SQMI);
0 Kudos
StephenLead
Regular Contributor III
Hi Brad,

Thanks for the reply. As far as I can see, the problem is that the value of the attribute isn't evaluated at the time that the infoTemplate is created (so there's nothing to round yet). The code is setting up a template that is read at the point at which the graphic object is created.

Put another way - I can't see how to put the Math.round directive in the code above, and have it run as JavaScript, rather than being quoted as text. I'm not a JS expert.......

Any help would be appreciated.

Cheers,
Steve
0 Kudos
KellyHutchins
Esri Frequent Contributor
This question comes up frequently so I added a new sample to the ArcGIS Resource Center this morning showing how to format the contents of an info window.  The sample formats string values but could easily be modified to apply numeric or date formatting.

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jssamples/fl_infowindow.html

FYI: We are making some changes at 2.2 that will simplify this process.
0 Kudos
StephenLead
Regular Contributor III
Thanks again Kelly, I'll give it a go.

It would definitely be nice to be able to include the formatting directions in the infoWindow template, rather than having to listen for the click event.
0 Kudos