Default Sorting for a Flex DataGrid
Friday, June 15th, 2007While working on the DeveloperCircuit Flex widget, I had the need to set a default sorting for a Flex DataGrid control. The standard control does not provide a mechanism to do this.
The Flex documentation suggests that you sort the underlying dataset, however I felt that this was very unsatisfactory. When you manually sort a DataGrid, the column which is being used to sort the data and the order of the sort, ascending or descending, is shown by the way of little black arrow in the column header. I felt that the default sorting should also be communicated to the user by this mechanism.
After some experimentation, this is the solution I came up with. There is a DataGrid event which is called when the user sorts a column. By announcing this event manually, once the underling data set has been returned by a call to a back end server, one can simulate the user action and set the default sorting of the DataGrid, complete with arrow. Example below:
<mx:RemoteObject id="someService" destination="ColdFusion" source="{this.someServiceLocation}" showBusyCursor="false" result="this.someDataGrid.dispatchEvent ( new DataGridEvent ( DataGridEvent.HEADER_RELEASE, false, true, 0, // The zero-based index of the column to sort in the DataGrid object's columns array. null, 0, null, null, 0 ) );" />
