4 Replies Latest reply on Dec 14, 2007 4:55 AM by Solerous

    Combobox populate from other Combobox

    Solerous
      I would like to use comboboxes to 'drilldown' into a multi-layered data system. So you select options from the first combobox and it fills up the next combobox based on your selection. Here is the script I am using:

      // fill destination combobox based on selection from src combobox
      private function fillComboBox(src:ComboBox, dest:ComboBox) : void {
      var file:String = src.selectedItem.toString();
      var result:XML = new XML("../xml/"+file+".xml");

      dest.dataProvider = result.String;
      }

      It is getting a valid file string. Earlier in the code, I populate the first combobox using an XML object so I don't see why this doesn't work:

      <mx:XML id="sys" source="../xml/systemData.xml"/>

      <mx:ComboBox id="mainSys"
      change="fillComboBox(mainSys, subSys)" dataProvider="{sys.String}" />

      Thanks for any help!
        • 1. Re: Combobox populate from other Combobox
          David_M..
          Hi,

          I am not sure that using XML("../xml/"+file+".xml") allows you to access the content of the XML file.

          I guess you should rather use an HTTPService object and set the URL to "../xml/"+file+".xml". Then you invoke the send() method of the HTTPService and in the result handler you populate your second combo.

          David


          • 2. Re: Combobox populate from other Combobox
            Solerous Level 1
            Thanks for the quick reply David. You are right that the XML doesn't read the file. I tried HTTPService, however and it did not work either:

            var file:String = src.selectedItem.toString();
            var url:HTTPService = new HTTPService("../xml/"+file+".xml");
            url.send();
            dest.dataProvider = url.lastResult.String;

            In debug, url is null so I don't know why that won't work either....
            • 3. Re: Combobox populate from other Combobox
              David_M.. Level 1
              I would have done something like:

              var file:String = src.selectedItem.toString();
              var svc:HTTPService = new HTTPService();
              svc.url = "../xml/"+file+".xml"; You set the url of the service
              svc.addEventListener(ResultEvent.RESULT, myHandler) You add an listener for the result
              url.send();

              and in the handler function you parse the result and update the content of the combobox.

              private function myHandler(event:ResultEvent):void {
              //parse event.result and set your provider
              }

              • 4. Re: Combobox populate from other Combobox
                Solerous Level 1

                David, thanks, that was a good start in the right direction. I ended up doing the following and it worked:

                public function fillComboBox(src:ComboBox, dest:ComboBox) : void {
                var file:String = src.selectedItem.toString();
                xmlLoader = new URLLoader();
                xmlLoader.addEventListener("complete", parseXML);
                xmlLoader.load(new URLRequest("../xml/"+file+".xml"));
                changBox = dest;
                }

                private function parseXML(evt:Event) :void {
                var myXML:XML = new XML(xmlLoader.data);
                changBox.dataProvider = myXML.String;
                }

                I got most of this code from another blog: http://www.colettas.org/?p=109

                Anyway it just seems odd to me that the solution would be so difficult in actionscript when the one line:

                <mx:ComboBox id="mainSys"
                change="fillComboBox(mainSys, subSys)" dataProvider="{sys.String}" />

                does it all in mxml. But I guess I'm still learning. Thanks again.