RR_QQ
03-11-2008, 07:53 PM
Here is the way to create a List with checkboxes in its rows entire in AS3 WITHOUT using MXML:
the List instantiation and itemrenderer setting:
var newList1:List = new List();
newList1.id = "newList1";
newList1.dataProvider = phpData;
newList1.allowMultipleSelection = true;
newList1.width = 200;
newList1.height = 400;
newList1.x = 10;
newList1.y = 20;
newList1.itemRenderer = new ClassFactory(CheckBoxRenderer);
newList1.setStyle("alternatingItemColors", new Array("#EEEEEE", "white"));
myCanvas.addChild(newList1);
the ItemRenderer object code:
// CheckBoxRenderer.as
package FormComponents
{
import mx.controls.CheckBox;
import flash.events.Event;
public class CheckBoxRenderer extends CheckBox
{
public function CheckBoxRenderer(){
super();
// => Add listener to detect change in selected
this.addEventListener(Event.CHANGE, onChangeHandler);
}
// Override the set method for the data property.
override public function set data(value:Object):void {
super.data = value;
// => Make sure there is data
if (value != null) {
// => Set the label
this.label = value.label;
// => Set the selected property
this.selected = value.isSelected;
}
// => Invalidate display list,
// => If checkbox is now selected, we need to redraw
super.invalidateDisplayList();
}
// => Handle selection change
private function onChangeHandler(event:Event):void
{
super.data.isSelected = !super.data.isSelected;
}
}
}
I had a hell of time trying to find out how to do it so hopefully this will save others a lot of time searching
the List instantiation and itemrenderer setting:
var newList1:List = new List();
newList1.id = "newList1";
newList1.dataProvider = phpData;
newList1.allowMultipleSelection = true;
newList1.width = 200;
newList1.height = 400;
newList1.x = 10;
newList1.y = 20;
newList1.itemRenderer = new ClassFactory(CheckBoxRenderer);
newList1.setStyle("alternatingItemColors", new Array("#EEEEEE", "white"));
myCanvas.addChild(newList1);
the ItemRenderer object code:
// CheckBoxRenderer.as
package FormComponents
{
import mx.controls.CheckBox;
import flash.events.Event;
public class CheckBoxRenderer extends CheckBox
{
public function CheckBoxRenderer(){
super();
// => Add listener to detect change in selected
this.addEventListener(Event.CHANGE, onChangeHandler);
}
// Override the set method for the data property.
override public function set data(value:Object):void {
super.data = value;
// => Make sure there is data
if (value != null) {
// => Set the label
this.label = value.label;
// => Set the selected property
this.selected = value.isSelected;
}
// => Invalidate display list,
// => If checkbox is now selected, we need to redraw
super.invalidateDisplayList();
}
// => Handle selection change
private function onChangeHandler(event:Event):void
{
super.data.isSelected = !super.data.isSelected;
}
}
}
I had a hell of time trying to find out how to do it so hopefully this will save others a lot of time searching