+展开-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical" width="520" height="440">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.collections.IGroupingCollection;
import mx.collections.GroupingField;
import mx.collections.Grouping;
import mx.collections.GroupingCollection;
[Bindable]
private var generatedData:Array = [];
private var companyNames:Array = ["Adobe", "BEA", "Cosmos",
"Dogma", "Enigma","Fury", "Gama", "Hima", "Indian", "Jaadu",
"Karish", "Linovo", "Micro", "Novice","Oyster", "Puple", "Quag",
"Rendi", "Scrup", "Tempt", "Ubiqut", "Verna", "Wision","Xeno",
"Yoga", "Zeal" ];
private var products:Array = [ "Infuse", "MaxVis", "Fusion",
"Horizon", "Apex", "Zeeta", "Maza", "Orion", "Omega", "Zoota",
"Quata", "Morion" ];
private var countries:Array = [ "India", "USA", "Canada",
"China", "Japan", "France", "Germany", "UK", "Brazil", "Italy",
"Chile", "Bhutan", "Sri Lanka" ];
private var years:Array = ["2000", "2001", "2002", "2003",
"2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011",
"2012", "2013", "2014", "2015", "2016","2017", "2018", "2019",
"2020", "2021", "2022", "2023", "2024"];
private var quarters:Array = ["Q1", "Q2", "Q3", "Q4"];
private var months:Array = ["Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
private var sales:Array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ;
private var costs:Array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ;
private var dimNameMatch:Object = { Company:companyNames,
Product:products, Country:countries, Year:years, Quarter:quarters,
Month:months,Sales:sales,Cost:costs};
private function generateData():void
{
generatedData = [];
var length:int = numRows.value;
var dimNameMap:Object = dimNameMatch;
for (var index:int = 0; index < length; ++index)
{
var newObj:Object = {};
for (var prop:String in dimNameMap)
{
var input:Array = dimNameMap[prop];
var inputIndex:int = Math.random()*input.length;
newObj[prop] = input[inputIndex];
}
generatedData.push(newObj);
}
}
private function populateADG():void
{
if (generatedData.length != numRows.value)
generateData();
adg.dataProvider = generatedData;
}
[Bindable]
private var gc:GroupingCollection;
private function groupData():void
{
var fields:Array = [];
if (company.selected)
fields.push(new GroupingField("Company"));
if (product.selected)
fields.push(new GroupingField("Product"));
if (year.selected)
fields.push(new GroupingField("Year"));
if (fields.length == 0)
{
Alert.show("Select at least one of the items to
group on");
return;
}
gc = new GroupingCollection();
gc.source = generatedData;
gc.grouping = new Grouping();
gc.grouping.fields = fields;
//use async refresh so that we get to see the results early.
gc.refresh(true);
adg.dataProvider = gc;
}
private function handleOptionChange():void
{
//user has not started grouping yet
if (!gc) return;
//stop any refresh that might be going on
gc.cancelRefresh();
var fields:Array = [];
if (company.selected)
fields.push(new GroupingField("Company"));
if (product.selected)
fields.push(new GroupingField("Product"));
if (year.selected)
fields.push(new GroupingField("Year"));
//user might have checked off everything
if (fields.length == 0)
{
return;
}
gc.grouping.fields = fields;
gc.refresh(true);
}
]]>
</mx:Script>
<mx:AdvancedDataGrid id="adg" width="100%" height="260" >
<mx:columns>
<mx:AdvancedDataGridColumn dataField="Company" />
<mx:AdvancedDataGridColumn dataField="Product" />
<mx:AdvancedDataGridColumn dataField="Year" />
<mx:AdvancedDataGridColumn dataField="Sales" />
</mx:columns>
</mx:AdvancedDataGrid>
<mx:HBox>
<mx:NumericStepper id="numRows" stepSize="1000" minimum="1000"
maximum="10000" />
<mx:Button label="Populate ADG" click="populateADG()"
id="populateADGButton"/>
</mx:HBox>
<mx:VBox>
<mx:HBox>
<mx:Label text="Grouping fields:" />
<mx:CheckBox id="company" label="Company" selected="true"
click="handleOptionChange()"/>
<mx:CheckBox id="product" label="Product"
click="handleOptionChange()"/>
<mx:CheckBox id="year" label="Year"
click="handleOptionChange()"/>
</mx:HBox>
<mx:HBox>
<mx:Button label="Group" click="groupData()" />
<mx:Button label="Cancel grouping" click="gc.cancelRefresh()"
enabled="{gc != null}"/>
</mx:HBox>
</mx:VBox>
</mx:Application>