7 Replies Latest reply: Mar 8, 2013 8:29 AM by DeanLoganBH RSS

    Datagrid sorting

    Gug88

      Hello,

      I have a problem when it comes to sorting a datagrid with multiple fields. I have two fields A and B . B has numerical values . How can i sort B yet also change the data in A since they are dependent from each other?

       

      This is my datagrid

      <s:DataGrid id="rankinggrid" includeIn="Ranking" x="156" y="84" width="528" height="229"

                                              dataProvider="{gridrank}" requestedRowCount="4">

                          <s:columns>

                                    <s:ArrayList>

                                              <s:GridColumn dataField="sequrank" headerText="Sequence" sortDescending="true"></s:GridColumn>

                                              <s:GridColumn dataField="coeff" headerText="Coeffiecent"></s:GridColumn>

                                    </s:ArrayList>

                          </s:columns>

                  </s:DataGrid>

       

       

      Basically i want to sort coeff in desending order but i want to data in seqrank to move accordingly.

       


      Thanks for your help

       

       

      P.s i am managing to populate the datagrid so all i need is to sort it thanks

        • 1. Re: Datagrid sorting
          DeanLoganBH Community Member

          Do your sort on the "gridrank" before populating the grid.

           

          Here is one way of doing it.

          // Create the Sort instance.
          var mySort:Sort = new Sort();
                          
          // Set the sort field; sort on the last name first, first name second.
          var sortfieldRank:SortField = new SortField("sequrank", descendingSort);
          mySort.fields = [sortfieldRank];
          
          // Apply the sort to the collection.
          gridrank.sort = mySort;
          gridrank.refresh();
          
          

           

          If it is really not sorting all the columns together.

          • 2. Re: Datagrid sorting
            Gug88

            Thanks for your help i really appriciate it.

             

            Unforturnatly the key work descending sort is being marked as an error,and hence does not let me process..

             

            Do you have any idea why ?

             

            Thanks again

            • 3. Re: Datagrid sorting
              DeanLoganBH Community Member

              Can you post your code?

               

              When you do, click the "Use advanced editor" and insert code as SQL, to make it easier to read.

              • 4. Re: Datagrid sorting
                Gug88

                Thanks for your help again


                I have reduced the code since the original code is very lengthy and hence impossible to post , yet this should work.

                 

                Here i am sorting the grid array according to cost. The problem is that it doesnt work it out in a descending manner(the command you told me doesnt work it gives me an error ) i have used sort and it sorts it in an ascending way and does not give me an error

                 

                I would like to solve in a descending way Hope you can help thanks for your time i really appreciate

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                                                                     xmlns:s="library://ns.adobe.com/flex/spark"
                                                                     xmlns:mx="library://ns.adobe.com/flex/mx"
                                                                     creationComplete="init()" 
                                                                     width="800" height="600">
                  
                          <fx:Declarations>
                                    <s:ArrayCollection id="ProcessAdder">
                                              <fx:Object ProcessName ="Milling" Cost ="240" Service_Rate="14"  />
                                              <fx:Object ProcessName ="Lathe" Cost ="975" Service_Rate="9" />
                                              <fx:Object ProcessName ="Boring" Cost ="800" Service_Rate="8" />
                                              <fx:Object ProcessName ="Drilling" Cost ="270" Service_Rate="20"  />
                                    </s:ArrayCollection>
                          </fx:Declarations>
                  
                          <fx:Script>
                                    <![CDATA[
                
                
                                              import mx.collections.*;
                                              import mx.collections.ArrayCollection;
                                              import mx.collections.Sort;
                                              import mx.collections.SortField;
                  
                  
                                              public function init(){
                  
                                              var mySort:Sort = new Sort();
                  
                                              // Set the sort field; sort on the last name first, first name second.
                                              var sortfieldRank:SortField = new SortField("Cost", Sort); //descendingSort
                  
                                              mySort.fields = [sortfieldRank];
                  
                                              // Apply the sort to the collection.
                                              //gridrank.sort = mySort;
                                              ProcessAdder.sort = mySort;
                  
                                              ProcessAdder.refresh();
                  
                                              }
                  
                                    ]]>
                          </fx:Script>
                    
                  
                          <mx:Legend dataProvider="{ProcessAdder}"/>
                          <mx:DataGrid id="ProssGrid" x="22" y="334" width="715" dataProvider="{ProcessAdder}"
                                                        >
                                    <mx:columns>
                                              <mx:DataGridColumn dataField="ProcessName" headerText="Process Name"/>
                                              <mx:DataGridColumn dataField="Cost" headerText="Cost"/>
                                              <mx:DataGridColumn dataField="Service_Rate" headerText="Service Rate"/> 
                  
                                    </mx:columns>
                          </mx:DataGrid>
                  
                </s:WindowedApplication>
                
                
                • 5. Re: Datagrid sorting
                  DeanLoganBH Community Member

                  Oops.  So, here is what the issues was.  In my example, the variable "descendingSort" was a boolean that I created to change the sort from Descending to Ascending and back when a button was click.  In the SortField function, you pass in whether or not you want the fields to be a descending sort.

                   

                  Here is the working sort function, based on your code.

                  private function init(event:FlexEvent):void {
                   var mySort:Sort = new Sort();
                  
                   // Set the sort field; sort on the last name first, first name second.
                   var sortfieldRank:SortField = new SortField("Cost", true);
                   mySort.fields = [sortfieldRank];
                  
                   // Apply the sort to the collection.
                   ProcessAdder.sort = mySort;
                   ProcessAdder.refresh();
                  }
                  

                   

                  You might want to look at my simple DataGrid example to see how I handle the array objects.  I find it is easier to handled sorts and other actions if the values are the correct type (i.e. String, Date, Number).  Here is the View Source link.

                  • 6. Re: Datagrid sorting
                    Gug88

                    Again thanks for your help

                     

                    But unfortunatly it is not working. I want it that by default it comes in descending order without the need of pressing any buttons.

                     

                    Using the true and false boolean results in the same result(dont know why  )--->always results in giving the values in ascending order

                     

                    Please help me

                     

                    Thanks again for your help DeanLoganBH

                    • 7. Re: Datagrid sorting
                      DeanLoganBH Community Member

                      I think it has to do with the items being String and not Numeric, so you need to add the last parameter to make the sort Numeric.

                       

                      So, to see the vales in ascending order, you would do this.

                        
                      var sortfieldRank:SortField = new SortField("Cost", false, true);
                      

                       

                      AscendingSort_onNumeric.png