Select to view content in your preferred language

Get Fields ?

4457
18
10-13-2010 11:30 AM
Drew
by
Regular Contributor
Hello,
I am doing a small ESRI JS API project and don't use it too often so the library is new to me..
Anyways.. I cant find anyway to get field information.

I am trying to write a function that will determine what the the OBJECTID field name is based on its data type (esriFieldTypeOID)

The object is in the documentation (Field) but I can't find a way to get a field?

In the Flex API you can call a a function like so...


mapServiceLayer.getLayerDetails(...)



Please help me get a fields array.

Drew
0 Kudos
18 Replies
Drew
by
Regular Contributor
Thanks for the assistance everyone.
I ended up going with the FeatureLayer to get the information i needed.
I don't think it's the easiest way, but it works....
I would like to see the info come in when the AGSDynamicLayer gets loaded, ArcIMS did that.

I am still looking for better ways if someone else can point me in the right direction.
I feel that I am missing something obvious.....

Anyways.. here is my javascript code to get feature layer information (i.e field names and types)
The GetFeatureLayerInfo function does the work, the other is just sample code to test.

[php]
function TestFeatureLayerInfo(map, serviceURL, layerID)
{

    GetFeatureLayerInfo(map, serviceURL, layerID,
    function (featureLayerInfo)
    {
        alert("ObjectID Field: " + featureLayerInfo.objectIdField);

        var fieldInfos = "";
        for (var i = 0; i < featureLayerInfo.fields.length; i++)
        {
            var field = featureLayerInfo.fields;
            fieldInfos += field.name + "\t=\t" + field.type + "\n";
        }

        alert("Fields: " + fieldInfos);
    });
}

function GetFeatureLayerInfo(map, serviceURL, layerID, callbackFunction)
{

    var featureLayerURL = serviceURL + "/" + layerID;
    var featureLayer = new esri.layers.FeatureLayer(featureLayerURL, { mode: esri.layers.FeatureLayer.MODE_SELECTION });

    var layerAddResult_connect = dojo.connect(map, "onLayerAddResult", function (featureLayerInfo)
    {
        map.removeLayer(featureLayer);
        dojo.disconnect(layerAddResult_connect);

        if (callbackFunction != null)
        {
            callbackFunction(featureLayerInfo);
        }
    });
    map.addLayer(featureLayer);
}
[/php]

Drew
0 Kudos
JeffPace
MVP Alum
I am having a heck of a time with this.  I am using javascript api2.2, server 10sp1 for java

have a featureLayer

all the properties return undefined.  Cant get fields, version, anything.. very frustrating.
0 Kudos
derekswingley1
Frequent Contributor II
@jeff can you post some code that's not working for you?
0 Kudos
JeffPace
MVP Alum
var mapLayer=  new esri.layers.FeatureLayer(layer.url,{
                                   mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
                                    outFields: ["*"]              
                                });
alert(mapLayer.fields);  (undefined)
alert(mapLayer.version);   (undefined)

etc...
0 Kudos
derekswingley1
Frequent Contributor II
Those properties aren't defined until your feature layer loads...try:

var mapLayer= new esri.layers.FeatureLayer(layer.url,{
  mode: esri.layers.FeatureLayer.MODE_SNAPSHOT,
  outFields: ["*"] 
});
dojo.connect(mapLayer, 'onLoad', function() {
  alert(mapLayer.fields); (undefined)
  alert(mapLayer.version); (undefined)
});
0 Kudos
JeffPace
MVP Alum
Wow that works, an undocumented "feature" eh?

The whole reason I am doing this is because queries return coded values and not the "name" value of a domain.  Very frustrating.

Thank you for the advise.
0 Kudos
derekswingley1
Frequent Contributor II
Glad to help.

I wouldn't say this is a feature or a bug...it's just the asynchronous nature of JavaScript. You can't always assume that because a line of code comes after a variable definition that all properties of that variable are populated and accessible. Reading up on callbacks and how they're used in JavaScript would be a good idea. Here's a good start:  Getting comfortable with Javascript callbacks

Money quote: 
when you call a method, you know that it has done all of its work and returned something useful.  Right?  Well�?� not always.  Sometimes that method will invoke web requests that need some time to process, or it may call another method that has to do some processing or is waiting for user input�?�  These kinds of operations can be implemented asynchronously, and the only way to manage that logic programmatically is with a callback parameter.


I agree on the point about using fields that use domains...there's room for improvement there.
0 Kudos
JeffPace
MVP Alum
I get the asynchronous nature of Javascript.

What i do not understand is where in the API

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/featurelayer.htm

They say the the Properties of a Feature Layer are not available until it is loaded.
0 Kudos
derekswingley1
Frequent Contributor II
Right, properties are available when the layer is loaded. They're not necessarily available immediately after calling calling a layer constructor hence the reason you need to listen to the layer's onLoad event if you want to access layer properties as soon as the layer loads.

This isn't explicitly spelled out in the feature layer documentation you linked to but it is documented for all layers in the Layer class documentation:  http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi/layer.htm

When the layer is loaded, the value becomes "true", and layer properties can be accessed. The onLoad event is also fired.
0 Kudos