0 Replies Latest reply on Jun 5, 2010 8:52 AM by janhouse00

    Select radio button by default in datagrid

    janhouse00

      Hiya,

       

      I am trying to make the radio button in the first row of my datagird auto-selected by default. However, it shows as selected and unselected alternatively, whenever I click the "add RB" button. What could have I done wrong? Many thanks.

       

      Main.mxml

      ========

      <?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">
          <fx:Script>
          <![CDATA[
              [Bindable]
              private var myDP:Array = new Array();
              private var selected:Number = -1;
              private var counter:Number = 0;
                     
              private function addRadioButton():void {
                  var isSelected:Boolean;
                  if (selected == -1) {
                      isSelected = true;
                      selected = myDP.length;
                  } else {
                      isSelected = false;
                  }
                  myDP.push({
                      isSelected: isSelected,
                      no: "Radio Button No: " + (++counter)
                  });
                  myDG.dataProvider = myDP;
              }
          ]]>
          </fx:Script>
          <s:Panel><s:VGroup>
          <mx:DataGrid id="myDG">
              <mx:columns>
                  <mx:DataGridColumn dataField="isSelected" width="100" itemRenderer="myRadioButton"/>
                  <mx:DataGridColumn dataField="no" width="200"/>
              </mx:columns>
          </mx:DataGrid>
          <mx:ControlBar>
              <s:Button id="btnAdd" label="Add RB" click="addRadioButton()"/>
          </mx:ControlBar>
          </s:VGroup></s:Panel>
      </s:Application>

       

       

      myRadioButton.as

      =============

      package 
      {
          import mx.controls.*;
          public class myRadioButton extends RadioButton
          {
              public function myRadioButton()  { super(); } 

              override public function set data(value:Object):void {
                  super.data = value;
                  if (value.isSelected == true) {
                      this.selected = true;
                  } else {
                      this.selected = false;
                  }
                  invalidateDisplayList();
              }
          }
      }