Skip to content
Apr 2 / Peter deHaan

Determining the current sort column and sort order on a Flash CS3 DataGrid control

Often when building an application you need to determine the current sort column and sort order, since the user can re-sort columns at any time by clicking on a column’s header. The following example shows how to listen for the headerRelease event (DataGridEvent.HEADER_RELEASE) to determine the current sort order after a user clicks on a column header.

The following example creates an ActionScript 3.0 DataGrid component instance in Flash CS3 and traces the current column and sort order when a data grid header is clicked.

Full code after the jump.

/**
 * Requires:
 *   - A DataGrid control in the document library.
 */
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import fl.events.DataGridEvent;
 
var dp:DataProvider = new DataProvider();
dp.addItem({columnA:"Row 1A", columnB:1234.00});
dp.addItem({columnA:"Row 2A", columnB:56.30});
dp.addItem({columnA:"Row 3A", columnB:789.12});
 
var colA:DataGridColumn = new DataGridColumn("columnA");
var colB:DataGridColumn = new DataGridColumn("columnB");
colB.sortOptions = Array.NUMERIC;
 
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(colA);
myDataGrid.addColumn(colB);
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = myDataGrid.length;
myDataGrid.move(10, 10);
myDataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, headerReleaseHandler);
addChild(myDataGrid);
 
function headerReleaseHandler(evt:DataGridEvent):void {
    var dg:DataGrid = evt.currentTarget as DataGrid;
    trace("column: " + String(evt.dataField));
    trace("descending: " + String(dg.sortDescending));
}

3 Comments

leave a comment
  1. Jon / Mar 5 2009

    Hello, I see this does not work if you have the datagrid sortablecolumns set to false. What I want to do is do my own thing when a user clicks a column, not sort. Any ideas?

  2. Pedro / Aug 20 2009

    Hey.. I don’t have that propertied sortDescending.. I’m on flex.. but it’s the same actionscript.. :S

  3. Peter deHaan / Aug 20 2009

    Pedro,

    The Flex components are a completely different code base. There is no sortDescending property in the Flex DataGrid. If you are using an ArrayCollection or XMLListCollection you may be able to query the data provider’s sort for that information.

    Peter

Leave a Comment