6 Replies Latest reply on Apr 6, 2009 5:28 AM by k.crider

    Populate Many Combos

    k.crider
      I am new to Flex, just getting my feet wet. As such I have a single form I thought would be a good dry run to learn a bit.

      The form has 53 drop down comboboxes. Most of the combos have under 10 values, and all the values for all the combos are stored in 1 table (Oracle) differentiated by a "CODE".

      So, for example, one combo might be Gender, and in the database it looks like:

      [code]
      CODE VALUE
      GENDER Male
      GENDER Female
      etc.
      [/code]

      I have a database procedure that kicks out XML for the databind - that works fine it just accepts "CODE" as a param...and I have some Flex code to use an <mx:HTTPService> plus a "result event" to (databind) populate the combos, but it would require me to have separate HTTPService definitions/functions for each CODE/Combo that I need to populate!

      I know there's gotta be an easier+dynamic way...any ideas?
        • 1. Re: Populate Many Combos
          Gregory Lafrance Level 6
          If you can make your server side return one big XML file, and if you have stuctured the XML appropriately, you should be able to have one HTTPService result handler and use e4x syntax to access the data for the multiple comboboxes. So on the server side you might have multiple database queries, but then use PHP or something to cobble it all together into one big XML object for return.
          • 2. Re: Populate Many Combos
            k.crider Level 1
            I think I see what you're saying...instead of querying for each individual combo box source separately, just query once and get all the data. Then I suppose I need help with the AS code for parsing the xml and populating the combos...

            I've tried this in my HTTPServer result event but it's not giving me any result at all:

            [quote]
            public function httpResult(event:ResultEvent):void {
            result = event.result;
            //Do something with the result.
            //txtOutput.text=String(result);
            var thisColl:XMLListCollection;
            thisColl.source = httpSvc.lastResult.category;
            for each(var item:XMLNode in thisColl) {
            trace(item.toString());
            //Alert.show(item.toString());
            }
            [/quote]

            My XML Looks like this:

            [quote]
            <?xml version="1.0" encoding="iso-8859-1" ?>
            - <root>
            - <category>
            <UZVTREF_CAT>EthnicityCD</UZVTREF_CAT>
            <UZVTREF_CODE>1</UZVTREF_CODE>
            <UZVTREF_DESC>Am. Ind/Alaskan Nat</UZVTREF_DESC>
            <UZVTREF_DEFAULT_IND />
            </category>
            - <category>
            <UZVTREF_CAT>EthnicityCD</UZVTREF_CAT>
            <UZVTREF_CODE>2</UZVTREF_CODE>
            <UZVTREF_DESC>Asian</UZVTREF_DESC>
            <UZVTREF_DEFAULT_IND />
            </category>
            - <category>
            <UZVTREF_CAT>EthnicityCD</UZVTREF_CAT>
            <UZVTREF_CODE>3</UZVTREF_CODE>
            <UZVTREF_DESC>Black or African Am.</UZVTREF_DESC>
            <UZVTREF_DEFAULT_IND />
            </category>
            ........................etc..
            [/quote]

            • 3. Re: Populate Many Combos
              Gregory Lafrance Level 6
              Try this:

              [Bindable] private var allColl:XMLListCollection;

              public function httpResult(event:ResultEvent):void {
              var result:XMLList = event.result..category as XMLList;
              allColl:XMLListCollection = new XMLListCollection(result);
              for each(var item:XML in allColl as XML) {
              trace(item.toXMLString());
              }
              }
              • 4. Re: Populate Many Combos
                k.crider Level 1
                Greg,

                Thanks for the reply, that helps, but when I switched to using an XMLList I could loop through no problem.

                I've got it working to that point but still NOT working... **sigh**

                As soon as I call the next "PopTREF" (see below) function the _previous_ Combobox's data gets wiped out.

                This is so difficult I feel as though I must be missing something conceptually with the way data operations work with Flex. HTTPService+event messes with my head. It would be SO easy in PHP, ASP, ASP.NET...etc...especially using techniques like dynamic SQL I could code this in 10 lines. Just a simple call to a query function to populate a dataset, then use the dataset to populate a dropdown...what am I missing here??!?

                I've got 53 comboboxes... I really don't want to code & maintain 53 HTTPServices, 53 initiators, 53 result handlers and 53 error result handlers....................is that what I'm expected to do in Flex?

                Here's my code I'm using:

                I do appreciate your help by the way...

                ====================================
                private var httpSvc:HTTPService;
                private var cboPop:ComboBox;
                private var category:String;
                private var result:Object;

                private function init() : void {
                // POP THE COMBOS
                //Ethnicity
                cboPop = cboStuEthnicity;
                category="EthnicityCD";
                PopTREF();
                // Status
                cboPop = cboStuStatus;
                category="Enrolled";
                PopTREF();
                Alert.show('Welcome!');
                }

                public function PopTREF():void {
                httpSvc = new HTTPService;
                var params:Object = new Object();
                params.tref_category=category;
                httpSvc.clearResult(false);
                httpSvc.url = " http://mydomain.P_XML_LIST_TREF";
                httpSvc.method = "GET";
                httpSvc.resultFormat="e4x";
                httpSvc.addEventListener("result", httpResult);
                httpSvc.addEventListener("fault", httpFault);
                httpSvc.send(params);
                //Alert.show("Sent!");
                }

                public function httpResult(event:ResultEvent):void {
                result = event.result;
                //Do something with the result.
                var thisXML:XMLList = XMLList(httpSvc.lastResult.category);
                //txtOutput.text=thisXML;
                //Alert.show(thisXML.length());

                for ( var i:Number = 0 ; i < thisXML.length() ; i++ )
                {
                cboPop.dataProvider.addItem({label:thisXML .UZVTREF_DESC.toString(), data: thisXML.UZVTREF_CODE.toString()});

                }
                httpSvc.clearResult(false);
                }

                public function httpFault(event:FaultEvent):void {
                var faultstring:String = event.fault.faultString;
                Alert.show(faultstring);
                }


                Combo box looks like:

                <mx:ComboBox id="cboStuStatus" width="200"></mx:ComboBox>
                • 5. Re: Populate Many Combos
                  Gregory Lafrance Level 6
                  Notice the structure of my data, and how I am assigning to the various ComboBox data providers. You owe me one bud... :-)

                  <?xml version="1.0" encoding="utf-8"?>
                  <topNode>
                  <comboBoxData>
                  <cbxItem>Cake</cbxItem>
                  <cbxItem>Cookies</cbxItem>
                  <cbxItem>Ice Cream</cbxItem>
                  </comboBoxData>
                  <comboBoxData>
                  <cbxItem>Meat</cbxItem>
                  <cbxItem>Fish</cbxItem>
                  <cbxItem>Chicken</cbxItem>
                  </comboBoxData>
                  <comboBoxData>
                  <cbxItem>Water</cbxItem>
                  <cbxItem>Juice</cbxItem>
                  <cbxItem>Milk</cbxItem>
                  </comboBoxData>
                  </topNode>
                  -----------------------------------------------------------------------------
                  <?xml version="1.0"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                  creationComplete="dataRequest.send();">
                  <mx:Script>
                  <![CDATA[
                  import mx.collections.XMLListCollection;
                  import mx.rpc.events.ResultEvent;

                  [Bindable] private var allColl:XMLListCollection;
                  [Bindable] private var cbx1Coll:XMLListCollection;
                  [Bindable] private var cbx2Coll:XMLListCollection;
                  [Bindable] private var cbx3Coll:XMLListCollection;

                  public function httpResult(event:ResultEvent):void {
                  var result:XMLList = event.result..comboBoxData as XMLList;
                  allColl = new XMLListCollection(result);
                  cbx1Coll = new XMLListCollection(allColl.getItemAt(0).cbxItem);
                  cbx2Coll = new XMLListCollection(allColl.getItemAt(1).cbxItem);
                  cbx3Coll = new XMLListCollection(allColl.getItemAt(2).cbxItem);
                  }
                  ]]>
                  </mx:Script>
                  <mx:HTTPService id="dataRequest" resultFormat="e4x" useProxy="false"
                  result="httpResult(event)" url="myXML1.xml"/>
                  <mx:HBox horizontalGap="20">
                  <mx:ComboBox dataProvider="{cbx1Coll}"/>
                  <mx:ComboBox dataProvider="{cbx2Coll}"/>
                  <mx:ComboBox dataProvider="{cbx3Coll}"/>
                  </mx:HBox>
                  </mx:Application>
                  • 6. Re: Populate Many Combos
                    k.crider Level 1

                    Thanks Greg with some modification your code helped a lot. Here's a cookie...