how to populate other fields in data grid

577
3
05-09-2011 02:54 PM
MikeJun
New Contributor II
I'd like to populate more fields in datagrid in 'select graphic within extent' sample code(http://resources.esri.com/help/9.3/arcgisserver/apis/flex/samples/index.html).

I add 'results.addItem(graphic.attributes.STATE_NAME);' in drawEndHandler function and add <mx:DataGridColumn dataField="STATE_NAME"/> in datagrid.

However it returns only city name attribute in both fields.

thanks in advance.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:esri="http://www.esri.com/2008/ags"
    layout="horizontal"
>

    <mx:initialize>
        <![CDATA[

             var queryTask:QueryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/Map...");
             var query:Query = new Query();
             query.where = "STATE_NAME = 'Washington'";
             query.returnGeometry = true;
             query.outFields = ["CITY_NAME", "STATE_NAME"];
             queryTask.execute(query, new AsyncResponder(onResult, onFault));

             function onResult(featureSet:FeatureSet, token:Object = null):void
             {
                for each (var myGraphic:Graphic in featureSet.features)
                {
                        myGraphic.symbol = defaultSymbol;
                        myGraphicsLayer.add(myGraphic);
                }
             }
             function onFault(info:Object, token:Object = null):void
             {
                Alert.show(info.toString());
             }

             drawToolbar.activate(Draw.EXTENT);
        ]]>
    </mx:initialize>
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import com.esri.ags.geometry.MapPoint;
            import com.esri.ags.geometry.Extent;
            import com.esri.ags.events.DrawEvent;
            import mx.controls.Alert;
            import com.esri.ags.Graphic;
            import mx.rpc.AsyncResponder;
            import com.esri.ags.tasks.Query;
            import com.esri.ags.tasks.QueryTask;
            import com.esri.ags.tasks.FeatureSet;

            private function drawEndHandler(event:DrawEvent):void
            {

                var extent:Extent = event.graphic.geometry as Extent;
                var graphic:Graphic;
                var results:ArrayCollection = new ArrayCollection;
                for (var i:Number = 0 ; i < myGraphicsLayer.numChildren ; i++)
                {
                    graphic = myGraphicsLayer.getChildAt(i) as Graphic;
                    //if point is contained within extent, highlight it and add for display in results list
                    if (extent.contains(MapPoint(graphic.geometry)))
                    {
                        graphic.symbol = highlightedSymbol;
                       
                       
                        results.addItem(graphic.attributes.CITY_NAME);
                        results.addItem(graphic.attributes.STATE_NAME);
                    }
                    //else if point was previously highlighted, reset its symbology
                    else if (graphic.symbol == highlightedSymbol)
                    {
                        graphic.symbol = defaultSymbol;
                    }
                }
                labelPoints.text = "# of points in extent = " + results.length;
                dg.visible = true;
                dg.dataProvider = results;

           }
        ]]>
    </mx:Script>

    <!-- Start Declarations -->
        <esri:SimpleMarkerSymbol id="defaultSymbol" color="0x0000FF" style="circle" size="12" alpha="0.5">
            <esri:SimpleLineSymbol/>
        </esri:SimpleMarkerSymbol>

        <esri:SimpleMarkerSymbol id="highlightedSymbol" color="0xFF0000" style="circle">
            <esri:SimpleLineSymbol/>
        </esri:SimpleMarkerSymbol>

        <esri:SimpleFillSymbol id="sfs" style="solid" alpha="0.5" color="0x000000">
            <esri:SimpleLineSymbol color="0xFF0000"/>
        </esri:SimpleFillSymbol>

        <esri:Draw id="drawToolbar" map="{myMap}" fillSymbol="{sfs}" drawEnd="drawEndHandler(event)"/>
    <!-- End Declarations -->

    <mx:VBox width="100%" height="100%">
        <mx:Text text="Draw a rectangle to select points within an extent" fontSize="12" fontWeight="bold" width="100%"/>
        <esri:Map id="myMap">
            <esri:extent>
                <esri:Extent xmin="-125.9" ymin="44.6" xmax="-114.6" ymax="50.2">
                    <esri:spatialReference>
                        <esri:SpatialReference wkid="4326"/>
                    </esri:spatialReference>
                </esri:Extent>
            </esri:extent>
            <esri:ArcGISTiledMapServiceLayer
                url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
            <esri:GraphicsLayer id="myGraphicsLayer"/>
        </esri:Map>
    </mx:VBox>
    <mx:VBox width="240" height="100%">
        <mx:Text id="labelPoints" fontSize="12" fontWeight="bold" width="100%"/>
        <mx:DataGrid id="dg" visible="false" width="100%" height="100%">
            <mx:columns>
                <mx:DataGridColumn dataField="CITY_NAME"/>
                <mx:DataGridColumn dataField="STATE_NAME"/>
            </mx:columns>
        </mx:DataGrid>
    </mx:VBox>

</mx:Application>
Tags (2)
0 Kudos
3 Replies
RobertMyers
New Contributor
I think you'll be able get you answer from this sample Query Results in a Table.

http://resources.esri.com/help/9.3/arcgisserver/apis/flex/samples/index.html?sample=QueryTaskNoMap
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Mike,

   Here is the necessary change.

   private function drawEndHandler(event:DrawEvent):void
   {
    
    var extent:Extent = event.graphic.geometry as Extent;
    var graphic:Graphic;
    var results:ArrayCollection = new ArrayCollection;
    for (var i:Number = 0 ; i < myGraphicsLayer.numChildren ; i++)
    {
     graphic = myGraphicsLayer.getChildAt(i) as Graphic;
     //if point is contained within extent, highlight it and add for display in results list
     if (extent.contains(MapPoint(graphic.geometry)))
     {
      graphic.symbol = highlightedSymbol;
      results.addItem({CITY_NAME: graphic.attributes.CITY_NAME, STATE_NAME:graphic.attributes.STATE_NAME});
     }
      //else if point was previously highlighted, reset its symbology
     else if (graphic.symbol == highlightedSymbol)
     {
      graphic.symbol = defaultSymbol;
     }
    }
    labelPoints.text = "# of points in extent = " + results.length;
    dg.visible = true;
    dg.dataProvider = results;
   }
0 Kudos
MikeJun
New Contributor II
Robert Myers, thanks for the tip.

Robert Scheitlin, it works like a charm as always. Thanks again!!!
0 Kudos