开发者

Preserve multi-column sort in AdvancedDataGrid with dataProvider as GroupingCollection

开发者 https://www.devze.com 2022-12-14 05:45 出处:网络
I have three attributes in my XML object: last name, first name, and age. My sample XML looks like: <dataXML>

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 even开发者_如何学Ct 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>


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();
            }


I had this same problem, and based on JtheGeek's and rocksoccer's answers, I was able to preserve the sort order.

My set-up is a bit different. I have a var _xmlListData:XMLListCollection that is bound directly to AdvancedDataGrid.dataProvider. My refresh function simply does this:

var sort:Sort = _xmlListData.sort;
_xmlListData.source = data as XMLList;
_xmlListData.sort = sort;

And it works. So, I wonder if doing the same thing to your XMLListCollection before calling gc.refresh() would have the same results? I.e.:

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


I think JTtheGeek's anwser is not quite correct. The code would just work when you sort on one column. Because except the descending order, you also need to remember the priority between different columns in sorting.

In fact, it is so easy, create a temp sort object to store the sort in the dataProvider, and then restore it after you apply the new dataProver, then refresh it.

0

精彩评论

暂无评论...
验证码 换一张
取 消