23
Aug
07

Determining a DataGridColumn object’s current sort order

OK, I’m convinced there is a better way to do this, but so far this is the only solution I’ve found…
Any other ideas? Leave em in the comments!

Basically the following post shows one possible way of determining the current sort order of a data grid by grabbing the sortDescending property whenever a user presses on a data grid header.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/23/determining-a-datagridcolumn-objects-current-sort-order/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.events.DataGridEvent;

            private function doHeaderRelease(evt:DataGridEvent):void {
                var column:DataGridColumn = DataGridColumn(evt.currentTarget.columns[evt.columnIndex]);
                DataGrid(evt.currentTarget).callLater(onCallLater, [column]);
            }

            private function onCallLater(column:DataGridColumn):void {
                columnSortDescending.text = column.dataField + ".sortDescending: " + column.sortDescending;
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Label id="columnSortDescending" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            rowCount="4"
            headerRelease="doHeaderRelease(event)">
        <mx:columns>
            <mx:DataGridColumn id="col1" dataField="label" />
            <mx:DataGridColumn id="col2" dataField="data" />
        </mx:columns>
        <mx:dataProvider>
            <mx:Array>
                <mx:Object data="one" label="User 1" />
                <mx:Object data="two" label="User 2" />
            </mx:Array>
        </mx:dataProvider>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.


2 Responses to “Determining a DataGridColumn object's current sort order”


  1. 1 Dan Apr 28th, 2008 at 12:46 pm

    I think this would work and you do not have to make a callLater method…..

    private function doHeaderRelease(event:DataGridEvent):void {
    var dg:DataGrid= event.currentTarget as DataGrid;

    trace(”column: ” + String(event.dataField));
    trace(”descending: ” + String(dg.columns[event.columnIndex].sortDescending));
    }

  2. 2 Matt Sep 10th, 2008 at 11:55 am

    Dan -

    Turns out doing it that way cause inaccuracies, for some reason sortDecending will report false too often. Checkout this post:

    http://blog.flexexamples.com/2007/08/31/toggling-sortable-columns-in-a-flex-datagrid-control/#comment-4653

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed