views:

2730

answers:

2

I have button like random click ,if i click this button then will select 15 checkbox within datagrid itemrenderer checkbox But i can't access the checkbox ? please refer me ? How can access the checkbox ?

<mx:DataGridColumn headerText="1"  rendererIsEditor="true" editorDataField="selected">
     <mx:itemRenderer> 
<mx:Component> 
<mx:CheckBox textAlign="center" click="{data.check1 = (data.check1 != 'true') ? 'true' : 'false'};outerDocument.toggleCheckbox(data);outerDocument.calcValues();" dataChange="this.selected=outerDocument.validateCheckbox(data)" themeColor="#ffff00" fillAlphas="[1, 1, 0.75, 0.65]" fillColors="[#f7f7d3, #ffffff, #ff9900, #ffff00]" /> 
     </mx:Component>
     </mx:itemRenderer>
     </mx:DataGridColumn>
A: 

Call outerDocument.someMethod(this) from the check box's change event handler. This way, the someMethod(checkBox:CheckBox) can access the check box.

It would be easier to read your code if you break it to different lines rather than hiding everything at the far right end.

UPDATE:

<!-- this is your DataGridColumn's itemRenderer -->
<mx:itemRenderer>  
  <mx:Component>
    <mx:CheckBox textAlign="center" selected="{data.checked}"/>
  <mx:Component>
<mx:itemRenderer>

Add this to the random button's click handler:

var selectedNumbers:Array = [];
var randomNumber:Number;
for(i = 0; i < 15; i++)
{
  do
    randomNumber = Math.floor(Math.random() * 40);
  while(selectedNumbers.indexOf(randomNumber) != -1);
  //dp is the data provider of the data grid
  dp.getItemAt(randomNumber).checked = true;
  selectedNumbers.push(randomNumber);
}
Amarghosh
ok thank you for your value infromation Amarghosh .But i want How can i select 15 checkbox when the button(btn Name is Random)click .
R.Vijayakumar
updated the post to select 15 random check boxes in the data grid. btw, why do you wanna do this?
Amarghosh
user want to select randomly 15 checkbox so i add button this button .i tried but shows error like checked not defult value so i add //calamount is datagrid namefor each (var item:Object in calamount.dataProvider) {item.checked = true;} if i run i gotdisplay tag like <checked>true</checked>
R.Vijayakumar
multiple lines of code in a comment is hard to read. Updating the question with this code would make it easier.
Amarghosh
so after that i add like for(i = 0; i < 15; i++) { calamount.dataProvider[i].check1=true; }what can i do it ? Why Why cant select checkbox in datagrid ?
R.Vijayakumar
Make sure that the `check1` property is `[Bindable]`. Then the check box will be selected automatically.
Amarghosh
NO . How can i set check1 property is [Bindable] .here i used<mx:CheckBox click="{data.check1 = (data.check1 != 'true') ? 'true' : 'false'};outerDocument.toggleCheckbox(data);outerDocument.calcValues();" dataChange="this.selected=outerDocument.validateCheckbox(data)"()
R.Vijayakumar
+2  A: 

Check out this code:
Data.as

package
{
    public class Data
    {
     [Bindable]
     public var name:String;

     [Bindable]
     public var checked:Boolean;
     public function Data(name:String, checked:Boolean)
     {
      this.name = name;
      this.checked = checked;
     }
    }
}

Test.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
    xmlns:local="*" creationComplete="create();">
    <mx:Button label="Randomize" click="randomize()"/>
    <mx:Button label="Trace selected indices" click="traceValues()"/>
    <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" 
     dataProvider="{dp}">
     <mx:columns>
      <mx:DataGridColumn dataField="name" headerText="Name"/>
      <mx:DataGridColumn headerText="Checked" dataField="checked">
        <mx:itemRenderer>
          <mx:Component>
            <mx:CheckBox selected="{data.checked}" 
              change="{data.checked = this.selected;}/>
          </mx:Component>
        </mx:itemRenderer>
      </mx:DataGridColumn>
     </mx:columns>
    </mx:DataGrid>
    <mx:Script>
     <![CDATA[
      import mx.collections.ArrayCollection;
      [Bindable]
      public var dp:ArrayCollection;
      public function create():void
      {
       var array:Array = [];
       for(var i:Number = 0; i < 20; i++)
        array.push(new Data("The Name", false));
       dp = new ArrayCollection(array);
      }
      private function randomize():void
      {
        var selectedNumbers:Array = [];
        var randomNumber:Number;
        for(var i:Number = 0; i < 5; i++)
        {
          do
            randomNumber = Math.floor(Math.random() * 20);
          while(selectedNumbers.indexOf(randomNumber) != -1);
          dp.getItemAt(randomNumber).checked = !dp.getItemAt(randomNumber).checked;
          selectedNumbers.push(randomNumber);
        }
      }
      private function traceValues():void
      {
        for(var i:Number = 0; i < 20; i++)
        {
         if(dp.getItemAt(i).checked)
           trace(i);
        }
      }
     ]]>
    </mx:Script>
</mx:Application>
Amarghosh
Amarghosh thank you thank you so . i am working lot of time for this but you can do within 10 min . What!!!! a talent you are .
R.Vijayakumar