AdvancedDataGrid 自定义排序

AdvancedDataGrid自定义排序问题之前在DataGrid写了一个排序方法

public function sortList(colName:String,sortType:String):void{
var sortField:SortField;
this.colName = colName;
if(sortType=="desc"){
sortField = new SortField(colName);
sortField.descending = true;
}
if(sortType=="asc"){
sortField = new SortField(colName);
sortField.descending = false;
}
var sort:Sort = new Sort();
sort.fields = [sortField];

dpFlat.sort = sort;
dpFlat.refresh();
}


很好用,但是在AdvancedDataGrid中,这个方法就不好使了。
我的想法是,在AdvancedDataGrid中,不打乱分组,然后对某一列进行排序,应该如何实现?

----------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.DateField;
import mx.events.AdvancedDataGridEvent;
import mx.collections.Sort;
import mx.collections.SortField;
import mx.utils.ObjectUtil;
import mx.collections.ArrayCollection;
import mx.collections.Grouping;
import mx.collections.GroupingCollection;
import mx.collections.GroupingField;
import mx.controls.Alert;
//include "SimpleFlatData.as"
[Bindable]
public var dpFlat:ArrayCollection = new ArrayCollection([
{Name:"name", Region:"Southwest", Territory:"Northern California", Territory_Rep:"T.R. Smith", Actual:55498, Estimate:50000},
{Name:"name", Region:"Southwest", Territory:"Southern California", Territory_Rep:"Alice Treu", Actual:44985, Estimate:8000},
{Name:"name", Region:"Southwest", Territory:"Arizona", Territory_Rep:"Barbara Jennings", Actual:38865, Estimate:40000},
{Name:"name", Region:"Southwest", Territory:"Abc", Territory_Rep:"Dana Binn", Actual:29885, Estimate:20000},
{Name:"name", Region:"Southwest", Territory:"Central California", Territory_Rep:"Joe Smith", Actual:29134, Estimate:30000},
{Name:"name", Region:"Southwest", Territory:"evada", Territory_Rep:"Bethany Pittman", Actual:52888, Estimate:45000},
{Name:"name", Region:"Southwest", Territory:"Northern California", Territory_Rep:"Lauren Ipsum", Actual:38805, Estimate:40000},
{Name:"name", Region:"Southwest", Territory:"Srthern California", Territory_Rep:"Bauren Ipsum", Actual:38805, Estimate:40000},
{Name:"name", Region:"Southwest", Territory:"Vrthern California", Territory_Rep:"Fauren Ipsum", Actual:38805, Estimate:40000},
{Name:"name", Region:"Southwest", Territory:"Southern California", Territory_Rep:"Jane Grove", Actual:44913, Estimate:9000}]);

public function numericCompareFunc(itemA:int, itemB:int):int
{
return ObjectUtil.numericCompare(itemA, itemB);
}
public function stringCompareFunc(itemA:String, itemB:String):int
{
return ObjectUtil.stringCompare(itemA, itemB);
}
public function myCompare(a:Object, b:Object, fields:Array=null):int
{
var result:int = 0;
var i:int = 0;
var propList:Array = ["Estimate","Territory"];
var len:int = propList.length;
var propName:String;
while (result == 0 && (i < len))
{
propName = propList[i];
if (propName == "Estimate") {
result = numericCompareFunc(a[propName], b[propName]);
} else {
result = stringCompareFunc(a[propName], b[propName]);
}
i++;
}
return result;
}
public function sortTerritory():void
{
gc.grouping.compareFunction = myCompare;
gc.refresh();
}
]]>
</mx:Script>
<mx:GroupingCollection id="gc" source="{dpFlat}" >
<mx:Grouping >
<mx:GroupingField id="gf" name="Estimate" caseInsensitive="true" />
</mx:Grouping>
</mx:GroupingCollection>
<mx:AdvancedDataGrid id="myADG" width="100%" height="100%"
dataProvider="{gc}"
initialize="{gc.refresh()}"
displayItemsExpanded="true"
creationComplete="{myADG.expandAll()}"
defaultLeafIcon="{null}"
folderOpenIcon="{null}"
folderClosedIcon="{null}">
<mx:groupedColumns>
<mx:AdvancedDataGridColumn dataField="Region"/>
<mx:AdvancedDataGridColumn dataField="Territory" />
<mx:AdvancedDataGridColumn dataField="Territory_Rep" headerText="Territory Rep" sortable="false"/>
<mx:AdvancedDataGridColumn dataField="Actual" visible="false" />
<mx:AdvancedDataGridColumn dataField="Estimate" sortable="false"/>
</mx:groupedColumns>
</mx:AdvancedDataGrid>
<mx:Button label="按Territory排序" click="sortTerritory()"/>
</mx:Application>



[color=red][b]注意,在GroupingCollection中,要让arrayCollation itemUpdate有作用,一定需要加上gc.refresh()[/b][/color]
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页