Sort a DataGrid : DataGrid « Grid « Flex






Sort a DataGrid

Sort a DataGrid
         
<!--
Code from Flex 4 Documentation "Using Adobe Flex 4".

This user guide is licensed for use under the terms of the Creative Commons Attribution 
Non-Commercial 3.0 License. 

This License allows users to copy, distribute, and transmit the user guide for noncommercial 
purposes only so long as 
  (1) proper attribution to Adobe is given as the owner of the user guide; and 
  (2) any reuse or distribution of the user guide contains a notice that use of the user guide is governed by these terms. 
The best way to provide notice is to include the following link. 
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

-->



    <!-- dpcontrols/DataGridSort.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"
    initialize="initDP();" width="550" height="400">
    <s:layout>
        <s:VerticalLayout />
    </s:layout>
    <fx:Script> 
         
        import mx.events.DataGridEvent; 
        import mx.collections.*; 
        // Declare storage variables and initialize the simple variables. 
        // The data provider collection. 
        private var myDPColl:ArrayCollection; 
        // The Sort object used to sort the collection. 
        [Bindable] 
        private var sortA:Sort; 
        // The sort fields used to determine the sort. 
        private var sortByInStock:SortField; 
        private var sortByArtist:SortField; 
        private var sortByAlbum:SortField; 
        private var sortByPrice:SortField; 
        // The data source that populates the collection. 
        private var myDP:Array = [ 
            {Artist:'Pavement', Album:'Slanted and Enchanted',Price:11.99, InStock: true}, 
            {Artist:'Pavement', Album:'Crooked Rain,Crooked Rain',Price:10.99, InStock: false}, 
            {Artist:'Pavement', Album:'Wowee Zowee',Price:12.99, InStock: true}, 
            {Artist:'Asphalt', Album:'Brighten the Corners',Price:11.99, InStock: false}, 
            {Artist:'Asphalt', Album:'Terror Twilight',Price:11.99, InStock: true}, 
            {Artist:'Asphalt', Album:'Buildings Meet the Sky',Price:14.99, InStock: true}, 
            {Artist:'Other', Album:'Other',Price:5.99, InStock: true} 
        ]; 
        //Initialize the DataGrid control with sorted data. 
        private function initDP():void { 
            //Create an ArrayCollection backed by the myDP array of data. 
            myDPColl = new ArrayCollection(myDP); 
            //Create a Sort object to sort the ArrrayCollection. 
            sortA = new Sort(); 
            //Initialize SortField objects for all valid sort fields: 
            // A true second parameter specifies a case-insensitive sort. 
            // A true third parameter specifies descending sort order. 
            // A true fourth parameter specifies a numeric sort. 
            sortByInStock = new SortField("InStock", true, true); 
            sortByArtist = new SortField("Artist", true); 
            sortByAlbum = new SortField("Album", true); 
            sortByPrice = new SortField("Price", true, false, true); 
            // Sort the grid using the InStock, Artist, and Album fields. 
            sortA.fields=[sortByInStock, sortByArtist, sortByAlbum]; 
            myDPColl.sort=sortA; 
            // Refresh the collection view to show the sort. 
            myDPColl.refresh(); 
            // Initial display of sort fields 
            tSort0.text = "First Sort Field: InStock"; 
            tSort1.text = "Second Sort Field: Artist"; 
            tSort2.text = "Third Sort Field: Album"; 
            // Set the ArrayCollection as the DataGrid data provider. 
            myGrid.dataProvider=myDPColl; 
            // Set the DataGrid row count to the array length, 
            // plus one for the header. 
            myGrid.rowCount=myDPColl.length +1; 
        } 
        // Re-sort the DataGrid control when the user clicks a header. 
        private function headRelEvt(event:DataGridEvent):void { 
            // The new third priority was the old second priority. 
            sortA.fields[2] = sortA.fields[1]; 
            tSort2.text = "Third Sort Field: " + sortA.fields[2].name; 
            // The new second priority was the old first priority. 
            sortA.fields[1] = sortA.fields[0]; 
            tSort1.text = "Second Sort Field: " + sortA.fields[1].name; 
            // The clicked column determines the new first priority. 
            if (event.columnIndex==0) { 
                sortA.fields[0] = sortByArtist; 
            } else if (event.columnIndex==1) { 
                sortA.fields[0] = sortByAlbum; 
            } else if (event.columnIndex==2) { 
                sortA.fields[0] = sortByPrice; 
            } else { 
                sortA.fields[0] = sortByInStock;
            } 
            tSort0.text = "First Sort Field: " + sortA.fields[0].name; 
            // Apply the updated sort fields and re-sort. 
            myDPColl.sort=sortA; 
            // Refresh the collection to show the sort in the grid. 
            myDPColl.refresh(); 
            // Prevent the DataGrid from doing a default column sort. 
            event.preventDefault(); 
        } 
      
    </fx:Script>
    <!--
        The Data Grid control. By default the grid and its columns can be
        sorted by clicking. The headerRelease event handler overrides the
        default sort behavior.
    -->
    <mx:DataGrid id="myGrid" width="100%" headerRelease="headRelEvt(event);">
        <mx:columns>
            <mx:DataGridColumn minWidth="120" dataField="Artist" />
            <mx:DataGridColumn minWidth="200" dataField="Album" />
            <mx:DataGridColumn width="75" dataField="Price" />
            <mx:DataGridColumn width="75" dataField="InStock"
                headerText="In Stock" />
        </mx:columns>
    </mx:DataGrid>
    <mx:VBox>
        <mx:Label id="tSort0" text="First Sort Field: " />
        <mx:Label id="tSort1" text="Second Sort Field: " />
        <mx:Label id="tSort2" text="Third Sort Field: " />
    </mx:VBox>
</s:Application>

   
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Scroll DataGridScroll DataGrid
2.Set selected index for DataGridSet selected index for DataGrid
3.Variable row heightsVariable row heights
4.Multi-line data grid rowMulti-line data grid row
5.variable scope for DataGridvariable scope for DataGrid
6.Creating inline item renderer and editorCreating inline item renderer and editor
7.Using a DataGridUsing a DataGrid
8.Formatting grid lines with CSSFormatting grid lines with CSS
9.Set editing position for DataGridSet editing position for DataGrid
10.Append new value to DataGrid from Form fieldsAppend new value to DataGrid from Form fields
11.Item click event for DataGridItem click event for DataGrid
12.Editable DataGridEditable DataGrid
13.Variable row height DataGridVariable row height DataGrid
14.DataGrid change eventDataGrid change event
15.Use Panel to layout controls and hold DataGridUse Panel to layout controls and hold DataGrid
16.DataGrids take a dataProvider to populate themDataGrids take a dataProvider to populate them
17.Default setting for DataGridDefault setting for DataGrid
18.Call HTTP server and set result to DataGrid
19.Binding XML to DataGridBinding XML to DataGrid
20.DataGrid Configuration through ActionScriptDataGrid Configuration through ActionScript
21.DataGrid Validate NowDataGrid Validate Now
22.DataGrid Validate Now Selected indexDataGrid Validate Now Selected index
23.DataGrid with static string valuesDataGrid with static string values
24.DataGrid DataDataGrid Data
25.DataGrid EventsDataGrid Events
26.Inline DataGrid Image ScopeInline DataGrid Image Scope
27.Print DataGrid outPrint DataGrid out
28.Enabling the user to move many items at onceEnabling the user to move many items at once
29.data provider contains fields for an artist, album name, and price.data provider contains fields for an artist, album name, and price.