views:

324

answers:

1

I have three attributes in my XML object: last name, first name, and age. My sample XML looks like:

<dataXML>
 <info last="Abc" first="Def" age="20"/>
 <info last="Abc" first="Hij" age="10"/>
 <info last="Xyz" first="Klm" age="25"/>
 <info last="Xyz" first="Opq" age="64"/>
 <info last="Xyz" first="Rst" age="08"/>
</dataXML>

I am using Grouping Collection and AdvancedDataGrid to show the data. My problem is to preserve multi-column sort. After a refresh happens, the user selected sorting order goes away, and the grid gets sorted by first column only. So suppose, user has sorted the table, first ascending by "Age" and then descending by "Name"; after a refresh happens, the grid again gets sorted ascending by "Name". I don't want the refresh event to change the sort order, only the data should get refreshed.

Thanks in advance.

P.S. I can't use any other datatype like ArrayCollection, to store the data.

Part of my code looks as follows:

<mx:Script>
<![CDATA[
 [Bindable]
 private var dataXML:XMLListCollection = new XMLListCollection();

 private function refresh(data:Object):void
 {
  dataXML.source = XML(data.result.value).info;
  gc.refresh();
  adGrid.dataProvider = gc;
  adGrid.validateNow();
  adGrid.dataProvider.refresh();
 }

 private function nameCompareFunction(a:XML, b:XML):int
 {
  return ObjectUtil.stringCompare(a.attribute("last") + a.attribute("first"), b.attribute("last") + b.attribute("first"));
 }

 private function valueSortCompareFunction(a:XML, b:XML):int
 {
  return ObjectUtil.numericCompare(Number(a.attribute("age")), Number(b.attribute("age")));
 }
]]>
</mx:Script> 

<Control:AdvancedDataGrid id="adGrid">
 <Control:dataProvider>
  <mx:GroupingCollection id="gc" source="{dataXML}">
   <mx:grouping>
    <mx:Grouping>
     <mx:GroupingField name="@last" compareFunction="nameCompareFunction"/>
    </mx:Grouping>
   </mx:grouping>
  </mx:GroupingCollection>
 </Control:dataProvider>   

 <Control:columns>
  <mx:AdvancedDataGridColumn id="ADGCName" dataField="@first" headerText="Name" wordWrap="true"/>
  <mx:AdvancedDataGridColumn id="ADGCAge" dataField="@age" headerText="Age" sortCompareFunction="valueSortCompareFunction"/>
 </Control:columns>
</Control:AdvancedDataGrid>
A: 

before the refresh (or when the sorting options are changed at all) store the current sort options, they are actually on the dataprovider. var objDp:HierarchicalCollectionView = ucADG.dataProvider as HierarchicalCollectionView;

the current SortField objects are on objDp.sort.fields in this case.

after the collection is refreshed just do the opposite. For example:

var objDP:HierarchicalCollectionView = ucADG.dataProvider as HierarchicalCollectionView;
            if(objDP != null)
            {
                var objSort:Sort = new Sort();
                objSort.fields = [ new SortField("SomeField", true, bUseDescending) ];
                objDP.sort = objSort;
                objDP.refresh();
            }
JTtheGeek