RendererExample class
/******************************************************************************
RendererExample class
copyright Flashscript.biz: 2010
Document class for the DataGrid example.
This is part of a movie where a DataGrid has a checkbox in a row.
********************************************************************************/
package
{
import flash.display.Sprite;
import flash.display.MovieClip;
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import flash.events.*;
/*
* This class extends an Abstractclass, which we need for communication.
*/
public class RendererExample extends AbstractClass
{
private static var itemArray:Array = new Array ;
public function RendererExample ():void
{
/*
* We add a new DataGrid and set its properties.
*/
var myGrid:DataGrid = new DataGrid ;
myGrid.name = "Datagrid";
myGrid.move (35,80);
myGrid.setSize (350,125);
addChild (myGrid);
/*
* We add 4 columns here and define their properties. The text in the columns
* represents the variables for the array.
*/
var _no:DataGridColumn = new DataGridColumn("no");
var _item:DataGridColumn = new DataGridColumn("item");
var _price:DataGridColumn = new DataGridColumn("price");
var _check:DataGridColumn = new DataGridColumn("data");
/*
* In the _check column we want to load the CheckBox instances. So we create a
* cellrenderer for this column.
*/
_check.cellRenderer = SpriteCellRenderer;
/*
* we add the columns to the grid.
*/
myGrid.addColumn (_no);
myGrid.addColumn (_item);
myGrid.addColumn (_price);
myGrid.addColumn (_check);
/*
* we set heights and widths of the columns and add header text for each column.
*/
myGrid.rowHeight = 25;
_no.width = 50;
_item.width = 100;
_price.width = 75;
_check.width = 125;
_no.headerText = "Num";
_item.headerText = "Item Description";
_price.headerText = "Price";
_check.headerText = "Check to buy";
/*
/ We fill the DataGrid with data. To add the CheckBox we use MyIcon" as
* for the data variable. That will be the data value, which we use to
* identify the library symbol.
*/
var dp:DataProvider = new DataProvider ;
dp.addItem ({no:"1",item:"Apples",price:"$1,50",data:"MyIcon"});
dp.addItem ({no:"2",item:"Oranges",price:"$0.89",data:"MyIcon"});
dp.addItem ({no:"3",item:"Banana",price:"$0.59",data:"MyIcon"});
dp.addItem ({no:"4",item:"Cherries",price:"$2.69",data:"MyIcon"});
myGrid.dataProvider = dp;
myGrid.addEventListener (MouseEvent.CLICK,changeHandler);
}
/*
* This is the listener when the DataGrid rows have been selected.
*/
private function changeHandler (event:Event):void
{
var myGrid:DataGrid = event.currentTarget as DataGrid;
/*
* Since the DataGrid does not only have rows, which have a selectedIndex of
* 0 and > 0, we need to add an if statement here. When the user clicks on other
* parts of the DataGrid the selectedIndex is -1.
*/
if (myGrid.selectedIndex != -1)
{
var _item = myGrid.selectedItem.item;
/*
* The get_Message() was defined in the super class of this class, the AbstractClass.
* If it is null nothing happens.
*/
if (get_Message() != null)
{
/*
* if get_Message() is true (String value), we add the item to the array.
*/
if (get_Message() == "true")
{
itemArray.push ({item:_item,checked:get_Message()});
}
/*
* If get_Message() is "false" we nedd to identify the item in the array.....
*/
for (var i:int = 0; i < itemArray.length; i++)
{
if (get_Message() == "false")
{
/*
* ......and remove the item from the array by slicing the array and joining
* the new array minus the deleted item to a new array.
*/
if (itemArray[i].item == _item)
{
var myArray1:Array = itemArray.slice(0,i);
var myArray2:Array = itemArray.slice(i + 1,itemArray.length);
itemArray = myArray1.concat(myArray2);
}
}
}
/*
* We now need to set the message null.
*/
set_Message (null);
/*************OPTIONAL TRACER********************************/
for (var j:int = 0; j < itemArray.length; j++)
{
trace (itemArray[j].item + " " + itemArray[j].checked);
}
/*************************************************************/
}
}
}
}
}