Select to view content in your preferred language

Populate DataGrid with FeatureLayer Attributes

4701
15
07-27-2010 10:29 AM
DouglasZedler
Occasional Contributor
How do you populate a DataGrid with the attributes of features in a FeatureLayer?  I've searched high and low and can't find the answer to this question, which is frustrating because it seems so fundamental.  Apparently, you can set the dataProvider property of the DataGrid to the attributes property of a FeatureSet object.  However, I can't get a reference to a FeatureSet object containing all the features in a FeatureLayer.  I've tried using the featureCollection property of the FeatureLayer object, but that is always null.  I've seen ways to do it using queries and selections, but I want to put the attributes of all the features from the FeatureLayer in the DataGrid.

Thanks,
Doug
Tags (2)
0 Kudos
15 Replies
DavidBoiano
Deactivated User
Hello,

I have been following the information on this thread and it makes great sense and seems like a good starting point for what I am trying to do, but I can't get past this error: -1067: Implicit coercion of a value of type Array to an unrelated type mx.collections:IList.

My ultimate goal is to develop a data export button that will create an excel file (.xls) containing the values that are currently in the AttributeTable (essentially recreating the Table Options export to .csv, without concern of exporting location).  My thought was to use the code presented in this thread to assign the attributes from the featureLayer to a separate DataGrid that can be used as the object reference for exporting to excel.  I am using the as3xls library for this http://code.google.com/p/as3xls/

I am not sure what is causing the error. This is my code:
//Code for Data Download button
   public function dataDownload():void
   {
    var featureSet:FeatureSet = new FeatureSet(ArrayCollection(myFeatureLayer.graphicProvider).toArray());
    ssoDataGrid.dataProvider = featureSet.attributes;
   }



Any help in the right direction would be greatly appreciated! Thank you.

David
0 Kudos
DasaPaddock
Esri Regular Contributor
0 Kudos
DavidBoiano
Deactivated User
Hi Dasa,

Thank you so much for the code and resources! That solved my error problem.  It is almost working to how I need it--perhaps you or someone else can help me fine tune it!

My application has a feature layer that uses a drop down list to change the definitionExpression property of the feature layer to only show point features from a specific time frame.  There is an attribute table at the bottom of the page which updates to show only the features in the current extent, which changes with the definitionExpression.  I have created an AttributeTable skin which has removed the Table Options button because my boss did not want it there, but he does want the option to export the data in the Attribute Table to to an excel (.xls) file (not csv!) by clicking a button in the header.

I need to figure out some way to grab the data that is only in the current extent.  Currently, my code is grabbing the attributes from all of the features in the feature layer as defined by the current definitionExpression.  Is there some way to copy the data in the attribute table? Or some way to only add features to the featureSet that are in the current map extent?

Thanks!

David
0 Kudos
DasaPaddock
Esri Regular Contributor
You can use Extent.intersects() to check if each feature is in the map's extent.

See:
http://resources.arcgis.com/en/help/flex-api/apiref/com/esri/ags/geometry/Extent.html#intersects()
0 Kudos
DavidBoiano
Deactivated User
Dasa,

Thanks again for your continual assistance.  I have been struggling with making the appropriate for loop to loop through each feature in my feature layer.  My thought process was to create a new array to populate with the features that returned as true with the extent.intersects() method.  I am not sure on the correct loop to use or the syntax I guess because I can't get it to work correctly.

Here is my latest attempt:

   public function dataDownload():void
   {
    var featureSet:FeatureSet = new FeatureSet(ArrayCollection(myFeatureLayer.graphicProvider).toArray());
    var record:Array = featureSet.attributes;
    var exportSet:Array = new Array();
    for each(record in featureSet){
     if (myMap.extent.intersects(featureSet.features.geometry)){
      exportSet.push(record)
     }
    }
    ssoDataGrid.dataProvider = new ArrayList(exportSet.attributes);
   }


I have been using http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#for..in to try and use the correct syntax for a for each...in loop.  I am very confused by what the variable "record" (in my case) refers to.  In the link, they use "item" to loop through but it doesn't seem to be defined anywhere or have a set data type.

Any ideas, or am I going off in the wrong direction??

Thank you!

David
0 Kudos
DavidBoiano
Deactivated User
I have also unsuccessfully tried:

   public function dataDownload():void
   {
    var featureSet:FeatureSet = new FeatureSet(ArrayCollection(myFeatureLayer.graphicProvider).toArray());
    var record:Array = featureSet.attributes;
    var exportSet:Array = new Array();
    for (var i:int=0; i<record.length; i++){
     if (myMap.extent.intersects(record.geometry)){
      exportSet.addItemAt(record,i)
     }
    }
    ssoDataGrid.dataProvider = new ArrayCollection(exportSet);
   }


Trial and error continues!
0 Kudos