2 Replies Latest reply on Feb 22, 2011 4:32 AM by Michael J Roberts

    how to Bind a collection to a actionscript created checkbox

    flexiblekrish Level 1

      Hi All,

       

               I have created the checkboxs(mulitple) using actionscript.If some thing changes in that Array that checkboxs has not refreshing i.e not binded(because its not dataprovider).

       

      So please look at this code: I have created one button if you clikc am changething the flag which is a data provider for checkbox selection please help me in this

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="600" initialize="addCh(event)">
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
          <fx:Script>
              <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.containers.VBox;
                  import mx.controls.Alert;
                  import mx.controls.CheckBox;
                  import mx.utils.OnDemandEventDispatcher;
                  [Bindable]
                  public var races:ArrayCollection = new ArrayCollection();
                  public var currentIndex:Number = -1;
                  public var race1:ArrayCollection = new ArrayCollection();
                  public var race2:ArrayCollection = new ArrayCollection();
                 
                  public function addCh(e:Event):void{
                     
                      race1.addItem({name:"Race 1 Horse 1", age:'26', race: 1, horse: 1, isPicked: true} );
                      race1.addItem({name:"Race 1 Horse 2", age:'27', race: 1, horse: 2, isPicked: false} );
                      race1.addItem({name:"Race 1 Horse 3", age:'28', race: 1, horse: 3, isPicked: true} );
                      race1.addItem({name:"Race 1 Horse 4", age:'29', race: 1, horse: 4, isPicked: false} );
                      races.addItem(race1);
                     
                 
                      race2.addItem({name:"Race 2 Horse 1", age:'36', race: 2, horse: 1, isPicked: false});
                      race2.addItem({name:"Race 2 Horse 2", age:'37', race: 2, horse: 2, isPicked: false});
                      race2.addItem({name:"Race 2 Horse 3", age:'38', race: 2, horse: 3, isPicked: true});
                      race2.addItem({name:"Race 2 Horse 4", age:'39', race: 2, horse: 4, isPicked: false});
                      races.addItem(race2);
                  }
                 
                  public function addEn():void{
                      for(var i=0; i< races.length; i++){
                          var vbox:VBox = new VBox();
                          vbox.id= "allPickRepeater";
                          for(var j=0; j< races[i].length;j++){
                              var leg:Object = races[i][j];
                              var checkBox:CheckBox = new CheckBox();
                              checkBox.id = 'ch';
                              checkBox.name = i.toString();
                              checkBox.height = 17;
                              checkBox.selected = leg.isPicked;
                              vbox.addChild(checkBox);
                          }
                          pics.addChild(vbox);
                      }
                  }
                  public function changeValue(e:Event):void{
                      race1[1].isPicked = true;
                  }
                 
              ]]>
          </fx:Script>
          <mx:VBox>
             
          <mx:Canvas  x="100" y="20" borderVisible="true" borderColor="red">
               <mx:HBox id="pics" creationComplete="addEn()" backgroundColor="red">
                 </mx:HBox>
           <s:List dataProvider="{race1}" labelField="name" x="40"/>
              </mx:Canvas>
          <s:Button click="changeValue(event)" label="Change Value"/>
              </mx:VBox>
      </s:Application>