views:

26

answers:

2

I have an AdvancedDataGrid that is being populated by customer data. Each customer has 3 monthly products(1, 3, 6), and also a passed field specifying whether the customer qualifies for any of the monthly products.

Now the grid is sorting the customer data alphabetically, which is a good thing, but it isnt sorting the monthly products, not so good thing.

The dataProvider looks something like this. (I am grouping by Funder.)

{Funder:"Customer1", Product:"1 Month", Passed:"False"}, 
{Funder:"Customer1", Product:"3 Month", Passed:"True"}, 
{Funder:"Customer1", Product:"6 Month", Passed:"True"}, 
{Funder:"Customer2", Product:"1 Month", Passed:"False"}, 
{Funder:"Customer2", Product:"3 Month", Passed:"False"}, 
{Funder:"Customer2", Product:"6 Month", Passed:"False"}

Then results that I get in the grid looks something like this

 ----------------------------------------
| Funder & Products   |  Product Passed  |
 ----------------------------------------
| Customer1           |                  |
|    6 Month          |  True            |
|    3 Month          |  True            |
|    1 Month          |  False           |
| Customer2           |                  |
|    3 Month          |  False           |
|    6 Month          |  False           |
|    1 Month          |  False           |
 ----------------------------------------

Any help on getting the products sorted as well?

EDIT:

Here is the code I use for the grid

<mx:AdvancedDataGrid id="myADG" 
                     width="100%" height="100%"
                     initialize="gc.refresh();"
                     folderClosedIcon="{null}"
                     folderOpenIcon="{null}"
                     defaultLeafIcon="{null}">

    <mx:dataProvider>
        <mx:GroupingCollection id="gc" source="{mCustomerData}">
            <mx:grouping>
                <mx:Grouping>
                    <mx:GroupingField name="Funder"/>
                </mx:Grouping>
            </mx:grouping>
        </mx:GroupingCollection>
    </mx:dataProvider>        

    <mx:columns>
        <mx:AdvancedDataGridColumn dataField="Product" 
                                   headerText="Funder &amp; Products"/>
        <mx:AdvancedDataGridColumn dataField="Passed"
                                   headerText="Product Passed"/>
        <mx:AdvancedDataGridColumn dataField="Passed"
                                   headerText="Product Failed"/>
    </mx:columns>
</mx:AdvancedDataGrid>
+1  A: 

For all intents and purposes, a Grid doesn't sort data. It just displays the data you provide it in the order you specified. The dataProvider must be sorted by you and the grid will update accordingly.

If you're sorting by clicking on column headers, then take a look at the sortCompareFunction on the AdvancedDataGridColumn

If you're using an ArrayCollection or XMLListCollection, then take a look at this documentation on how to sort it.

www.Flextras.com
I know that the grid doesnt sort the data for you. The thing is, the data is sorted and the grid displays it in a random sequence. I needed a way to get the data displayed in the correct way. Thank you for suggesting the sortCompareFunction, tried that, but it didnt work.
Pieter van Niekerk
From your question "the grid is sorting the customer data". I find it hard to believe that the grid is doing any sort on your data. the sortCompareFunction will only have affect if you click one of the headers to sort your data. Your solution to apply a sort to the data on creationComplete seems fine. Were you not applying a sort to the data before?
www.Flextras.com
No, I didnt apply a sort before, but the data got sorted alphabetically even though it isnt sorted in the dataProvider. I suppose it could be the grouping that alphabetized the dataProvider?
Pieter van Niekerk
That I'm not sure of, but it would make sense to me.
www.Flextras.com
A: 

Okay, so I found a solution that works for me. Here follows.

Basically, on creationComplete I call the sortData function that does the following.

private function sortData():void {
    var sort:Sort = new Sort();
    var sortField:SortField = new SortField("Product");

    sort.fields = [sortField];        // Set 'Product' as the field to be sorted on
    myADG.dataProvider.sort = sort;   // Add the sort to the dataProvider of the dataGrid
    gc.source.refresh();              // Refresh the GroupCollection
}

Hope to see another way of doing this, as I cant think that this is the only way of doing it

Pieter van Niekerk