datagrid自动排序 (Sorting DataGrid programmatically)

项目中很多时候会遇datagrid排序的问题,譬如翻页的datagrid,在第一页做了多行排序(multiColumnSort),翻页后的数据也要做这样的排序,怎么办呢?

1.首先找到每个column的排列顺序和sort方向。

2.把所有column按照顺序(升序)和sort方向组装到一个Array中。

3.生成一个sort对象,其datefields 就是这个数组。

4.重新赋Datagrid的数据源,并把原来的sort对象(第三步生成的)赋给此数据源。

5.大功告成!

 

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%">
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.collections.ICollectionView;
			import mx.controls.Label;
			import mx.controls.advancedDataGridClasses.AdvancedDataGridHeaderRenderer;
			import mx.controls.advancedDataGridClasses.AdvancedDataGridItemRenderer;
			import mx.controls.advancedDataGridClasses.SortInfo;
			import mx.core.FlexGlobals;
			import mx.core.UITextField;
			import mx.core.UITextFormat;
			import mx.core.mx_internal;
			import mx.events.AdvancedDataGridEvent;
			
			import spark.collections.Sort;
			import spark.collections.SortField;
			
			[Bindable]
			private var dpFlatOne:ArrayCollection = new ArrayCollection([
				{Region:"Southwest1", Territory:"Arizona1",Territory_Rep:"BTerritory_Rep1", Actual:3, Estimate:40000}, 
				{Region:"Southwest2", Territory:"Arizona2",Territory_Rep:"Territory_Rep2", Actual:29885, Estimate:30000},  
				{Region:"Southwest3", Territory:"Central3",Territory_Rep:"Territory_Rep3", Actual:29134, Estimate:30000},  
				{Region:"Southwest4", Territory:"Nevada4",Territory_Rep:"Territory_Rep4", Actual:1, Estimate:45000},  
				{Region:"Southwest5", Territory:"Northern5",Territory_Rep:"Territory_Rep5", Actual:38805, Estimate:40000}, 
				{Region:"Southwest6", Territory:"Northern6",Territory_Rep:"Territory_Rep6", Actual:55498, Estimate:40000},  
				{Region:"Southwest7", Territory:"Southern7",Territory_Rep:"Territory_Rep7", Actual:44985, Estimate:45000}, 
				{Region:"Southwest8", Territory:"Southern8",Territory_Rep:"Territory_Rep8", Actual:1, Estimate:45000}
			]);
			private var dpFlatTwo:ArrayCollection = new ArrayCollection([
				{Region:"Southwest9", Territory:"Arizona9",Territory_Rep:"Territory_Rep9", Actual:138865, Estimate:40000}, 
				{Region:"Southwest10", Territory:"Arizona10",Territory_Rep:"Territory_Rep10", Actual:229885, Estimate:30000},  
				{Region:"Southwest11", Territory:"Central11",Territory_Rep:"Territory_Rep11", Actual:329134, Estimate:30000},  
				{Region:"Southwest12", Territory:"Nevada12",Territory_Rep:"Territory_Rep12", Actual:452888, Estimate:45000},  
				{Region:"Southwest13", Territory:"Northern13",Territory_Rep:"Territory_Rep13", Actual:538805, Estimate:40000}, 
				{Region:"Southwest14", Territory:"Northern14",Territory_Rep:"Territory_Rep14", Actual:655498, Estimate:40000},  
				{Region:"Southwest15", Territory:"Southern15",Territory_Rep:"Territory_Rep15", Actual:744985, Estimate:45000}, 
				{Region:"Southwest16", Territory:"Southern16",Territory_Rep:"Territory_Rep16", Actual:855555, Estimate:45000}
			]);
			private var sortCount:int = 0;
			protected function programmingSortclickHandler(event:MouseEvent):void
			{
				var sortFiledsArray:ArrayCollection = new ArrayCollection();
				for each(var column:AdvancedDataGridColumn in firstDatagrid.columns)
				{
					var sortInfo:SortInfo = firstDatagrid.getFieldSortInfo(column);
					if (sortInfo != null)
					{
						sortFiledsArray.addItem({sortFiled:new SortField(column.dataField,column.sortDescending),index:sortInfo.sequenceNumber});
					}
				}
				var sort1:Sort = new Sort();
				sort1.fields = [new SortField("index",false,true)];
				sortFiledsArray.sort = sort1;
				sortFiledsArray.refresh();
				
				var sortFields:Array = new Array();
				for each(var sortObj:Object in sortFiledsArray)
				{
					sortFields.push(sortObj.sortFiled);
				}
				var sort:Sort = new Sort();
				sort.fields = sortFields;
				if(sortCount % 2 == 0)
				{
					firstDatagrid.dataProvider = dpFlatTwo;
				}
				else
				{
					firstDatagrid.dataProvider = dpFlatOne;
				}
				(firstDatagrid.dataProvider as ICollectionView).sort = sort;
				sortCount++;
			}
		]]>
	</fx:Script>
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<mx:AdvancedDataGrid id="firstDatagrid"
						 sortableColumns="true"
						 sortExpertMode="true"
						 width="600" height="100%" 
						 dataProvider="{dpFlatOne}">        
		<mx:columns>
			<mx:AdvancedDataGridColumn dataField="Region" itemRenderer="mx.controls.Label"/>
			<mx:AdvancedDataGridColumn dataField="Territory"/>
			<mx:AdvancedDataGridColumn dataField="Territory_Rep"
									   showDataTips="true"
									   headerText="Territory_Rep" itemRenderer="mx.controls.Label"/>
			<mx:AdvancedDataGridColumn dataField="Actual" headerText="asdfadsfasdfasafsdf" dataTipField="Actual" showDataTips="true"/>
			<mx:AdvancedDataGridColumn dataField="Estimate" headerText="asdddddddddddddddddddddddf"/>
		</mx:columns>
	</mx:AdvancedDataGrid>
	<s:Button label="Programming Sort" click="programmingSortclickHandler(event)"/>
</s:Group>
 

 

你可能感兴趣的:(sorting,自动排序)