Sort Grouped Columns in AdvancedDatagrid

3.3k views Asked by At

I#m looking for a solution to sort all grouped colums in advancedDatagrid. It should be the same behavior like clicking in a columns head.

Please note, i'm not looking for a solution to sort the fields IN a the grouping. There is working solution named compareFunction.

But I'm not able to sort the two rows in my picture by column "Einnahmen". Sorry for the german word. Do you have an Idea? enter image description here

Thank you Frank

4

There are 4 answers

2
Ian T On BEST ANSWER

If you're looking to start the application with sorted columns, you need to simulate a header click upon creation/dateprovider update.

Try this:

<mx:AdvancedDataGrid id="adg"
  updateComplete="adg.dispatchEvent(new AdvancedDataGridEvent(AdvancedDataGridEvent.HEADER_RELEASE, false, true, 3, 'Einnahmen'))">
    <mx:columns>
        ...
        <mx:AdvancedDataGridColumn dataField="Einnahmen" sortDescending="true" />
    </mx:columns>
</mx:AdvancedDataGrid> 

I haven't tried this for grouped collections but let me know how this works.

2
kbgn On

I used 'HierarchicalData' as the dataprovider for the AdvancedDataGrid.I also created the HierarchialData from the ArrayCollection, the sort order was maintained and it works like a charm.You can give it a try! Please find the Actionscript code and a sample screenshot.

Advanced Datagrid

    public function createHierarchialResultVO(results:ArrayCollection):ArrayCollection
    {
        [ArrayElementType("ResultsVO")]
        var dpHierrarchy:ArrayCollection = new ArrayCollection();

        for each(var result:Result in results)
        {
            var resultVO:ResultsVO= new ResultsVO();
            resultVO.resultName = result.resultName;
            resultVO.runDate = result.runDate.toString();
            resultVO.type="header";

            var childrens:ArrayCollection = new ArrayCollection();
            for each(var processDetails:ProcessDetails in result.details)
            {
                var children:ResultsVO= new ResultsVO();
                children.files =result.fileCount;
                children.status=result.status;
                children.type="result";
            }
            resultVO.children =children;
            dpHierrarchy.addItem(resultVO);
        }
        return dpHierrarchy;            
    }
0
kbgn On

Did you try to set the 'sortExpertMode="true"' in the AdvancedDataGrid tag? Please find the sample code below:

  <mx:AdvancedDataGrid height="318"
                     id="dataSetsDG"
                     allowMultipleSelection="false"
                     folderClosedIcon="{null}"
                     folderOpenIcon="{null}"
                     defaultLeafIcon="{null}"
                     left="28"
                     top="162"
                     rowCount="11"
                     width="70%"
                     displayItemsExpanded="true"
                     sortExpertMode="true">
2
Jason Towne On

EDIT: I didn't know you were building your dataProvider through ActionScript. You should still be able to run the Sort on your collection after you've finished putting it together though.

Another option would be to extend the AdvancedDataGrid component and override the set dataProvider method to sort the data as soon as you bind it. Here's an example another developer provided (source) for the Tree control, but the same concept could probably be used on the AdvancedDataGrid.

Original answer:

Assuming you're just trying to sort the top level "Einnahmen" values (which it looks like you are from your screenshot), I would manually sort your dataProvider after data is returned from whatever service you're using to get your data.

Here's a very basic example of manually sorting your collection.

[Bindable] public var MyDataList:ArrayCollection;
private var einnahmenSortField:SortField = new SortField("Einnahmen", true, false);
private var theSort:Sort = new Sort();

// Called after data is returned from the remote service call to sort the data
public function SetMyDataList(returnedList:ArrayCollection):void
{
  MyDataList = returnedList;

  if (theSort == null)
  {
    theSort = new Sort();
    theSort.fields = [einnahmenSortField];
  }

  MyDataList.sort = this.theSort;   
  MyDataList.refresh();
}

To convert it to HierarchicalData for use in your AdvancedDataGrid, just create a new instance of HierarchicalData and assign it to your AdvancedDataGrid like this:

var hd:HierarchicalData = new HierarchicalData(myDataList);
hd.childrenField = "MyChildField";
myAdvancedDataGrid.dataProvider = hd;