import mx.controls.gridclasses.DataGridColumn;
var my_dp:Array = new Array({name:'Grissom, M.', avg:0.279}, {name:'Bonds,
B.', avg:0.362}, {name:'Cruz, D.', avg:0.292}, {name:'Snow, J.',
avg:0.327});
var dg = this.createClassObject(mx.controls.DataGrid, "my_dg", 999, {dataProvider:my_dp});
dg.setSize(240, 180);
var name_dgc:DataGridColumn = new DataGridColumn("name");
name_dgc.headerText = "Name:";
name_dgc.width = 160;
dg.addColumn(name_dgc);
var avg_dgc:DataGridColumn = new DataGridColumn("avg");
avg_dgc.headerText = "Avg:";
avg_dgc.width = 60;
dg.addColumn(avg_dgc);
//
var myListener:Object = new Object();
myListener.headerRelease = function(evt:Object) {
var sortOrder:String = evt.target.sortDirection;
var sortColumn:String = evt.target.columnNames[evt.columnIndex];
switch (sortColumn) {
case 'name' :
sortArray(my_dp, sortColumn, "TEXTNOCASE", sortOrder);
break;
case 'avg' :
sortArray(my_dp, sortColumn, "NUMERIC", sortOrder);
break;
}
};
my_dg.addEventListener("headerRelease", myListener);
//
function sortArray(my_array:Array, sortColumn:String, sortType:String,
sortOrder:String) {
var sortOptions:Number = 0;
switch (sortType.toUpperCase()) {
case 'NUMERIC' :
sortOptions |= Array.NUMERIC;
break;
case 'TEXTNOCASE' :
sortOptions |= Array.CASEINSENSITIVE;
break;
}
if (sortOrder.toUpperCase() == 'DESC') {
sortOptions |= Array.DESCENDING;
}
my_array.sortOn(sortColumn, sortOptions);
} |