DeleteRenderer

Package:
Class:
Inheritance:
biz.flashscript.cellrenderer
public class DeleteRenderer
CheckBox
The DeleteRenderer class is used to add CheckBox instances to a datagrid component. Pressing the checkboxes will delete rows in the datagrid.
Public Properties
Property Defined By
data : Object
Interface method.
ICellRenderer
listData : ListData
Interface method
ICellRenderer
Public Methods
Method Defined By
DeleteRenderer()
Creates a new instance of the DeleteRenderer component.
DeleteRenderer
Property Detail
data property
data:Object [read-write]

Method is Interface method of ICellRenderer.

Implementation

public function get data():Object
public function set data(value:Object):void

listData property
listData:ListData [read-write]

Method is Interface method of ICellRenderer.

Implementation

public function get listData():ListData
public function set listData(value:ListData):void
Constructor Detail
DeleteRenderer ()  Constructor
public function DeleteRenderer()
Intializes a new DeleteRenderer instance.
Examples
Create a fla file and name it GridCheckboxDelete.fla. Place a datagrid anc checkbox component in the library. Create a new Actionscript 3 file and name it GridCheckboxDelete.as. Place the script below in the file.

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.*;
	import biz.flashscript.abstract.AbstractClass;
	import biz.flashscript.icons.CheckBoxIcon;
	import biz.flashscript.cellrenderer.DeleteRenderer;
	/*
	* This class extends an Abstractclass, which we need for communication.
	*/
	public class GridCheckboxDelete extends AbstractClass
	{
		private static var itemArray:Array = new Array  ;

		public function GridCheckboxDelete ():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 = DeleteRenderer;
			/*
			* 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 = "Click to Delete";
			/*
			/ 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.
			*/
			//CheckBoxIcon.myString = "Delete from cart";
			var dp:DataProvider = new DataProvider ();
			dp.addItem ({no:"1",item:"Apples",price:"$1,50",data:"biz.flashscript.icons.CheckBoxIcon"});
			dp.addItem ({no:"2",item:"Oranges",price:"$0.89",data:"biz.flashscript.icons.CheckBoxIcon"});
			dp.addItem ({no:"3",item:"Banana",price:"$0.59",data:"biz.flashscript.icons.CheckBoxIcon"});
			dp.addItem ({no:"4",item:"Cherries",price:"$2.69",data:"biz.flashscript.icons.CheckBoxIcon"});
			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 getMessage() was defined in the super class of this class, the AbstractClass.
				* If it is null nothing happens.
				*/
				if (getMessage () != null)
				{
					/*
					* if getMessage() is true (String value), we add the item to the array.
					*/
					if (getMessage () == "true")
					{
						itemArray.push ({item:_item,checked:getMessage()});
					}
					/*
					* If getMessage() is "false" we need to identify the item in the array.....
					*/
					for (var i:int = 0; i < itemArray.length; i++)
					{

						if (getMessage () == "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.
					*/
					setMessage (null);

					/*************OPTIONAL TRACER********************************/

					for (var j:int = 0; j < itemArray.length; j++)
					{
						trace (itemArray[j].item + "    " + itemArray[j].checked);
					}

					/*************************************************************/

				}
			}
		}
	}
}
Get Adobe Flash player