Toggling sortable columns in a Flex DataGrid control

by Peter deHaan on August 31, 2007

in DataGrid

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.

{ 9 comments… read them below or add one }

1 laoyu September 1, 2007 at 8:02 am

very good!this is very basic sample!

Reply

2 Alberto September 2, 2007 at 3:40 am

bit bug :)

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

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

Reply

3 peterd September 2, 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

Reply

4 Alejandro November 30, 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

Reply

5 fenix April 28, 2008 at 7:23 am

Thanks for interesting clause

Reply

6 Kelvin May 7, 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.

Reply

7 Anonymous March 27, 2009 at 10:19 pm

how to sort the data in the datagrid wnen an xml was passed as swap the header coluimn using the selected item in the combobox

Reply

8 raghava November 26, 2009 at 3:10 am

Hi Peter,

I want to have some more functionality here. If we make the srtableColumns false, still the sortIcon appears in the datagrid. Can please tell me is it possible for me to this.

regards
Raghava

Reply

9 Peter deHaan November 26, 2009 at 6:03 pm

@raghava,

I’m not sure what you’re seeing, but I only get the sort arrow if the Boolean sortable property to true. Are you changing the sortable or sortableColumns property at runtime?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 
    <mx:DataGrid>
        <mx:columns>
            <mx:DataGridColumn dataField="c1" headerText="sortable=true" sortable="true" width="150" />
            <mx:DataGridColumn dataField="c2" headerText="sortable=false" sortable="false" width="150" />
        </mx:columns>
        <mx:dataProvider>
            <mx:ArrayCollection>
                <mx:Object c1="1.One" c2="1.Two" />
                <mx:Object c1="2.One" c2="2.Two" />
                <mx:Object c1="3.One" c2="3.Two" />
                <mx:Object c1="4.One" c2="4.Two" />
                <mx:Object c1="5.One" c2="5.Two" />
                <mx:Object c1="6.One" c2="6.Two" />
                <mx:Object c1="7.One" c2="7.Two" />
                <mx:Object c1="8.One" c2="8.Two" />
                <mx:Object c1="9.One" c2="9.Two" />
            </mx:ArrayCollection>
        </mx:dataProvider>
    </mx:DataGrid>
 
</mx:Application>

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: