Identify - How to show Alias filed instead of Field Name

6013
9
07-07-2010 11:56 AM
AlexanderAnkrah
New Contributor III
Hi,

Can anyone shed light on how I could display the Alias field of a layer when using the Identify task?

By default it displays the field names. However, I would like to display the Alias as this has got more user friendly naming.

Can any help?

Cheers
0 Kudos
9 Replies
derekswingley1
Frequent Contributor
If you use a query task, you get field aliases back with your query result.

If you're stuck using identify, can you specify your user-friendly names in your info template? Otherwise, I think you're going to have to send off an additional request to get the layer info from the map service, build a field_name: field_alias object and then use the aliases in your info window content. I don't see a way to access field aliases from an identify result. Maybe someone else knows...
0 Kudos
AlexanderAnkrah
New Contributor III
If you use a query task, you get field aliases back with your query result.

If you're stuck using identify, can you specify your user-friendly names in your info template? Otherwise, I think you're going to have to send off an additional request to get the layer info from the map service, build a field_name: field_alias object and then use the aliases in your info window content. I don't see a way to access field aliases from an identify result. Maybe someone else knows...


Thanks for getting back to me Swingley.

I am kind of stuck using identify. We are currently hard coding user-friendly names into our info template.

What I'm now trying to achieve is to programmatically display the  Alias. Could you provide more info on the requesting for layer info from the map service part of your response? A sample code perhaps?

I'm trying to get my head round using this approach (which I think is what you mean?) but it's just not working.

http://forums.arcgis.com/threads/3971-HowTo-Get-Field-Names-From-Layer?highlight=layer+alias

Any help? Thanks
0 Kudos
derekswingley1
Frequent Contributor
Something like this:
<!DOCTYPE html">
<html>
  <head>
    <script type="text/javascript">var djConfig = {parseOnLoad: true};</script>
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.0"></script>
    <script type="text/javascript">
      dojo.require("esri.map");
      var field_obj = {};

      function init() {
        var layer_url = 'http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Hurricanes/NOAA_Tracks_1851_2007/MapServer/0'
        esri.request({
          url: layer_url,
          content: { f: 'json' },
          callbackParamName: 'callback',
          load: function(response, io) {
            var fields = response.fields;
            dojo.byId('fields').innerHTML = '';
            dojo.forEach(fields, function(field) {
              dojo.byId('fields').innerHTML += 'Name: ' + field.name + '; Alias: ' + field.alias + '<br />';
              field_obj[field.name] = field.alias;
            });
          },
          error: function(error) {
            console.log('error: ', error);
          }
        });
      }
      dojo.addOnLoad(init);
    </script>
  </head>
  <body>
    <div id="fields">field names and aliases will show up here.</div>
  </body>
</html>


You can mess around with the "field_obj" variable in the firebug console since it's global. You would use this object when constructing your info window content to map field names with field aliases.
0 Kudos
AlexanderAnkrah
New Contributor III
Thanks ever so much Swingley!! Exactly What I was after!!!

I'm still working on this, but perhaps you could help (if it's not a bother) seeing you are well versed in this that I.

Reading through what you've provided makes perfect sense. I was however trying expand on it... including it in my code which  identifies multiple layers on click but it doesn't want to play ball!!

My code is pretty much like ESRI's example 

http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm

save I'm not using the infoWindow, but that's cosmetics. Any ideas?

Thanks again!!!

attached is a section of my code.
0 Kudos
AlexanderAnkrah
New Contributor III
OK, I have re written the/my (addToMap) function...

But I have a nagging problem and wondering if I could get some pointers....

From my initial problem this approach gives me what I want.

function addToMap(idResults, evt) {
 var strContent="";
 for(var i in idResults){
  if(idResults.layerId===2||idResults.layerId===3)
  {
   //Header
   strContent += "<label style='font-size:14px; text-decoration:underline;'>"+idResults.layerName+"</label> <br/>";
   //Attributes
   switch(idResults.layerId){
    case 2:
     strContent += " -- <a href=\"" + idResults.feature.attributes.LANDUSE + "\"target=\"_blank\" >Land Use: " + idResults.feature.attributes.LANDUSE + "</a><br/>";
     break;
    case 3:
     strContent += " -- <a href=\"" + idResults.feature.attributes.Shape + "\"target=\"_blank\" >Shape: " + idResults.feature.attributes.Shape + "</a><br/>";
     break;
   }
  }
 }
 
 if (strContent == '') {
        document.getElementById('divMiniResults').innerHTML = 'No Data at this location. <br/><br/>';
    }
    else {
        document.getElementById('divMiniResults').innerHTML = strContent;
    }

}


The problem however is I am getting repeated values (see attached img) being returned on some of the values depending on where I click on the map!

Pulled the little hair I've got out but can't seem to locate where the bug is!!

Any pointers.
0 Kudos
derekswingley1
Frequent Contributor
You don't need that if statement. Just use your switch statement inside your for loop.
0 Kudos
AlexanderAnkrah
New Contributor III
Thanks Derek,

Tried that, still having the repetition.

Cheers
0 Kudos
derekswingley1
Frequent Contributor
OK...what else have you tried?
0 Kudos
AlexanderAnkrah
New Contributor III
Ok think I cracked it...
The loop was doing exactly as it should... adding whatever it found to my result container... hence the repetition.

This here bundles the result for a particular layerID in an array then puts that bundle in the result container afterwards!

I know this doesn't quiet show the alias but now I can fit your initial code into in.

Thanks for your help.


function addToMap(idResults, evt) {
    var contentArray= new Array();
    var resultLayerId;
 //var strContent="";
 for (var i in idResults){
     resultLayerId = idResults.layerId;
     idResults.feature.setInfoTemplate(new esri.InfoTemplate("", "${*}"));
     if (contentArray[resultLayerId] === undefined)
     {
   // write heading for that layer
   contentArray[resultLayerId] = "<label style='font-size:14px; text-decoration:underline;'>"+idResults.layerName+"</label> <br/>";
   // write record result
   contentArray[resultLayerId] += idResults.feature.getContent(); 
  }
  else
  {
   // Heading already written. Append feature details
   contentArray[resultLayerId] +=  idResults.feature.getContent();
  }
  
  }
  
  if (contentArray.length === 0) {
        document.getElementById('divMiniResults').innerHTML = 'No Data at this location. <br/><br/>';
    }
    else {
        document.getElementById('divMiniResults').innerHTML = contentArray.join('<br />');
    }

      }
0 Kudos