www.diy567.com累了,去上面休息一下下,QQ空间,美文,非主流,网络日记,搞笑短信,祝福短信,热门短信,有意思啊
下面是完整代码(或点击这里察看):
Download:
main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.utils.ObjectUtil;
private function sortOld():void {
var sortField:SortField = new SortField();
sortField.compareFunction = test2_compareFunc;
sortField.descending = checkBox.selected;
var oldSort:Sort = new Sort();
oldSort.fields = [sortField];
xmlListColl.sort = oldSort;
xmlListColl.refresh();
}
private function sortNew():void {
var sortField:SortField = new SortField();
sortField.compareFunction = test3_compareFunc;
sortField.descending = checkBox.selected;
var sort:Sort = new Sort();
sort.fields = [sortField];
xmlListColl.sort = sort;
xmlListColl.refresh();
}
private function resetSort():void {
xmlListColl.sort = null;
xmlListColl.refresh();
}
private function dataGridCol_labelFunc(item:XML, col:DataGridColumn):String {
return item.*.(@name == col.dataField).text();
}
private function test2_compareFunc(itemA:XML, itemB:XML):int {
var valueA:String = itemA.test2.text();
var valueB:String = itemB.test2.text();
return ObjectUtil.stringCompare(valueA, valueB);
}
private function test3_compareFunc(itemA:XML, itemB:XML):int {
var valueA:String = itemA.test3.text();
var valueB:String = itemB.test3.text();
return ObjectUtil.stringCompare(valueA, valueB);
}
]]>
</mx:Script>
<mx:XML id="tests" source="tests.xml" />
<mx:XMLListCollection id="xmlListColl" source="{tests.test}" />
<mx:ApplicationControlBar dock="true">
<mx:CheckBox id="checkBox" label="descending:" labelPlacement="left" />
<mx:Button label="Sort 'old'"
click="sortOld();" />
<mx:Button label="Sort 'new'"
click="sortNew();" />
<mx:Button label="Reset sort"
click="resetSort();" />
</mx:ApplicationControlBar>
<mx:DataGrid id="dataGrid"
dataProvider="{xmlListColl}"
width="300"
verticalScrollPolicy="on"
sortableColumns="true">
<mx:columns>
<mx:DataGridColumn dataField="old"
labelFunction="dataGridCol_labelFunc"
sortCompareFunction="test2_compareFunc" />
<mx:DataGridColumn dataField="new"
labelFunction="dataGridCol_labelFunc"
sortCompareFunction="test3_compareFunc" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.utils.ObjectUtil;
private function sortOld():void {
var sortField:SortField = new SortField();
sortField.compareFunction = test2_compareFunc;
sortField.descending = checkBox.selected;
var oldSort:Sort = new Sort();
oldSort.fields = [sortField];
xmlListColl.sort = oldSort;
xmlListColl.refresh();
}
private function sortNew():void {
var sortField:SortField = new SortField();
sortField.compareFunction = test3_compareFunc;
sortField.descending = checkBox.selected;
var sort:Sort = new Sort();
sort.fields = [sortField];
xmlListColl.sort = sort;
xmlListColl.refresh();
}
private function resetSort():void {
xmlListColl.sort = null;
xmlListColl.refresh();
}
private function dataGridCol_labelFunc(item:XML, col:DataGridColumn):String {
return item.*.(@name == col.dataField).text();
}
private function test2_compareFunc(itemA:XML, itemB:XML):int {
var valueA:String = itemA.test2.text();
var valueB:String = itemB.test2.text();
return ObjectUtil.stringCompare(valueA, valueB);
}
private function test3_compareFunc(itemA:XML, itemB:XML):int {
var valueA:String = itemA.test3.text();
var valueB:String = itemB.test3.text();
return ObjectUtil.stringCompare(valueA, valueB);
}
]]>
</mx:Script>
<mx:XML id="tests" source="tests.xml" />
<mx:XMLListCollection id="xmlListColl" source="{tests.test}" />
<mx:ApplicationControlBar dock="true">
<mx:CheckBox id="checkBox" label="descending:" labelPlacement="left" />
<mx:Button label="Sort 'old'"
click="sortOld();" />
<mx:Button label="Sort 'new'"
click="sortNew();" />
<mx:Button label="Reset sort"
click="resetSort();" />
</mx:ApplicationControlBar>
<mx:DataGrid id="dataGrid"
dataProvider="{xmlListColl}"
width="300"
verticalScrollPolicy="on"
sortableColumns="true">
<mx:columns>
<mx:DataGridColumn dataField="old"
labelFunction="dataGridCol_labelFunc"
sortCompareFunction="test2_compareFunc" />
<mx:DataGridColumn dataField="new"
labelFunction="dataGridCol_labelFunc"
sortCompareFunction="test3_compareFunc" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
下面是测试用代码:
Download:
tests.xml
<?xml version="1.0" encoding="utf-8"?>
<tests>
<test name="group1">
<test2 name="old">One (old)</test2>
<test3 name="new">Un (new)</test3>
</test>
<test name="group2">
<test2 name="old">Two (old)</test2>
<test3 name="new">Deux (new)</test3>
</test>
<test name="group8">
<test2 name="old">Eight (old)</test2>
<test3 name="new">Huit (new)</test3>
</test>
</tests>
<tests>
<test name="group1">
<test2 name="old">One (old)</test2>
<test3 name="new">Un (new)</test3>
</test>
<test name="group2">
<test2 name="old">Two (old)</test2>
<test3 name="new">Deux (new)</test3>
</test>
<test name="group8">
<test2 name="old">Eight (old)</test2>
<test3 name="new">Huit (new)</test3>
</test>
</tests>
本文转自:http://blog.minidx.com/2008/04/10/699.html