Skip navigation
Gug88
Currently Being Moderated

Datagrid sorting

Mar 6, 2013 2:55 AM

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

 
Replies
  • Currently Being Moderated
    Mar 6, 2013 11:48 AM   in reply to Gug88

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 7, 2013 4:16 AM   in reply to Gug88

    Can you post your code?

     

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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 8, 2013 4:15 AM   in reply to Gug88

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 8, 2013 8:29 AM   in reply to Gug88

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points