Populate DataGrid with FeatureLayer Attributes

3950
15
07-27-2010 10:29 AM
DouglasZedler
New Contributor III
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
AmySmith
New Contributor II
Hi Doug,

I wish I could answer your question!

I am having a similar issue populating a Data Grid with Attributes from an Identify Task applied to a Dynamic Map Service. I've created a custom Info Renderer that contains a Data Grid. I'd like the Data Grid to display "Key" and "Value" columns populated by the identify results contained in data.attributes (exactly as they do in the default renderer).

Any advice from the community would be immensely appreciated!

Thanks,

Amy
0 Kudos
ReneRubalcava
Frequent Contributor
I'm still not 100% clear on FeatureLayers purpose. It would seem looking at examples it's usefulness is really for editing and time-aware data. I'm sure they've covered it in more detail somewhere, but I may have missed it.
If you review this sample, you'll see a quick note about FeatureCollection for FeatureLayer that states you should initialize FeatureCollection on your own. So it's not useful for data binding results of a query.
http://help.arcgis.com/en/webapi/flex/samples/index.html?sample=TemporalRenderer_FeatureCollection

So far the method I have been using to extract attributes from a FeatureLayer is manually.
protected function extractAttributesFromFeatures(features:Array):Array {
 var f:Array = [];
 var g:Graphic;
 for each (g in features) {
  f.push(g.attributes);
 }
 return f;
}


Then you can set that result to a Datagrid dataprovider.
0 Kudos
NathanEnge
Esri Contributor
If you download the Sample Flex Viewer and look at the MXML for the Query Builder it should give you an idea on how to do it. There is a viewstack that is a datagrid populated from a selected set.
QBData.dataProvider = fset.attributes;

Then

  <mx:VBox width="100%" height="95%" minHeight="0" minWidth="0">
     <mx:DataGrid id="QBData" width="100%" height="100%" itemClick="itemClick(event);" minColumnWidth="100" resizableColumns="true">
     </mx:DataGrid>
    </mx:VBox>
0 Kudos
AmySmith
New Contributor II
Thanks for sharing your knowledge!

I'm able to use the attributes as the dataProvider for the dataGrid. Now I'd like to specify dataFields for dataGridColumn components, where the name of the field is in one column and the associated value in another. I think I'm having trouble accessing the arrays within the attributes object. Basically, I'm not sure what to insert as the dataField:

<mx:DataGrid id="attributeTable" dataProvider="{data}"> 
 <mx:columns>
  <mx:DataGridColumn headerText="Key" dataField="{?}"/>
  <mx:DataGridColumn headerText="Value" dataField="{?}"/>
 </mx:columns>
</mx:DataGrid>


-Amy
0 Kudos
DouglasZedler
New Contributor III
I'm still not 100% clear on FeatureLayers purpose. It would seem looking at examples it's usefulness is really for editing and time-aware data. I'm sure they've covered it in more detail somewhere, but I may have missed it.


So, do you typically use GraphicLayer?

So far the method I have been using to extract attributes from a FeatureLayer is manually.

...

Then you can set that result to a Datagrid dataprovider.


How do you get the reference to the features in the FeatureLayer object to pass as a parameter to your extractAttributesFromFeatures function?

Thanks,
Doug
0 Kudos
ReneRubalcava
Frequent Contributor
I use GraphicsLayer for miscellaneous selections, more like stuff where I just want to see a boundary, but don't care too much about the data behind it except maybe a name.
I use the FeatureLayer for a couple of items where I can leverage the RelationshipQuery to easily pull in detailed data. I can't tell you how much easier this has made pulling detailed data from SDE tables for me. Previously, I would have had to use a WebService/RemoteObject to hit the SDE directly for table queries. Now that we have access to AMF in the QueryTask which is built into FeatureLayer, it's cut my service calls down drastically.

When you do a selection using a FeatureLayer it returns an array of graphics, each with an attribute field.
So when I do this
AsyncToken(fLayer.selectFeatures(query, "new", new AsyncResponder(response, onFault)));

I use this
[Bindable]
featuresColl:ArrayCollection; // bind this to a DataGrid dataprovider
 
private function response(features:Array, token:Object = null):void {
 var f:Array = extractAttributesFromFeatures(features); // for loop in previous post
 featuresColl = new ArrayCollection(f);
}


Graphics in the FeatureLayer are automatically show on your map and also stored in flayer.selectedFeatures

So in comparison
flayer.selectedFeatures is equivalent to featureSet.features
but there doesn't seem to be an equivalent pointer to featureSet.attributes.
I imagine this might be for performance reasons to keep Featurelayer efficient.

You can review the FeatureLayerEvent which is the result when using queries with FeatureLayer to see some options
http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/events/FeatureLayerEvent.html
You can see the features array in there and you can also see that you get a FeatureSet when you use fLayer.queryFeatures, but a regular query doesn't show the graphics on the map automatically.
0 Kudos
DasaPaddock
Esri Regular Contributor
To get the features from a FeatureLayer, you can use the inherited graphicProvider property:
http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/layers/GraphicsLayer.html#graphicProvider

FeatureSet.attributes is really nothing more than the extractAttributesFromFeatures function Rene has posted. You could get the attributes like this:

var attributes:Array = new FeatureSet(ArrayCollection(featureLayer.graphicProvider).toArray()).attributes;


Reference:
http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/FeatureSet.html#attributes
0 Kudos
DouglasZedler
New Contributor III
Thank you, Dasa!  That did the job in only two lines of code:

var featureSet:FeatureSet = new FeatureSet(ArrayCollection(firePerimeter.graphicProvider).toArray());
fireDataGrid.dataProvider = featureSet.attributes;


Doug
0 Kudos
DouglasZedler
New Contributor III
Thanks also to Nathan and Rene for the insight and resources!

Doug
0 Kudos