Resetting a sort on a DataGrid control in Flex

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.

2 thoughts on “Resetting a sort on a DataGrid control in Flex

Comments are closed.