3 Replies Latest reply on Apr 27, 2009 7:57 AM by firdosh

    Multiple selection in List control using CheckBox as itemrenderer

    firdosh Level 1

      Hey all,

                  I am trying to get multiple selection working in a list control using the CheckBox as itemrederer but I am unable to get a list of selected indices even though I have multiple check boxes selected

       

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                      layout="vertical">
          <mx:Script>
              <![CDATA[
                  
                  private function onChange():void
                  {
                      trace(chkList.selectedIndices);
                  }
                  
              ]]>
          </mx:Script>
      
      
      <mx:ArrayCollection id="collection">
              <mx:Object label="Test A"/>
              <mx:Object label="Test B"/>
              <mx:Object label="Test C"/>
              <mx:Object label="Test D"/>
              <mx:Object label="Test E"/>
              <mx:Object label="Test F"/>
              <mx:Object label="Test G"/>
          </mx:ArrayCollection>
      <mx:List id="chkList" dataProvider="{collection}" itemRenderer="mx.controls.CheckBox" change="onChange();" allowMultipleSelection="true"/>
      
      </mx:Application>
      

      I always get the last item I clicked

       

      Thanks,

      Firdosh

        • 1. Re: Multiple selection in List control using CheckBox as itemrenderer
          Michael Borbor Level 4

          Hi there, I'll tweak your code a little bit to something like this:

           

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                          layout="vertical">
              <mx:Script>
                  <![CDATA[
                       import mx.events.ListEvent;
                       import mx.controls.CheckBox;
                     
                     [Bindable]
                     private var mySelectedIndexes:ArrayCollection=new ArrayCollection();
                     
                      private function onChange(e:ListEvent):void
                      {
                         if(CheckBox(e.itemRenderer).selected){
                                   mySelectedIndexes.addItem(e.rowIndex);
                                   
                         }else{
                                        mySelectedIndexes.removeItemAt(mySelectedIndexes.getItemIndex(e.rowIndex));     
                         }
                         chkList.selectedIndices=mySelectedIndexes.toArray();
                         
                      }
                     
                  ]]>
              </mx:Script>
           
           
          <mx:ArrayCollection id="collection">
                  <mx:Object label="Test A"/>
                  <mx:Object label="Test B"/>
                  <mx:Object label="Test C"/>
                  <mx:Object label="Test D"/>
                  <mx:Object label="Test E"/>
                  <mx:Object label="Test F"/>
                  <mx:Object label="Test G"/>
              </mx:ArrayCollection>
          <mx:List id="chkList" dataProvider="{collection}" itemRenderer="mx.controls.CheckBox"  itemClick="onChange(event);" allowMultipleSelection="true"/>
           
          </mx:Application>
          
          1 person found this helpful
          • 2. Re: Multiple selection in List control using CheckBox as itemrenderer
            Flex harUI Adobe Employee

            Did you try modifying one of the examples on my blog?

             

            Alex Harui

            Flex SDK Developer

            Adobe Systems Inc.

            Blog: http://blogs.adobe.com/aharui

            • 3. Re: Multiple selection in List control using CheckBox as itemrenderer
              firdosh Level 1

              Thanks Guys, that was definately helpfull