13 Replies Latest reply on Aug 27, 2008 6:29 AM by Developer504

    Populating Combo Box with external data

    Developer504 Level 1
      I have this combo box:

      <mx:ComboBox change="changeEvt(event)" x="134" y="48">
      <mx:Array>
      <mx:Object label="AM" data="ALL"/>
      <mx:Object label="BM" data="CM"/>
      <mx:Object label="CM" data="DE"/>
      </mx:Array>
      </mx:ComboBox>

      I would like to populate this from an XML file, for example I import this

      <empTypes>
      <laborClass lc=”AM” </>
      <laborClass lc=”BM” </>
      <laborClass lc=”CM” </>
      </empTypes>

      ...into an XMLList, named say "empTypes" what is the syntax I'd have to use to insert this into the comboBox? I'm not sure how to make it fit.

      M.
        • 2. Re: Populating Combo Box with external data
          Level 7

          "Greg Lafrance" <glafrance@chikaradev.com> wrote in message
          news:g7fa3t$3kt$1@forums.macromedia.com...
          > Here is an example:
          >
          > ----- data/data2.xml -----
          > <?xml version="1.0" encoding="utf-8"?>
          > <empTypes>
          > <laborClass lc="AM"/>
          > <laborClass lc="BM"/>
          > <laborClass lc="CM"/>
          > </empTypes>
          >
          > ----- test.mxml -----
          > <?xml version="1.0"?>
          > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          > creationComplete="dataRequest.send()">
          > <mx:Script>
          > <![CDATA[
          > import mx.rpc.events.ResultEvent;
          >
          > [Bindable]
          > private var resultObj:Object = new Object();
          >
          > private function resultHandler(event:ResultEvent):void{
          > resultObj = Object(event.result);
          > }
          > ]]>
          > </mx:Script>
          > <mx:HTTPService id="dataRequest" url="data/data2.xml"
          > result="resultHandler(event)" resultFormat="e4x"/>
          > <mx:ComboBox dataProvider="{resultObj.laborClass}" labelField="@lc"
          > x="134"
          > y="48"/>
          > </mx:Application>


          Or you could use something like this:

          <mx:XML id="theXML" source="data/data2.xml" />
          <mx:ComboBox dataProvider="{theXML}" labelField="@lc" />

          and skip the HTTP stuff.

          HTH;

          Amy


          • 3. Populating Combo Box with external data
            Developer504 Level 1
            quote:

            Originally posted by: Greg Lafrance
            Here is an example:


            Thanks so much, both of you, much appreciated! The part I was missing was the labelfield.

            M.

            • 4. Populating Combo Box with external data
              Developer504 Level 1
              Ok I have another problem here though.

              I got my XML list into the combo box, but now when I select a value from the box, the filter it drives doesn't work any more.
              I don't understand why that would have changed...?

              Here are the relevant functions and vars:



              <mx:ComboBox dataProvider="{lcDs}" labelField="@lc" change="changeEvt(event)" x="134" y="48">
              </mx:ComboBox>

              [Bindable]
              private var xlc:XMLListCollection = new XMLListCollection(euDs);

              xmlListColl.filterFunction = filterEUData;

              <mx:XMLListCollection id="xmlListColl" source="{euDs}">
              <mx:sort>
              <mx:Sort>
              <mx:fields>
              <mx:SortField id="sortField" name="@code" caseInsensitive="true"
              />
              </mx:fields>
              </mx:Sort>
              </mx:sort>
              </mx:XMLListCollection>

              private function filterEUData(item:Object):Boolean
              {if (filter1=="ALL")
              {return true;}
              else
              {if(item.@code == filter1)
              {return true;}
              else
              {return false;}
              }
              }

              public function changeEvt (event:Event):void {
              filter1=event.currentTarget.selectedItem.label;
              xmlListColl.refresh();
              }
              • 5. Populating Combo Box with external data
                Developer504 Level 1
                I put the variable 'filter1' on the form, to see what was in it. Initially it has the default value "ALL", but when I pick something from the ComboBox, it changes to a blank value.

                So something has changed in how the final function (changeEvt) works with the new data structure,
                filter1=event.currentTarget.selectedItem.label works when I have my data hard coded, but not now that it's coming from an XMLList. Why is that?

                is it something like using an @ somewhere, or assigning a name (ID) to the control... or what? My hunch is it's got to be something small and syntax related.

                M.
                • 6. Re: Populating Combo Box with external data
                  Developer504 Level 1
                  I changed it to filter1=event.currentTarget.selectedItem.@lc; and that now seems to be working :)
                  • 7. Re: Populating Combo Box with external data
                    Level 7

                    "Developer504" <webforumsuser@macromedia.com> wrote in message
                    news:g7fk28$ed1$1@forums.macromedia.com...
                    > Ok I have another problem here though.
                    >
                    > I got my XML list into the combo box, but now when I select a value from
                    > the
                    > box, the filter doesn't work any more.
                    > I don't understand why that would have changed...?

                    > private function filterEUData(item:Object):Boolean
                    > {if (filter1=="ALL")
                    > {return true;}
                    > else
                    > {if(item.@code == filter1)
                    > {return true;}
                    > else
                    > {return false;}
                    > }
                    > }

                    item.@code is XML
                    filter1 is a string

                    try

                    if (item.attribute('code').toString==filter1){
                    return true;
                    }

                    return false;

                    HTH;

                    Amy


                    • 8. Re: Populating Combo Box with external data
                      newhelper
                      I have a question.
                      If the code is:

                      ----- data/data2.xml -----
                      <?xml version="1.0" encoding="utf-8"?>
                      <empTypes>
                      <laborClass lc="AM" dt="O"/>
                      <laborClass lc="BM" dt="P"/>
                      <laborClass lc="CM" dt="Q"/>
                      </empTypes>

                      <mx:ComboBox id="cmbTest" dataProvider="{lcDs}" labelField="@lc" change="changeEvt(event)" >
                      </mx:ComboBox>
                      <mx:Button label="Search" click="test()"/>

                      //++++++++++++++++
                      public function changeEvt (event:Event):void {
                      Alert.show(event.currentTarget.selectedItem.@dt);
                      }

                      public function test():void {
                      Alert.show(cmbTest.selectedItem.@dt); //??
                      }

                      //++++++++++++++++
                      How to get the dt value from selected item?
                      I tried cmbTest.selectedItem.@dt, but it seems doesn't work.

                      thx!
                      • 9. Re: Populating Combo Box with external data
                        Developer504 Level 1
                        quote:

                        Originally posted by: newhelper
                        I have a question.
                        If the code is:

                        ----- data/data2.xml -----
                        <?xml version="1.0" encoding="utf-8"?>
                        <empTypes>
                        <laborClass lc="AM" dt="O"/>
                        <laborClass lc="BM" dt="P"/>
                        <laborClass lc="CM" dt="Q"/>
                        </empTypes>

                        <mx:ComboBox id="cmbTest" dataProvider="{lcDs}" labelField="@lc" change="changeEvt(event)" >
                        </mx:ComboBox>
                        <mx:Button label="Search" click="test()"/>

                        //++++++++++++++++
                        public function changeEvt (event:Event):void {
                        Alert.show(event.currentTarget.selectedItem.@dt);
                        }

                        public function test():void {
                        Alert.show(cmbTest.selectedItem.@dt); //??
                        }

                        //++++++++++++++++
                        How to get the dt value from selected item?
                        I tried cmbTest.selectedItem.@dt, but it seems doesn't work.

                        thx!


                        I'm not sure what you are doing wrong. Do you have your data bound into an XMLlist?

                        M.
                        • 10. Populating Combo Box with external data
                          newhelper Level 1
                          My httpservice function are as follows:

                          //+++++++++++++++++++++
                          <mx:HTTPService id="srvTest" url=" http://localhost:8080/data/data2.xml"
                          method="get" requestTimeout="9" resultFormat="e4x"
                          result="httpHandle(event);" fault="errorHandle(event)" >
                          </mx:HTTPService>

                          <mx:ComboBox id="cmbTest" labelField="@lc" change="changeEvt(event)" creationComplete="srvTest.send()" />

                          <mx:Button label="Search" click="test()"/>
                          //+++++++++++++++++++++
                          private function httpHandle(e:ResultEvent):void {
                          var xmlResult:XML = XML(e.result);
                          cmbTest.dataProvider = xmlResult.laborClass;
                          }
                          public function changeEvt (event:Event):void {
                          Alert.show(event.currentTarget.selectedItem.@dt);
                          }

                          public function test():void {
                          Alert.show(cmbTest.selectedItem.@dt); //??
                          }

                          //++++++++++++++
                          ----- data/data2.xml -----
                          <?xml version="1.0" encoding="utf-8"?>
                          <empTypes>
                          <laborClass lc="AM" dt="O"/>
                          <laborClass lc="BM" dt="P"/>
                          <laborClass lc="CM" dt="Q"/>
                          </empTypes>

                          I tried to debug the event.currentTarget.selectedItem, the value is rignt, such as <laborClass lc="BM" dt="P"/>, so it can get the @dt value.
                          But the cmbTest.selectedItem is an Object, it cannot get the @dt value. And I also tried cmbTest.data, it seems does not work. Why? And how to get the dt value?
                          • 11. Populating Combo Box with external data
                            Developer504 Level 1
                            I'm very new to Flex myself, so take this with a grain of salt, but I think you need to use an XMLList rather than an XML.

                            My handlers look like this:

                            private function handleXmlDataCE(event:ResultEvent):void
                            {ceDs=XMLList(event.result.Expense);
                            }


                            with ceDs in this case defined as an XMLList.

                            M.
                            • 13. Re: Populating Combo Box with external data
                              Developer504 Level 1
                              Did that fix the problem?