31
Aug
07

Toggling sortable columns in a Flex DataGrid control

The following example demonstrates how you can enable/disable sortable columns in a Flex DataGrid control using the DataGrid class’s sortableColumns property, as well as toggling specific column’s sortability using the DataGridColumn class’s sortable property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/31/toggling-sortable-columns-in-a-flex-datagrid-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="User 1" data="1" />
        <mx:Object label="User 2" data="2" />
        <mx:Object label="User 3" data="3" />
        <mx:Object label="User 4" data="4" />
        <mx:Object label="User 5" data="5" />
        <mx:Object label="User 6" data="6" />
        <mx:Object label="User 7" data="7" />
        <mx:Object label="User 8" data="8" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="sortableColumnsCh"
                label="sortableColumns"
                selected="true" />

        <mx:CheckBox id="labelSortableCh"
                label="label.sortable"
                selected="true" />

        <mx:CheckBox id="dataSortableCh"
                label="data.sortable"
                selected="true" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            sortableColumns="{sortableColumnsCh.selected}"
            dataProvider="{arr}">
        <mx:columns>
            <mx:DataGridColumn dataField="label"
                    sortable="{labelSortableCh.selected}" />
            <mx:DataGridColumn dataField="data"
                    sortable="{dataSortableCh.selected}" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

If you want to detect when a data grid column header has been clicked, you can listen for the headerRelease event on the DataGrid control, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/30/toggling-draggable-columns-in-a-flex-datagrid-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

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

            private function dataGrid_headerRelease(evt:DataGridEvent):void {
                var dg:DataGrid = DataGrid(evt.currentTarget);
                var column:DataGridColumn = dg.columns[evt.columnIndex];
                debug.text += column.dataField + " (sortDescending:" + column.sortDescending + ")" + "\n";
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="User 1" data="1" />
        <mx:Object label="User 2" data="2" />
        <mx:Object label="User 3" data="3" />
        <mx:Object label="User 4" data="4" />
        <mx:Object label="User 5" data="5" />
        <mx:Object label="User 6" data="6" />
        <mx:Object label="User 7" data="7" />
        <mx:Object label="User 8" data="8" />
    </mx:Array>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arr}"
            headerRelease="dataGrid_headerRelease(event)">
        <mx:columns>
            <mx:DataGridColumn dataField="label" />
            <mx:DataGridColumn dataField="data" />
        </mx:columns>
    </mx:DataGrid>

    <mx:TextArea id="debug"
            width="{dataGrid.width}"
            height="{dataGrid.height}" />

</mx:Application>

View source is enabled in the following example.


6 Responses to “Toggling sortable columns in a Flex DataGrid control”


  1. 1 laoyu Sep 1st, 2007 at 8:02 am

    very good!this is very basic sample!

  2. 2 Alberto Sep 2nd, 2007 at 3:40 am

    bit bug :)

    debug.text = column.dataField ” (sortDescending:” column.sortDescending “)” “n”;

    At end, replace “n”; by “\n”;

  3. 3 peterd Sep 2nd, 2007 at 10:15 am

    Alberto,

    Thanks for the heads up. The code actually has the ‘\n’ if you download source, but something in the HTML or WordPress template always eats my single backslashes. I doubled up the backslash so now it should appear properly above.

    Peter

  4. 4 Alejandro Nov 30th, 2007 at 5:07 am

    Hi guys, someone knows why when i start clicking the datagrid header column, i receive two events with the same order? example:

    label (sortDescending:false)
    label (sortDescending:false)
    label (sortDescending:true)

    After the 3ยบ click, the order ascending/descending starts to work correctly.

    Is it a bug?
    thx in advance

  5. 5 fenix Apr 28th, 2008 at 7:23 am

    Thanks for interesting clause

  6. 6 Kelvin May 7th, 2008 at 7:04 am

    Hi All.
    Just incase, you come across this sample, you will find that Alejandro is talking the truth. For whatever reason the sortDescending property does not accurately reflect the sort order. For a good sample that works and shows the correct sorting order, take a look at http://blog.flexexamples.com/2007/08/23/determining-a-datagridcolumn-objects-current-sort-order/#more-117

    It works as expected.

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;".