5 Replies Latest reply on Dec 21, 2010 10:42 AM by SpaghettiCoder

    Can I sort a datagrid by DataGridColumn

    DesOC1

      Hi,

       

      I have a DataGrid and I want to sort the data by the dataField "date" by default before the table is output to the FrontEnd? Is this possible?

       

      <mx:Panel width="95%" height="90%" title="FlexList1">
      <mx:DataGrid dataProvider ="{voiceAuthLogging}" width="100%" height="100%" id="datagrid1" editable="true">
        <mx:columns>
                       <mx:DataGridColumn headerText="Row#" labelFunction="lfRowNumError" width="42" textAlign="center"/>
                       <mx:DataGridColumn dataField="date" headerText="DATE" showDataTips="true"/>
                       <mx:DataGridColumn dataField="employeeid" headerText="EMPLOYEE" showDataTips="true"/>
                       <mx:DataGridColumn dataField="comment" headerText="COMMENT"/>
                       <mx:DataGridColumn dataField="appusage" headerText="APPUSAGE"/>
                       <mx:DataGridColumn dataField="age" headerText="AGE"/>
        </mx:columns>
      </mx:DataGrid>
      </mx:Panel>

       

      Thanks.

       

      D

        • 1. Re: Can I sort a datagrid by DataGridColumn
          SpaghettiCoder Level 3

           

           

           

          var _mysortField:SortField = new SortField("date", true, false, true);

          var _mySort:Sort = new Sort();

           

          _mySort.fields = [_mySortField];

           

          yourArrayCollection.sort = _mySort;

          yourArrayCollection.refresh();

          • 2. Re: Can I sort a datagrid by DataGridColumn
            DesOC1 Level 1

            Hi, I have done this (code below) and it has not worked. My Collection is called authLogging. This has not worked. Apologies, I'm fairly new at Flex. I've called the function in the init()  function.

             

            Any other idea?

             

             

            private function sortDataGrid(): void

            {

             

             

            var _mySortField:SortField = new SortField("date", true, false, true);

             

            var _mySort:Sort = new Sort();

            _mySort.fields = [_mySortField];

            authLogging.sort = _mySort;

            authLogging.refresh();

            }

            • 3. Re: Can I sort a datagrid by DataGridColumn
              SpaghettiCoder Level 3

              When you say it doesn't work, it simply doesn't do anything...and it doesn't throw a error message right?

               

              1. do a trace in init, make sure it's being executed

              2. do a trace in sortDataGrid() make sure it's being executed here as well

               

              If both of those functions are executed and the app doesn't throw an error msg,then post your init function, so we can see what's going on there...

              • 4. Re: Can I sort a datagrid by DataGridColumn
                SpaghettiCoder Level 3

                <?xml version="1.0" encoding="utf-8"?>

                <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" minWidth="955" minHeight="600">

                 

                 

                 

                <fx:Script>

                <![CDATA[

                 

                 

                import mx.collections.ArrayCollection;

                 

                import mx.collections.Sort;

                 

                import mx.collections.SortField;

                 

                 

                private var array:Array = [

                {employeeid:

                "john", age:21, date:"2010-11-21"},

                {employeeid:

                "jane", age:12, date:"2009-12-24"},

                {employeeid:

                "jack", age:64, date:"2009-01-15"},

                {employeeid:

                "jill", age:32, date:"2008-04-01"}

                ];

                 

                [

                Bindable]

                 

                private var yourArrayCollection:ArrayCollection = new ArrayCollection(array);

                 

                 

                private function sort(event:MouseEvent):void

                {

                 

                var _mySortField:SortField = new SortField("date", true, false);

                 

                 

                var _mySort:Sort = new Sort();

                _mySort.fields = [_mySortField];

                 

                yourArrayCollection.sort = _mySort;

                 

                yourArrayCollection.refresh();

                }

                 

                ]]>

                 

                </fx:Script>

                 

                 

                <mx:DataGrid dataProvider="{yourArrayCollection}" id="datagrid1" editable="true" x="10" y="29">

                 

                <mx:columns>

                 

                <mx:DataGridColumn headerText="Row#" width="42" textAlign="center"/>

                 

                <mx:DataGridColumn dataField="date" headerText="DATE" showDataTips="true"/>

                 

                <mx:DataGridColumn dataField="employeeid" headerText="EMPLOYEE" showDataTips="true"/>

                 

                <mx:DataGridColumn dataField="comment" headerText="COMMENT"/>

                 

                <mx:DataGridColumn dataField="appusage" headerText="APPUSAGE"/>

                 

                <mx:DataGridColumn dataField="age" headerText="AGE"/>

                 

                </mx:columns>

                 

                </mx:DataGrid>

                 

                 

                 

                <s:Button label="sort" click="sort(event)" x="10" y="0"/>

                 

                </s:Application>

                • 5. Re: Can I sort a datagrid by DataGridColumn
                  SpaghettiCoder Level 3

                  posted code that I tested on...

                   

                  made a slight change to

                   

                  var _mySortField:SortField = new SortField("date", true, false);

                   

                  because you're sorting numbers rather than strings...other than tha the sort function should be working...