2 Replies Latest reply on May 25, 2006 3:46 AM by FredFlex

    Multiselection into a  list by coding

    FredFlex
      Hello,

      I have problem to select all items of a list using "selectedItems" property. When I click on Sel All button only one item is selected and the other are deleted from the list.
      Here is the code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx: Application xmlns:mx=" http://www.adobe.com/2006/mxml" height="100%" width="100%">

      <mx: Panel title="Filtre Connexions" width="100%" height="50%">
      <mx:VBox width="100%">
      <mx:List id="lst" width="100%"
      dataProvider="{dp}"
      allowMultipleSelection="true"/>
      <mx:ControlBar>
      <mx:Button label="Sel. All" click="onClic_bp_SelAll(event)"/>
      </mx:ControlBar>
      </mx:VBox>
      </mx: Panel>

      <mx:Script>
      <![CDATA[

      import mx.controls.List;
      [Bindable]
      public var dp: Array = ["ONE","TWO","TREE"];

      private function onClic_bp_SelAll(e : Event):void
      {
      lst.selectedItems = dp;
      }
      ]]>
      </mx:Script>
      </mx: Application>


      I don't understand what is the probem.
      Thank for your support
        • 1. Re: Multiselection into a  list by coding
          ntsiii Level 3
          The problem is that you are assigning the dataProvider itself back to the selectedItems of the same array.

          I am not sure exactly what is happening, but clearly it is a problem.

          One fix would be to copy the dg array:
          lst.selectedItems = dp.slice(); //I tried this it works

          Be careful using this with an array of objects though. As I recall, it doesn't copy the object elements, but puts references to the original objects into the new array. This is not a problem for an a rray of strings or other primitive objects.

          Tracy
          • 2. Re: Multiselection into a  list by coding
            FredFlex Level 1
            Thank you very much, it didn't think to try to duplicate the array because I didn't see why selectedItems would alter the array.
            Now it is working fine.

            For using with object array, it is necessary to use the following code to copy values and not references:

            import flash.utils.ByteArray;

            function clone(source bject):*{
            var myBA:ByteArray = new ByteArray();
            myBA.writeObject(source);
            myBA.position = 0;
            return(myBA.readObject());
            }