1 Reply Latest reply on Aug 31, 2011 6:10 AM by jcbell3

    Multiple Comboboxes with single instance of Array Collection

    jcbell3

      First off - thanks to everyone for their contributions.  This forum provides a lot of good information.

       

      I'm stuck on a particular problem and I suspect I'm not thinking about the issue correctly.

       

      I'm developing a dynamic module that instantiates components at runtime based on an XML scheme.  Many of these components are drop-downs that are chained togther (a selection in one drives the population of the next)

       

      I loop through the initial XML document to create and layout the components.  I have been using a single Array Collection to populate the drop-down.  This worked fine with the chained comboboxes since I only load one combox initially.  However, when I add a couple of static combo boxes, the arraycollection seemed to be binding to the orginal combobox.  For example, if I create 3 combo boxes they have drop-down values that display all 3 lists appended together.  I was a bit surprised because I didn't think that I could bind an Array Collection.  I certainly haven't defined a Binable ArrayCollection.

       

      I thought I could solve the issue by simply creating a unique Array Collection at runtime - one for each element.  However, I don't think this is possible or I can't figure out how to create a new Array Collection with a dynamic name - something like this:

       

      I was hoping this would work -

       

      var newDropDown_[numOfElement]:ArrayCollection = new ArrayCollection(); 

       

      I've tried to use a Dictionary object to store each ArrayCollection.  I couldn't get that to work properly.

       

      Essentially - my code loops on the number of components to add:

      ...

       

      for (var i:int = 0; i < numFormElements; i++)

                for (var j:int = 0; j < numvalues; j++)
                                  {   
                                     
                                      var valuefield:String = 'value_' + [j+1];
                                      var obj:Object=new Object();
                                      obj.label=instFields[i][valuefield].@label;
                                      obj.data=instFields[i][valuefield].@data;
                                      newDropDown.addItemAt(obj,j);
                                     
                                  }

       

      ....

       

      Then I add the components:

       

                var c:ComboBox = new ComboBox ();
                  c.labelField='label';
                  c.name=numberOfElement.toString();                                  

                  c.dataProvider=    newDropDown;
                  _h1.addChild(c);
                  numComboDropDwns.push(c);

       

      I need a way of looping, getting data and populating each component independently??

       

       

      Any ideas?

       

      Thanks,

       

      John

        • 1. Re: Multiple Comboboxes with single instance of Array Collection
          jcbell3 Level 1

          I went a different route and solved the issue.  For someone looking at a similar problem - this what I ended up doing:

           

          I just loaded one array collection with data for all of the drop-downs on the form.  I then populated each dataprovider with individual elements from the array.

           

          for (var i:int = 0; i < numFormElements; i++)
                      {   

           

          .....

           

          for (var j:int = 0; j < numvalues; j++)
                                      {   
                                          var valuefield:String = 'value_' + [j+1];
                                          var obj:Object=new Object();
                                         
                                          obj.label=instFields[i][valuefield].@label;
                                          obj.data=instFields[i][valuefield].@data;
                                          newDropDown.addItemAt(obj,elementCounter);
                                          elementCounter = elementCounter + 1;
                                      }

          I keep track of the element #, number of values for each element and the overall index for the array.

           

          Each data provider is then loaded with the subset from the array:

           

          for (var z:int = (_counter - _numvalues) ; z < _counter; z++)
                                      {   
                                      var _label:String=    newDropDown.getItemAt(z).label.toString();
                                      var _data:String = newDropDown.getItemAt(z).data.toString();
                                      c.dataProvider.addItem({label:[_label],data:[_data]});
                                     
                                      }

           

          This seems to work nicely - each component is created and loaded with only their data.  I spent 3 days searching for a way to create a dynamic arraycollection at runtime.  I don't think it's possible - but I'm not sure it was the best method anyway.