1 Reply Latest reply on Oct 7, 2007 4:06 AM by chris.huston.t10

    Adding Object to ArrayCollection

    mnarra Level 1
      Hi All,

      I have a ArrayCollection which will be populated via a remote call to java code.

      The Collection is than bound to a ComboBox which is shownup in the panel.

      I Tried to add a object to the Iterated ArrayCollection at 0th Index, so that i could see that objects label as default label in the combo box.

      But when the combo box is rendered, the First Item from the returned List from JavaCode is shown up as default label.

      When seeing all the items present in the Combo Box, the Objects Label which is added at 0th Index is seen, but when rendered it is not taken up as the default one when showing up on the screen.

      <mx:ArrayCollection id="departments"/>

      private function departmentResultHandler(event:ResultEvent):void
      departments = event.result as ArrayCollection;
      var department:Department = new Department();
      department.departmentID = 0;
      department.departmentName = "Department";

      <mx:ComboBox id="add_departmentID" dataProvider="{departments}" labelField="departmentName" textAlign="left" selectedIndex="0"/>

      As i cant upload the screeshot images here, these are the URL's which will show up the images and give a clear idea of what am explaining



      Please suggest where i have gone wrong

        • 1. Re: Adding Object to ArrayCollection
          chris.huston.t10 Level 3
          The problem is timing. When you assign the event.result to departments, the result is bound to the ComboBox and the first item is selected by default. When new items are added to the combo box, flex does not change the selected item to the new item. So, just tell your combo box to select item 0 and it will show Department.

          add_departmentID.selectedIndex = 0;

          You might also want to look into using the prompt property of the Combo Box. The prompt will appear when comboBox.selectedIndex = -1. Unless you need the Department item to be selectable, you could set prompt="Department" and then do:

          private function departmentResultHandler(event:ResultEvent):void
          departments = event.result as ArrayCollection;
          add_departmentID.selectedIndex = -1;