The following example shows how you can reset a sort on a Flex DataGrid control by setting the ArrayCollection data provider’s sort property to null and refreshing the array collection using the refresh() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/08/resetting-a-sort-on-a-datagrid-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.collections.Sort;

            private function refreshSort():Boolean {
                arrColl.sort = null;
                return arrColl.refresh();
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl">
        <mx:source>
            <mx:Array>
                <mx:Object en="One" fr="Un" es="Uno" />
                <mx:Object en="Two" fr="Deux" es="Dos" />
                <mx:Object en="Three" fr="Trois" es="Tres" />
                <mx:Object en="Four" fr="Quatre" es="Cuatro" />
                <mx:Object en="Five" fr="Cinq" es="Cinco" />
                <mx:Object en="Six" fr="Six" es="Seis" />
                <mx:Object en="Seven" fr="Sept" es="Siete" />
                <mx:Object en="Eight" fr="Huit" es="Ocho" />
                <mx:Object en="Nine" fr="Neuf" es="Nueve" />
                <mx:Object en="Ten" fr="Dix" es="Diez" />
            </mx:Array>
        </mx:source>
    </mx:ArrayCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Reset sort"
                click="refreshSort();" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            rowCount="6"
            verticalScrollPolicy="on">
        <mx:columns>
            <mx:DataGridColumn dataField="en"
                    headerText="English:" />
            <mx:DataGridColumn dataField="fr"
                    headerText="French:" />
            <mx:DataGridColumn dataField="es"
                    headerText="Spanish:" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Resetting a sort on a DataGrid control in Flex

  1. Daniel Neri says:

    This is really great!

    Question, if the dataProvider was an Object(), then how would you do the reset?!

    Thanks!

  2. tom Dwan says:

    It works even though it is an object().

Leave a Reply

Your email address will not be published.

You may 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