Common Date Field Sorting for multiple columns in DataGrid, Flex

Hi All,

It is very easy to attach a common sorting function in data grid on the multiple String date field columns or any other type columns(multiple),

Below I am giving en example on attaching common sorting function on date field.

My example contains a data grid which is having two columns with String date. And for that I have written  a generic function which read the header text of the columns and attach sorting function if the header text contains “date” text.

public static function commonSortFunction(myGridObj:DataGrid):void{
try{
if(myGridObj!=null && myGridObj.dataProvider.length > 0){
for(var j:int = 0; j< myGridObj.columnCount; j++){
if((myGridObj.columns[j] as DataGridColumn).visible){
var columnDG:DataGridColumn=(myGridObj.columns[j] as DataGridColumn);
if(columnDG.headerText.toLowerCase().indexOf(“date”)!=-1 ){ // if date text found in column, attach the common method
columnDG.sortCompareFunction=sortDate(columnDG.fieldName+””);  // pass the field name, so that on fly t will pick which field to sort

}
}
}
}
}catch(e:Error){
trace(“Error Message “:+e”);
}
}

private static function sortDate(fieldName:String):Function{
return function (obj1:Object, obj2:Object):int {
var datA:Date = new Date(Date.parse(obj1[fieldName]));
var datB:Date = new Date(Date.parse(obj2[fieldName]));
return ObjectUtil.dateCompare(datA, datB);
}
}

Thanks

R Vashi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s