2

Hoping someone can tell me if I'm going about this wrong, but I'm executing a QueryTask / Query on a map service and getting back a FeatureSet as expected. Within the attributes of each Graphic, I have two fields that are of type esriFieldTypeDate and their values are the long integer value representing the date. My problem is I'm not seeing it formatted in any way when it's displayed in my dynamically created DataGridColumn. I'd like to see it in MM/DD/YYYY format.

I was hoping to check the attribute's data type and if it was equal to 'esriFieldTypeDate' - run some formatting / conversion method on it. But, I can't even get the data type of the attribute. There is no spatial querying going on here, this is purely based on attribute data entered on a form in the application.

The last if statement of the sample below is an error - just showing my thought process. Also, the "resultFields" variable is an array of field names. I use that to allow the field order to configurable.

Should I not be using a QueryTask for this???

function onResult(featureSet:FeatureSet, token:Object = null):void
{
...
    for each (var gr:Graphic in featureSet.features)
    {
        for (var i:int = 0; i < resultFields.length; i++)
        {
            var hg:HGroup = new HGroup();
            var lab:Label = new Label();
            lab.width = 200;
            var dgc:DataGridColumn = resultFields[i] as DataGridColumn;

            lab.text = dgc.headerText + ": ";
            hg.addElement(lab);

            var valLabel:Label = new Label();
            if (gr.attributes[dgc.dataField].type != "esriFieldTypeDate")
            {
                //Not a date - display text value
                valLabel.text = gr.attributes[dgc.dataField].type;
            }                               
            else
            {
                //Attribute is a date - format it to MM/DD/YYYY
                valLabel.text = gr.attributes[dgc.dataField];   
            }

            // ... add value label to group, add group to column, add column to grid etc.
1
  • Instead of using QueryTask+customgrid, did you try using the AttributeTable component that is part of the API? It should show records from a featurelayer in a grid (with date formatting applied). Commented Sep 12, 2014 at 23:00

1 Answer 1

1

I found the answer to this. In order to determine the data type on the fields contained in the FeatureSet's Graphics, you need to look at the fields property of the FeatureSet itself. My solution was to iterate through them and store fields that were of type date into an Array:

var dateFields:Array = new Array();
if (dateFields.length < 1)
{
    for each (var f:Field in featureSet.fields)
        {
            if (f.type == Field.TYPE_DATE)
            {
                dateFields.push(f.name);
            }
        }
    }
}

Then, since my application is configurable, another list is used to determine which attributes from the Graphic should be displayed/read. When iterating through that list of attributes to fetch, I compare that attribute to the fields stored within the Array I created above. If it's found in the array, I know it's a date field and format it appropriately.

var valLabel:Label = new Label();
if (dateFields.indexOf("<field name goes here>") < 0)
{
    //Not a date
    valLabel.text = gr.attributes[dgc.dataField];
}
else
{
    //Date field - format to mm/dd/yyyy
    valLabel.text = FormatDate(gr.attributes[dgc.dataField], "MM/DD/YYYY", true);           
}                           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.