PDA

View Full Version : Problem with CheckBoxes Array Created by Checking Boxes


ajwei810192
02-20-2009, 04:05 PM
Hi,

I thought this problem is solved, but it seems to me that the string is continuously being recreated, which causes a problem that I have to removeAll of my DataProvider whenever I check a checkBox. What I am hoping to accomplish here is to remove the checkBox, have the program search through which ones are unchecked, and remove only the "affected" elements without reconstructing.

I used trace to track, and that is when I realized that the string is continuously being recreated, which is not what I want, because I am trying to allow users edit the dataGrid or other things on the screen as they go.



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="create_table()">

<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.*;
import mx.controls.dataGridClasses.DataGridColumn;
[Bindable]private var checkBoxLength:uint = 0;
[Bindable]private var checkBoxSelected:uint = 0;
[Bindable]private var checkBox2Length:uint = 0;
[Bindable]private var checkBox2Selected:uint = 0;
[Bindable]private var myDataGrid:DataGrid;
[Bindable]private var color:String;
[Bindable]private var number:String;
[Bindable]private var selArr3:Array;
[Bindable]private var dgHeaderStyles:CSSStyleDeclaration;
private function changeHandler(evt:Event):void {

var message:String = new String();

for each(var obj:CheckBox in checkBox){
checkBoxLength++;
if(obj.selected){
checkBoxSelected++;
for each(var obj2:CheckBox in checkBox2){
checkBox2Length++;
if(obj2.selected){
checkBox2Selected++;
message += obj.label + "\t" + obj2.label + "\n";
selArr3= message.split("\n");
select_array();

}
}
}
}
var title:String = checkBoxSelected + " of " + checkBoxLength + " item(s) selected. "
+ checkBox2Selected + " of " + checkBox2Length + " item(s) selected.";
Alert.show(message, title);

}
private function create_table():void{

myDataGrid = new DataGrid();
myDataGrid.percentWidth = 85;
myDataGrid.percentHeight = 85;
myDataGrid.dataProvider = selArr3;
myDataGrid.editable= true;
myDataGrid.setStyle("headerStyleName", "myHeaderStyles");
var cols:Array = myDataGrid.columns;

dgHeaderStyles = new CSSStyleDeclaration(".myHeaderStyles");
dgHeaderStyles.setStyle("color", "red");
dgHeaderStyles.setStyle("fontWeight", "bold");
dgHeaderStyles.setStyle("fontSize", "8");

var col1:DataGridColumn = new DataGridColumn("color");
col1.width= 45;
col1.editable= false;
col1.headerText = "Color";

var col2:DataGridColumn = new DataGridColumn("number");
col2.width= 35;
col2.editable= false;
col2.headerText = "Number";

myDataGrid.columns = [col1, col2]; //Displays only selected column
addChild(myDataGrid);

}

private function select_array():void{
myDataGrid.dataProvider.removeAll();
var len:int= selArr3.length -1;
for (var i:int=0;i<len;i++) {
var test:String= selArr3[i];
var test2:Array= test.split("\t");

color= test2[0];
number= test2[1];

trace ("Color: " + color);
trace("Number: " + number);

myDataGrid.dataProvider.addItem(
{
"color": color,
"number": number

}
);
}


}


]]>
</mx:Script>
<mx:Array id="arr">
<mx:Object label="Red" data="red" />
<mx:Object label="Orange" data="haloOrange" />
<mx:Object label="Yellow" data="yellow" />
<mx:Object label="Green" data="haloGreen" />
<mx:Object label="Blue" data="haloBlue" />
</mx:Array>
<mx:Array id="arr2">
<mx:Object label="1" data="1" />
<mx:Object label="2" data="2" />
<mx:Object label="3" data="3" />
<mx:Object label="4" data="4" />
<mx:Object label="5" data="5" />
</mx:Array>
<mx:TabNavigator id="tn2" width="200" height="405" x="873">
<mx:Canvas id="test1" width="185" y="34.95" label="Test">
<mx:VBox>
<mx:Repeater id="checkBoxRepeater" dataProvider="{arr}">
<mx:CheckBox id="checkBox"
label="{checkBoxRepeater.currentItem.label}"
data="{checkBoxRepeater.currentItem.data}"
change="changeHandler(event);" />
</mx:Repeater>
</mx:VBox>
</mx:Canvas>
<mx:Canvas id="test2" width="185" y="34.95" label="Test2">
<mx:VBox>
<mx:Repeater id="checkBoxRepeater2" dataProvider="{arr2}">
<mx:CheckBox id="checkBox2"
label="{checkBoxRepeater2.currentItem.label}"
data="{checkBoxRepeater2.currentItem.data}"
change="changeHandler(event);" />
</mx:Repeater>
</mx:VBox>
</mx:Canvas>
</mx:TabNavigator>
</mx:Application>

Am I taking the wrong approach here?

Alice