8 Replies Latest reply on Jul 2, 2009 4:52 PM by softsafari

    How to access checkbox in mx:list

    softsafari Level 1

      I'm trying to access the checkboxes in my list

       

      <mx:List itemRenderer="mx.controls.CheckBox" x="0" y="153" id="listVocab" height="297" width="313"></mx:List>

       

      but I can't find a way. This is what I'm doing

       

              for(var i:int = 0 ; i < listVocab.numChildren; i++)
              {

       

       

       

                      if (  I want to access the checkbox here to see if it's checked  then do the below but I don't know how to access the checkboxes??? )

                     {

                      // This gives me access to the text but                 

                     var word:String = listVocab.dataProvider[i].toString() ;
                         
                      }                
               }

        • 1. Re: How to access checkbox in mx:list
          *Prashant Shelke* Level 4

          You need to create one attribute in your objects of dataProvider say selected. Which will bind with <mx:CheckBox selected={data.selected} .. />


          Then in for loop you can check this property to see which checkBoxes are selected or no.


          • 2. Re: How to access checkbox in mx:list
            softsafari Level 1

            Can you elaborate?  This is what I have

             

                 This is the class in which I bind to the words array

             

                public class CustomVocabulary
                {

                    public var sentence:String;
                    public var type:String;
                    public var dbId:String;
                    public var words:Array;

             

            }

             

            This is how I bind

             

            listVocab.dataProvider =  customDictionary.words;

             

            This is my list

             

            <mx:List itemRenderer="mx.controls.CheckBox" x="0" y="153" id="listVocab" height="297" width="313"></mx:List>

             

            Not sure what to do from here

            • 3. Re: How to access checkbox in mx:list
              *Prashant Shelke* Level 4

              <mx:List id="list" dataProvider="{ customDictionary.words }" rowHeight="28">
                  <mx:itemRenderer>
                      <mx:Component>           
                          <mx:CheckBox id="chkBox" label="{ data.sentence }" width="170" click="data.selected = !data.selected"
                              selected="{ data.selected }"/>
                      </mx:Component>
                  </mx:itemRenderer>
              </mx:List>

               

              public class CustomVocabulary
              {
                      public var sentence:String;
                      public var type:String;
                      public var dbId:String;
                      public var words:Array;
                      public var selected:Boolean;
              }

              • 4. Re: How to access checkbox in mx:list
                softsafari Level 1

                I'm getting this error

                 

                ReferenceError: Error #1069: Property selected not found on String and there is no default value.

                • 5. Re: How to access checkbox in mx:list
                  *Prashant Shelke* Level 4

                  Problem is with dataProvider your assigning to dataGrid is of type Array of Strings so thats why its saying selected property is not available on String.


                  Code given
                  above by me will work for arrayCollection having objects of following type instead of just plain strings:


                  public class CustomClass
                  {

                           public var data:String; // here id you can store
                          public var label:String; // here word you can store
                          public var selected:Boolean;
                  }


                  You can make modification to


                  public class CustomVocabulary
                  {
                          public var sentence:String;
                          public var type:String;
                          public var dbId:String;
                          public var words:ArrayCollection; // this will contain objects of type CustomClass.
                  }


                  <mx:List id="list" dataProvider="{ customVocabulary.words }"

                  • 6. Re: How to access checkbox in mx:list
                    softsafari Level 1

                    I can see the checkbox know but does this part change because of the customclass

                     

                     

                    <mx:CheckBox id="chkBox" label="{ data.sentence }" width="170" click="data.selected = !data.selected"
                                    selected="{ data.selected }"/>

                     

                    I'm confused on the  "data" is this the data in CustomClass or something that is a part of List?

                    • 7. Re: How to access checkbox in mx:list
                      softsafari Level 1

                      It works now! Thanks.