Get field by alias

2428
6
05-22-2012 11:12 AM
raphael_hernandesguimenes
New Contributor
Hi All.

I'm newbie javascript arcgis and i'm in doubt how do i get a field of my layer via alias.

Example:

I can get a field in two ways:

value of field is: ${field_name}'

or

graphic.attributes.field_name


But the field name is too long, so I want to get the field using alias

Can someone help-me?

Thanks
0 Kudos
6 Replies
StephenLead
Regular Contributor III
This should be possible, but can you provide more information - where are you attempting to get the field name (eg when running an Identify, when formatting the results of an infoWindow, etc)?

Are you working with a feature layer, dynamic layer, graphic, etc?  Do you have any sample code showing the problem you're facing?

This may help to show where/how to obtain the field via its alias in your circumstances.

Cheers,
Steve
0 Kudos
raphael_hernandesguimenes
New Contributor
Hi, thank you for your reply.

I need to show the fields within a infowindow.

Example:

infoWindow.setTitle('${my_field_via_alias}');


This infoWindow will be used with a featureLayer that uses join on two table.

I would like to use its alias because field name is very long. It is composed of database_name + database ownder + table name + field name.

So, i want to use field via alias.

Thanks again!
0 Kudos
StephenLead
Regular Contributor III
Hi Raphael,

I'd advise against doing what you're trying.

The fact that the field name is long shouldn't matter to you (as a developer) since the end-user won't ever need to see the long field-name. They'll simply see the value of the long field in the infoWinow.

A field-name is unique (ArcMap will throw an error if not) whereas aliases can be duplicated. The potential risk is that if you're trying to find a field by its alias you're not guaranteed to obtain the correct field.

To rephrase this - why do you need to use the alias rather than the field-name in the infoWindow configuration? It seems like a lot of work for no real return.

Cheers,
Steve
0 Kudos
raphael_hernandesguimenes
New Contributor
As I said, the field name is too large and it consists of database_name +  owner_database.

Because this software will be installed in various locations, the name of the database may change and this can cause a big problem.

I can't use a field name that contains the database_name

Do you understand now?

Please, see attachments

Thank you again.
0 Kudos
ReneRubalcava
Frequent Contributor
If I understand you right, you want to display the alias field name for your data? As far as I know, you still need to use the actual name in the infotemplate, but there are a couple of ways to do it.

If it's a featurelayer, you can get the alias from the fields property.
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/featurelayer.htm#fields

Which is an array of fields
http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/field.htm

You can then iterate the array, find the value you want and apply it.
something like
var content = "";
for (var i=0, field; field = fields; i++) {
    if (field.alias == "TARGET") {
        tmp = field.alias + ": ${" + field.name + "}";
        content += tmp;
    }
}


If not a featurelayer, you can use esri.request to get the field information.
var handle = esri.request({
    url: "URLPATH",
    content: {"f" : "json"},
    handleAs: "json"
});

handle.then(function(result){
    var fields = result.fields;
    // do your iteration to get template data here
});


I'm not sure if that's exactly the solution you are looking for, but I have a similar situtaion with a layer that has a join where I want to display the alias and not the long sde.dbo.blahblah name and this is similiar to how I do it.
0 Kudos
raphael_hernandesguimenes
New Contributor
there is the possibility of obtaining the field by "qualified name".?

In other words, without "sde.dbo" .

I want that the field name not contains "sde.dbo".

do you know?
0 Kudos