4 Replies Latest reply on Nov 3, 2007 10:37 AM by Mark Forsberg

    Binding Array of Objects to Combobox

    Mark Forsberg Level 1
      I have a function that creates an array of objects. When run this with the Alert I get the correct values:
      public function createTimeArray():void{
      var millisecondsPerMinute:int = 1000 * 60;
      var currentDate:Date = new Date();
      var year:int = currentDate.getFullYear();
      var month:int = currentDate.getMonth();
      var day:int = currentDate.getDate();

      var startTimeDate:Date = new Date(year,month,day,5,0);
      var ta:Array = new Array();

      var i:int;
      for(i = 0;i<102;i++){
      var myObj:Object = new Object();
      myObj.label = startTimeDate;
      myObj.data = i+1;

      ta.push(myObj);
      startTimeDate.setTime(startTimeDate.getTime() + (10 * millisecondsPerMinute));
      //Alert.show(ta .label);
      }
      }

      I create a bindable variable:
      [Bindable]
      private var ta:Array;

      Call it in my init():
      createTimeArray();

      And bind it to my combobox:
      <mx:ComboBox x="74" y="70" width="93" id="cb_departureTime" dataProvider="{ta}" labelFunction="convertTime" textAlign="right" labelField="label">

      I get nothing in my combobox. Any ideas?
      Thanks.

      Mark F.
        • 1. Re: Binding Array of Objects to Combobox
          atta707 Level 2
          remove

          var ta:Array = new Array();

          line from your createTimeArray() function and change

          [Bindable]
          private var ta:Array;

          to

          [Bindable]
          private var ta:Array = new Array();


          and it should work. I hope it will be easy to figure out why!


          ATTA
          • 2. Re: Binding Array of Objects to Combobox
            Gregory Lafrance Level 6
            This code seems to work (see trace output) but combo box has same date/time 5 times. Anyone know why?

            <?xml version="1.0"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="createTimeArray()">
            <mx:Script>
            <![CDATA[
            import mx.controls.Alert;

            [Bindable]
            private var ta:Array = new Array();

            public function createTimeArray():void{
            var millisecondsPerMinute:int = 1000 * 60;
            var currentDate:Date = new Date();
            var year:int = currentDate.getFullYear();
            var month:int = currentDate.getMonth();
            var day:int = currentDate.getDate();

            var startTimeDate:Date = new Date(year,month,day,5,0);
            trace("Start: " + startTimeDate);
            var i:int;
            for(i = 0;i<5;i++){
            var myObj:Object = new Object();
            myObj.label = startTimeDate;
            myObj.data = i+1;

            ta.push(myObj);
            startTimeDate.setTime(startTimeDate.getTime() + (10 * millisecondsPerMinute));
            trace("Loop: " + startTimeDate);
            }
            }
            ]]>
            </mx:Script>

            <mx:ComboBox x="74" y="70" width="300" id="cb_departureTime" dataProvider="{ta}"
            textAlign="right" labelField="label"/>

            </mx:Application>
            • 3. Re: Binding Array of Objects to Combobox
              Mark Forsberg Level 1
              Atta,

              The creation of the new array within the function appears to remove the binding during re-instantiation. I was under the impression that once a variable was bound it stayed bound.

              Greg,

              Nice catch. I had only been seeing the Alert.show and I thought that it would display correctly. I see no reason for the error other than a bug in Flex. I entered an item in their bug tracking system.
              • 4. Re: Binding Array of Objects to Combobox
                Mark Forsberg Level 1
                After several hours of trying various methods I've come to the conclusion that the Combobox control cannot handle dates in their native format. Whether in "data" or "label" the combobox would only show the last value of the array.
                What I've done is to convert the data and label fields to strings. They then display correctly. When I pass the data I will try to convert it back to a date object to use it.
                Seems like a bit of a hack.
                If anyone has any other ideas I'm all ears.
                Thanks.

                Mark F.