Example for CodedValueDomians

5620
8
04-29-2011 03:06 AM
MartinOver
New Contributor III
Hi,

Has anybody a working example how to deal with CodedValue Domains.

I have a Query Task on a layer with coded values and I want to show the names instead of the values in an InfoWindow. I could see the data via the rest interface:

e.g.: ... Domainn: Coded Value: [1:yes], [2:no]...

Best,

Martin
0 Kudos
8 Replies
Mete_ErcanPakdil1
New Contributor III
I'm also looking for an example for this, I hope someone have an example?
0 Kudos
DonKang
New Contributor III
Same here.
FindTask do return domain values but QueryTask doesn't.

Don
0 Kudos
derekswingley1
Frequent Contributor
The general workflow would be:

  • use esri.request() to get your layer's metadata

  • keep track of fields with domains

  • build an object for each field with a domain to map coded values to actual values

  • execute a query

  • when processing the query results, map the coded values from the attributes to actual values

0 Kudos
Mete_ErcanPakdil1
New Contributor III
The general workflow would be:

  • use esri.request() to get your layer's metadata

  • keep track of fields with domains

  • build an object for each field with a domain to map coded values to actual values

  • execute a query

  • when processing the query results, map the coded values from the attributes to actual values



Thank you Derek, I will test this workflow and give an example here.

Btw, Is there any Esri's sample server which has coded value domain fields?
0 Kudos
derekswingley1
Frequent Contributor
Looking forward to see what you come up with. This service has several layers with coded value domains:  http://sampleserver5.arcgisonline.com/ArcGIS/rest/services/Energy/Geology/MapServer
0 Kudos
JeffPace
MVP Alum
Looking forward to see what you come up with. This service has several layers with coded value domains:  http://sampleserver5.arcgisonline.com/ArcGIS/rest/services/Energy/Geology/MapServer


We query each layer for the layer description.  We then pass the resulting featureset through a function to replace names with aliases, format numbers, and replace domains with their values

Function to do formatting/aliases/domains

org.mymanatee.common.util.getAliasDomainFormat=function(layerdesc, fieldName, field_val){
           dojo.forEach(layerdesc.fields, dojo.hitch(this, function(ldf){
                            if(ldf.name==fieldName){
                                //replace with aliases
                               fieldName = ldf.alias;
                             //  alert(ldf.name+" : "+ldf.type);
                              if(ldf.type=="esriFieldTypeDate"){
                               //    alert(ldf.type);
                               field_val=   dojo.date.locale.format(new Date(field_val), {datePattern: "MM/dd/yyyy"});
                             //  alert(field_val);

                              }
                              if(ldf.type=="esriFieldTypeDouble"){
                               //    alert(ldf.type);
                               field_val=   dojo.number.format(field_val,{places: 2 });
                             //  alert(field_val);

                              }

                              if(ldf.domain){
                                  dojo.forEach(ldf.domain.codedValues, dojo.hitch(this, function(domain){
                                      if(field_val==domain.code){
                                        field_val=domain.name;
                                      }
                                  }));
                              }
                            }

                        }));
                        return [fieldName,field_val];
}


function to get the layer description (by querying url with the ?f=json parameter) - ignore the replaces they are a cross-domain hack

org.mymanatee.common.util.getLayerDesc=function(url){
    var desc;
                                    var layerurl= ""+url.replace("http://home.intranet","")+"/?f=json";
                                layerurl= ""+layerurl.replace("http://www.mymanatee.org","");
                                var params = {
                 url: layerurl,
                 handleAs: "json",
                 sync: true
             };
               var deferred =dojo.xhrGet(params);
                    desc= deferred.results[0];
    return desc;
}
Mete_ErcanPakdil1
New Contributor III
Here is my sample code for who need to use.

dojo.require("esri.tasks.query");
function init() {
    var queryTask = new esri.tasks.QueryTask("http://sampleserver5.arcgisonline.com/ArcGIS/rest/services/Energy/Geology/MapServer/1");
    var query = new esri.tasks.Query();
    query.objectIds = [1];
    query.outFields = ["*"];
    queryTask.execute(query, function(featureSet) {
  var domains = [];
  var requestHandle = esri.request({
   url: "http://sampleserver5.arcgisonline.com/ArcGIS/rest/services/Energy/Geology/FeatureServer/1",
   content: {f:"json"},
   callbackParamName:"callback",
   load: function(res){
    dojo.forEach(res.fields,function(f){
     if (f.domain != null)
     {
      domains[f.name] = f.domain.codedValues; 
     }
    });
    dojo.forEach(featureSet.features,function(f)
    {
     
     for(attr in f.attributes)
     {
      if (attr in domains)
      {
       dojo.forEach(domains[attr],function(d)
       {
        if (d.code == f.attributes[attr])
         f.attributes[attr] = d.name;
       });
      }
     }
    });
    console.log(featureSet.features);
   },
   error: function(res){ console.log(res); }
  }, {useProxy:false});
      },function(err){
  console.log(err);
 });
    
}
dojo.addOnLoad(init);
DavidHollema
New Contributor III
For feature layers it's real simple.  The feature layer has a fields property and each field has a domain property.  As soon as the layer is initialized, the fields property is filled.  If you're dealing with ArcGISDynamicMapServiceLayers then follow the previously mentioned approaches.

See http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/codedvaluedomain.htm#codedValues