Select to view content in your preferred language

Populate Combobox from FeatureSet

1726
14
10-12-2010 07:46 AM
DonCaviness
Occasional Contributor
I have a combobox that I want to populate with a field that is being returned in a featureSet from a query.  I am querying a table and getting the results but I am having trouble figuring out what I am missing to populate the combobox.  Here is the code I am using.  Can anyone see what I am doing wrong?

for each (var graphic:Graphic in featureSet.features)
{
 var category:String = graphic.attributes.category;
 var subcat:String = graphic.attributes.subCategory;
       
 var data:Object = {
  category: category,
  subcat: subcat
 }
 cboCategory.dataProvider = data.subcat;
}
Tags (2)
0 Kudos
14 Replies
DasaPaddock
Esri Regular Contributor
0 Kudos
Drew
by
Frequent Contributor
Also.. Set your dataProvidor outside of the loop...

Here is a working sample of code populating a combo box.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:esri="http://www.esri.com/2008/ags">
 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.collections.ArrayCollection;
   import com.esri.ags.Graphic;
   import com.esri.ags.tasks.FeatureSet;
   import com.esri.ags.events.QueryEvent;
   
   private function queryData():void
   {
    queryTask.execute(query);
    queryTask.addEventListener(QueryEvent.EXECUTE_COMPLETE, onQueryComplete);
   }
   
   private function onQueryComplete(event:QueryEvent):void
   {
    var featureSet:FeatureSet = event.featureSet;
          var results:ArrayCollection = new ArrayCollection();
       
       for each (var graphic:Graphic in featureSet.features)
       {
     var fieldValue:String = graphic.attributes["STATE_NAME"].toString(); 
     results.addItem({label: fieldValue, data:graphic});
          }
          cmb.labelField = "label";
          cmb.dataProvider = results;
   }

  ]]>
 </mx:Script>
 

        <esri:QueryTask
            id="queryTask"
            url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"/>
        <esri:Query
            id="query"
            returnGeometry="false"
            where="1=1"
            outFields='["STATE_NAME"]'/>

 <mx:ComboBox id="cmb" x="179" y="205" labelField=""></mx:ComboBox>
 <mx:Button x="87" y="205" label="Load Data" click="queryData();"/>
 

</mx:Application>




Drew
0 Kudos
NadeemShaukat
Deactivated User
Also.. Set your dataProvidor outside of the loop...

Here is a working sample of code populating a combo box.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:esri="http://www.esri.com/2008/ags">
 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.collections.ArrayCollection;
   import com.esri.ags.Graphic;
   import com.esri.ags.tasks.FeatureSet;
   import com.esri.ags.events.QueryEvent;
   
   private function queryData():void
   {
    queryTask.execute(query);
    queryTask.addEventListener(QueryEvent.EXECUTE_COMPLETE, onQueryComplete);
   }
   
   private function onQueryComplete(event:QueryEvent):void
   {
    var featureSet:FeatureSet = event.featureSet;
          var results:ArrayCollection = new ArrayCollection();
       
       for each (var graphic:Graphic in featureSet.features)
       {
     var fieldValue:String = graphic.attributes["STATE_NAME"].toString(); 
     results.addItem({label: fieldValue, data:graphic});
          }
          cmb.labelField = "label";
          cmb.dataProvider = results;
   }

  ]]>
 </mx:Script>
 

        <esri:QueryTask
            id="queryTask"
            url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"/>
        <esri:Query
            id="query"
            returnGeometry="false"
            where="1=1"
            outFields='["STATE_NAME"]'/>

 <mx:ComboBox id="cmb" x="179" y="205" labelField=""></mx:ComboBox>
 <mx:Button x="87" y="205" label="Load Data" click="queryData();"/>
 

</mx:Application>




Drew


Drew,

Have you tried this sample. When I compiled and ran this sample, it's complaining about FeatureSet. The error message is "1046: Type was not found or was not a compile-time constant: FeatureSet."

Do you have any idea?

