After coming across the difficulties surrounding the current sortCompareFunction for the DataGrid, I've come up with a solution that worked to solve my problem, and I thought might help others.
My particular issue was that I had two columns in my DataGrid which were dynamically set (that is, the dataField value [a month] was dynamically set based on a ComboBox setting). These two columns would display data allowing a user to compare two months of data. A third column used an itemRenderer to display a percentage value which represented the difference between the first month's column value, and the second month's column value. (see attached)
Because the information from the 3rd column was being calculated, when I went to implement sorting for it, I had to look to the sortCompareFunction. My solution is as follows:
I created a singleton class to hold my sort functionality, as well as a few custom properties that I set on headerRelease of the dataGrid.
MyApp.mxml (excerpt)
-----------------------
import helpers.MySingletonSortClass;
<mx:DataGrid headerRelease="setSortValues()">
<mx:DataGridColumn dataField="{month1Combo.selectedItem.data}" />
<mx:DataGridColumn dataField="{month2Combo.selectedItem.data}" />
<mx:DataGridColumn itemRenderer="MyPercentageRendererClass" sortCompareFunction="MySingletonSortClass.sortDifference" />
</mx:DataGrid>
private function setSortValues(event:Event):void
{
MySingletonSortClass.sortColumn1 = event.target.columns[0].dataField;
MySingletonSortClass.sortColumn2 = event.target.columsn[1].dataField;
}
MySingletonSortClass.as
-------------------------------
package helpers
{
public class MySingletonSortClass
{
private static var _sortInstance:MySingletonSortClass;
public static var sortColumn1:String;
public static var sortColumn2:String;
public function MySingletonSortClass()
{
if (DataGridSortFunctions._sortInstance != null)
{
throw new Error( "Only one please" );
}
}
public static function getInstance():MySingletonSortClass
{
if (_sortInstance == null)
{
_sortInstance = new MySingletonSortClass();
}
return _sortInstance;
}
//USAGE FUNCTIONALITY
public static function sortDifference(obj1:Object, obj2:Object):Number
{
//I'm now able to access my dataGrid values through obj1[sortColumn1], etc...
//do your sort comparison functionality here and return the usual -1, 0, 1
}
}
}
Because the headerRelease event gets fired before the custom sort happens, I'm able to set the properties inside the singleton class before the sort functionality takes place. Solved my problems, and I hope this idea helps others.
Brendan
__._,_.___
--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
SPONSORED LINKS
| Web site design development | Computer software development | Software design and development |
| Macromedia flex | Software development best practice |
YAHOO! GROUPS LINKS
- Visit your group "flexcoders" on the web.
- To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
- Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
<<attachment: sortCompare1.jpg>>

