Flex: question about the sortCompareFunction of the DataGridColumn
Is there anyway I could use the same sortCompareFunction for multiple columns instead of adding a function for each column, as I have a datagrid with 50 columns in which the dataField of 40 columns is not string or number, it's an array so the default sorting of the column won't work. And according to adobe help the two parameters of the sortCompareFunction are entire data provider elements and n开发者_如何转开发ot just the data for the item, so this means I have to define 40 sort functions one for each column. Is there any other solution?
Thanks in advance.
You could specify sort function like that:
<mx:DataGridColumn dataField="someFieldName"
sortCompareFunction="{function(ob1:Object, obj2:Object):int{return mySortFunc(obj1, obj2, 'someFieldName');}}"/>
where mySortFunc is implemented like that:
private function mySortFunc(obj1:Object, obj2:Object, fieldName:String):int{
if (obj1.hasOwnProperty(fieldName) && obj2.hasOwnProperty(fieldName)){
//comparison logic ex.
return obj1[fieldName]>obj2[fieldName]?1:(obj1[fieldName]<obj2[fieldName]?-1:0);
}
}
This way you can have one function for all your sorting needs :).
精彩评论