nshaukat
0 Kudos
Drew
by
Frequent Contributor
It could be a AGS Flex API version issue that's causing the problem.
The sample provided looks like its using the 1.3 lib.
0 Kudos
Drew
by
Frequent Contributor
Here is an upgraded version for the 2.1 lib.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:esri="http://www.esri.com/2008/ags">
 <mx:Script>
  <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.Graphic;
   import com.esri.ags.events.QueryEvent;
   
   
   import mx.collections.ArrayCollection;
   import mx.controls.Alert;
   
   private function queryData():void
   {
    queryTask.execute(query);
    queryTask.addEventListener(QueryEvent.EXECUTE_COMPLETE, onQueryComplete);
   }
   
   private function onQueryComplete(event:QueryEvent):void
   {
    var featureSet:FeatureSet = event.featureSet;
    var results:ArrayCollection = new ArrayCollection();
    
    for each (var graphic:Graphic in featureSet.features)
    {
     var fieldValue:String = graphic.attributes["STATE_NAME"].toString(); 
     results.addItem({label: fieldValue, data:graphic});
    }
    cmb.labelField = "label";
    cmb.dataProvider = results;
   }
   
  ]]>
 </mx:Script>
 
 
 <esri:QueryTask
  id="queryTask"
  useAMF="false"
  url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"/>
 <esri:Query
  id="query"
  returnGeometry="false"
  where="1=1"
  outFields='["STATE_NAME"]'/>
 
 <mx:ComboBox id="cmb" x="179" y="205" labelField=""></mx:ComboBox>
 <mx:Button x="87" y="205" label="Load Data" click="queryData();"/>
 
 
</mx:Application>



Drew
0 Kudos
NadeemShaukat
Deactivated User
Thanks Drew. It worked.

Do you know if there is way to populate the combobox without using "QueryData" function so I do not have to push the "Load Data" button.

nshaukat
0 Kudos
Drew
by
Frequent Contributor
Thanks Drew. It worked. 

Do you know if there is way to populate the combobox without using "QueryData" function so I do not have to push the "Load Data" button. 

nshaukat


you will want to do it on creationComplete

sample

<?xml version="1.0" encoding="utf-8"?>
<mx:Application creationComplete="creationCompleteHandler(event)" 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    xmlns:esri="http://www.esri.com/2008/ags">
 <mx:Script>
  <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.Graphic;
   import com.esri.ags.events.QueryEvent;
   
   import mx.collections.ArrayCollection;
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   
   
   protected function creationCompleteHandler(event:FlexEvent):void
   {
    queryData();
   }
   
   private function queryData():void
   {
    queryTask.execute(query);
    queryTask.addEventListener(QueryEvent.EXECUTE_COMPLETE, onQueryComplete);
   }
   
   private function onQueryComplete(event:QueryEvent):void
   {
    var featureSet:FeatureSet = event.featureSet;
    var results:ArrayCollection = new ArrayCollection();
    
    for each (var graphic:Graphic in featureSet.features)
    {
     var fieldValue:String = graphic.attributes["STATE_NAME"].toString(); 
     results.addItem({label: fieldValue, data:graphic});
    }
    cmb.labelField = "label";
    cmb.dataProvider = results;
   }
   

   

  ]]>
 </mx:Script>
 
 
 <esri:QueryTask
  id="queryTask"
  useAMF="false"
  url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"/>
 <esri:Query
  id="query"
  returnGeometry="false"
  where="1=1"
  outFields='["STATE_NAME"]'/>
 
 <mx:ComboBox id="cmb" x="179" y="205" labelField=""></mx:ComboBox>
 
 
 
</mx:Application>
0 Kudos
NadeemShaukat
Deactivated User
Drew,

Thanks much for your prompt response.

nshaukat
0 Kudos
NadeemShaukat
Deactivated User
I thought I was on my way to populate combo boxes using this sample. But when I was going to modify it for multiple combo boxes filled with different fields from one or more services, it brings up "Object Object" in one of the combo boxes instead of right values. Any suggestions from anyone who has used this sample for similar conditions. Any help will be much appreciated.

nshaukat
0 Kudos