3 Replies Latest reply on Oct 14, 2011 2:17 PM by m_j_95

    Sorting columns in a flex datagrid

    m_j_95 Level 1

      The datagrid gets its data from a back end database which has records like

       

           RecordID           Division     Department      Date_Report_Submitted

       

          1.                 Finance      Accounting        11/1/2010

          2.                 Engineering  Design            4/2/2011

          3.                 Engineering  Implementation    4/2/2011

          4.                 Support      Chat_Support      2/4/2010

       

       

       

       

      Clicking on the headers in the Datagrid column(Department) results in a sort based on recordID like

       

                  Division     Department      Date_Report_Submitted

       

                  Finance      Accounting        11/1/2010

                  Engineering  Design            4/2/2011

                  Engineering  Implementation    4/2/2011

                  Support      Chat_Support      2/4/2010

       

       

       

      whereas I want it to be sorted alphabetically for the Datagrid column(Department) like

       

                  Division     Department      Date_Report_Submitted

       

                  Finance      Accounting        11/1/2010

                  Support      Chat_Support      2/4/2010

                  Engineering  Design            4/2/2011

                  Engineering  Implementation    4/2/2011

       

      since Accounting should come before Chat_Support as per lexicographical order.

       

      Looked at http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-fl ex/#more-590 and have something like

       

               <mx:DataGrid id="myRecords"  dataProvider="{myRecords_dp}" width="810" height="274"                        

       

          itemClick="getRecordData(event)">

              <mx:columns>

                  <mx:DataGridColumn id="firstCol" width="180" fontFamily="Arial" fontSize="12"

                                     wordWrap="true" />

                  <mx:Button label="Click to Sort" click="mysort()" />

              </mx:columns>

          </mx:DataGrid>

       

      and

       

       

                  private function mysort():void

                  {

                     var sortField:SortField = new SortField();

                     sortField.compareFunction = mycompare;

                     sortField.descending = false;

                 

                     var sort:Sort = new Sort();

                     sort.fields = [sortField];

                 

                     myRecords.sort = sort;

                     myRecords.refresh();

                  }

       

       

                              private function mycompare(lhs:Object, rhs:Object):int

                  {

                      var valueA:String = lhs.text();

                      var valueB:String = rhs.text();

                      return ObjectUtil.stringCompare(valueA, valueB);

                     

                  }

       

      I get errors like

       

      1061: Call to a possibly undefined method refresh through a reference with static type mx.controls:DataGrid.   

      for myRecords.refresh();

      and

       

      Access of possibly undefined property sort through a reference with static type mx.controls:DataGrid.   

      for myRecords.sort

       

      Any suggestions would be appreciated.

        • 1. Re: Sorting columns in a flex datagrid
          jfb00 Level 3

          Try passing the name of your field like:

          private function mysort():void

                      {

                         var sort:Sort = new Sort();

                         sort.fields = [new SortField("Department",true)];

                         sort.fields[0].numeric = false; //true if it is a number or date type

                         myRecords.sort = sort;

                         myRecords.refresh();

                      }

          Rgds

           

          Johnny

          1 person found this helpful
          • 2. Re: Sorting columns in a flex datagrid
            bucpatr1 Level 2

            The reason you're running into trouble is that you are trying to sort the wrong thing. The datagrid doesn't know anything about sorting/filtering, it simply displays the data it is given. If you want to sort you need to call those methods on the dataprovider ( myRecords_dp ) not the datagrid ( myRecords).

             

            P.S. Keep in mind that the approach you are using will only work if your call to the server is bringing back all the data at once. If you are doing any sort of paging, the results will need to be sorted on the server side rather than the client.

            1 person found this helpful
            • 3. Re: Sorting columns in a flex datagrid
              m_j_95 Level 1

              I should have clarified this from the start itself. The backend database has few tables. One is Divisions which has some fields like

              Division ID Division Name. Another is Department which has fields like RecID, Division ID, Department Name. What is being retrieved from the dataprovider(myRecords_dp) is Rec ID, then a labelfunction converts the Rec ID into Department name by looping over the department table and displays it. So, if Implementation has a recID of 3 and Chat_Support of 4, Implementation will come before Chat_support when I click on the header so it will not be in lexicographical order.