The following code is a brief example of sorting a Flex XMLListCollection using the Sort and SortField classes, and the
XMLListCollection.sort
property. We also look at filtering the XMLCollection using a custom filter function.
<?
xml version="1.0" encoding="utf-8"
?>
<!-- http://blog.flexexamples.com/2007/08/22/sorting-and-filtering-data-in-an-xmllistcollection/ -->
< mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml"
layout ="vertical"
verticalAlign ="middle"
backgroundColor ="white"
creationComplete ="init()" >
< mx:Script >
<![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.controls.*;
private function init():void {
describeTypeXML = describeType(DataGrid);
factoryMethodsXLC.source = describeTypeXML.factory.method;
}
private function sortXLC():void {
var nameSort:Sort = new Sort();
nameSort.fields = [new SortField('@name', true)];
factoryMethodsXLC.sort = nameSort;
factoryMethodsXLC.refresh();
}
private function filterXLC():void {
if (filterCh.selected) {
factoryMethodsXLC.filterFunction = declaredBy_filterFunc;
factoryMethodsXLC.refresh();
} else {
factoryMethodsXLC.filterFunction = null;
factoryMethodsXLC.refresh();
}
}
private function declaredBy_filterFunc(item:XML):Boolean {
return item.@declaredBy == describeTypeXML.@name;
}
]]>
</ mx:Script >
< mx:XML id ="describeTypeXML" />
< mx:XMLListCollection id ="factoryMethodsXLC" />
< mx:VBox >
< mx:DataGrid id ="factoryMethodsGrid"
dataProvider ="{factoryMethodsXLC}"
width ="400"
rowCount ="7" >
< mx:columns >
< mx:DataGridColumn dataField ="@name" />
< mx:DataGridColumn dataField ="@returnType" />
< mx:DataGridColumn dataField ="@declaredBy" />
</ mx:columns >
</ mx:DataGrid >
< mx:HBox width ="100%" >
< mx:Button id ="sortBtn"
label ="Sort ({factoryMethodsGrid.dataProvider.length} items)"
click ="sortXLC()" />
< mx:Spacer width ="100%" />
< mx:CheckBox id ="filterCh"
label ="{describeTypeXML.@name} only"
click ="filterXLC()" />
</ mx:HBox >
</ mx:VBox >
</ mx:Application >
<!-- http://blog.flexexamples.com/2007/08/22/sorting-and-filtering-data-in-an-xmllistcollection/ -->
< mx:Application xmlns:mx ="http://www.adobe.com/2006/mxml"
layout ="vertical"
verticalAlign ="middle"
backgroundColor ="white"
creationComplete ="init()" >
< mx:Script >
<![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.controls.*;
private function init():void {
describeTypeXML = describeType(DataGrid);
factoryMethodsXLC.source = describeTypeXML.factory.method;
}
private function sortXLC():void {
var nameSort:Sort = new Sort();
nameSort.fields = [new SortField('@name', true)];
factoryMethodsXLC.sort = nameSort;
factoryMethodsXLC.refresh();
}
private function filterXLC():void {
if (filterCh.selected) {
factoryMethodsXLC.filterFunction = declaredBy_filterFunc;
factoryMethodsXLC.refresh();
} else {
factoryMethodsXLC.filterFunction = null;
factoryMethodsXLC.refresh();
}
}
private function declaredBy_filterFunc(item:XML):Boolean {
return item.@declaredBy == describeTypeXML.@name;
}
]]>
</ mx:Script >
< mx:XML id ="describeTypeXML" />
< mx:XMLListCollection id ="factoryMethodsXLC" />
< mx:VBox >
< mx:DataGrid id ="factoryMethodsGrid"
dataProvider ="{factoryMethodsXLC}"
width ="400"
rowCount ="7" >
< mx:columns >
< mx:DataGridColumn dataField ="@name" />
< mx:DataGridColumn dataField ="@returnType" />
< mx:DataGridColumn dataField ="@declaredBy" />
</ mx:columns >
</ mx:DataGrid >
< mx:HBox width ="100%" >
< mx:Button id ="sortBtn"
label ="Sort ({factoryMethodsGrid.dataProvider.length} items)"
click ="sortXLC()" />
< mx:Spacer width ="100%" />
< mx:CheckBox id ="filterCh"
label ="{describeTypeXML.@name} only"
click ="filterXLC()" />
</ mx:HBox >
</ mx:VBox >
</ mx:Application >
本文转自 OldHawk 博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2008/01/10/1034065.html
,如需转载请自行联系原作者