ActionScript 3: Detecting Click Events on Individual Cells in a Datagrid
A user named “betamakz” on the Kirupa forum has posted a very useful example of how to detect for click events on individual cells in a data grid. I’m going to repost here, just to make it easier for myself to find again.
import fl.data.DataProvider;
import fl.controls.DataGrid;
import fl.events.ListEvent;
// DUMMY DATA
var dp:DataProvider = new DataProvider();
for(var i:uint=0; i<10; i++) {
dp.addItem( {id:i, title:"Title"+i, price:"$"+i } );
}
// CREATE DATAGRID
var dg:DataGrid = new DataGrid();
dg.dataProvider = dp;
dg.setSize(300,200);
addChild(dg);
// EVENT LISTENER
dg.addEventListener(ListEvent.ITEM_CLICK, gridItemSelected);
// EVENT HANDLER
function gridItemSelected(e:ListEvent):void {
trace( "Row Index: " + e.rowIndex);
trace( "Column Index: " + e.columnIndex);
var colName:String = e.target.columns[e.columnIndex].dataField;
trace( "Column Field Name: " +colName);
trace( "Cell data: " +e.item[colName]);
}
0 Comments:
Posta un commento
<< Home