Format infoTemplate content with custom function

3767
11
Jump to solution
09-24-2013 09:27 AM
RyanClancy
Occasional Contributor
I'm trying to format the value of a field for display in an infoTemplate but nothing is happening; it appears as though the function is not being called. I used the example here as a guide, see the compare() function.

I simply want to format the text to display either Gbps or Tbps based on the value of the field. The relevant field is called "Capacity_G" and I put the console.log statements in there to test whether the function was executing; it is not.

var infoTemplate = new InfoTemplat(); infoTemplate.setTitle("<b>Cable: ${NAME}</b>"); infoTemplate.setContent("Capacity: ${CAPACITY_G:formatCapacity}");  function formatCapacity(value){     console.log("LALALALALAA");     var cap = "";     //1Tb = 1024Gb     if (value >= 1024){         cap = value/1024 + "Tbps";     }     else {         cap = value + "Mbps";     }     console.log("LALALALALALALA");     return cap; }


What have I done wrong? The text in the resulting popup appears unmodified, exactly as it is in the attributes of the data.
0 Kudos
11 Replies
DominickCisson
Occasional Contributor
Are you using 3.7? graphic.attr() was introduced at 3.7 (we'll update the docs to say this).

Attr is also used to add attributes to a graphic's DOM node, it doesn't affect graphic.attributes (which I admit is confusing). If you want to add to or update a graphic.attributes, modify that object directly.


Do you know of an example of how to add a real attribute to a graphic?  and I'll second the original poster here, graphic.attr is very confusing.  What exactly is the point of adding an attribute to a graphic's DOM node?
0 Kudos
DominickCisson
Occasional Contributor
Do you know of an example of how to add a real attribute to a graphic?  and I'll second the original poster here, graphic.attr is very confusing.  What exactly is the point of adding an attribute to a graphic's DOM node?


Never mind... Figured it out.  It's JSON 101 Duh.

Just set the attribute directly... graphic.attribute.<attribute name> = "New value"
0 Kudos