AutoSortDataGrid: A simple extension to the DataGrid Flex component
In response to that, I have created a simple extension to the DataGrid component. It is called AutoSortDataGrid and it adds two properties to DataGrid: sortColumn and sortDirection. These simple additions enable one to specify the initial sort state of a datagrid, and no additional code is required on the user’s part.
Building on the examples used in the previous post, to create a table with the colour data previous used, that sorts in a descending fashion on the red column, use the following code:
<gf:AutoSortDataGrid sortColumn="{red}" sortDescending="true" height="157"> <gf:columns> <mx:DataGridColumn headerText="Colour" dataField="name"/> <mx:DataGridColumn id="red" headerText="Red" dataField="red" sortCompareFunction="sortRed"/> <mx:DataGridColumn headerText="Green" dataField="green"/> <mx:DataGridColumn headerText="Blue" dataField="blue" /> </gf:columns> <gf:dataProvider> <mx:Object name="Red" red="255" green="0" blue="0" /> <mx:Object name="Green" red="0" green="255" blue="0" /> <mx:Object name="Blue" red="0" green="0" blue="255" /> <mx:Object name="Yellow" red="255" green="255" blue="0" /> <mx:Object name="Magenta" red="255" green="0" blue="255" /> <mx:Object name="Cyan" red="0" green="255" blue="255" /> <mx:Object name="White" red="255" green="255" blue="255" /> <mx:Object name="Black" red="0" green="0" blue="0" /> </gf:dataProvider> </gf:AutoSortDataGrid>
The component supports both the data being supplied via the MXML and via ActionScript, and the properties can be both set in the MXML or in ActionScript. The following SWF illustrates this in practice. The first table is pure MXML, the second sets the data via ActionScript and the third sets both the properties and data via ActionScript.
Resources:
The AutoSortDataGrid SWC, source and documentation.
The AutoSortDataGrid SWC, source and documentation, plus the source for the test SWF displayed above.
An on-line copy of the documentation.
Share This Post...
1 comment so far, click here to read it or add another
1 Comment so far
Leave a reply


Hi David,
Nice post. Can you please e-mail when when you get a minute? I’d like to suggest a way you can integrate this with the core Flex 3 documentation.
Best regards,
Randy Nielsen
Flex Documentation Manager
Adobe Systems Incorporated