Will Flex be the engine of economic recovery?
Labels: flash flex
Labels: flash flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.IList;
import mx.collections.SortField;
import mx.collections.Sort;
import mx.events.CollectionEvent;
import mx.collections.ArrayCollection;
import mx.collections.ICollectionView;
[Bindable]
private var underlyingList : IList = new ArrayCollection(
[
{name: "Graeme", age: 47},
{name: "Jane", age: 19},
{name: "Mary", age:30}
]);
private var itemIndex : int = 0;
private function onAddButtonClick():void
{
var newNames : Array =
[
{name:"Harry", age:70},
{name:"George", age:71},
{name:"Germain", age:65},
{name:"Vanessa", age:21}
];
this.underlyingList.addItem(newNames[itemIndex++]);
}
private function onSortByAgeButtonClick(collection : ICollectionView, descending:Boolean):void
{
var sort : Sort = new Sort();
sort.fields = [new SortField("age", true, descending)];
collection.sort = sort;
collection.refresh();
}
private function onSortByNameButtonClick(collection : ICollectionView, descending:Boolean):void
{
var sort : Sort = new Sort();
sort.fields = [new SortField("name", true, descending)];
collection.sort = sort;
collection.refresh();
}
private function filterByGenderFunction(person:Object):Boolean
{
switch (person.name)
{
case "Harry":
case "George":
case "Graeme":
return true;
case "Vanessa":
case "Germain":
case "Jane":
case "Mary":
return false;
default:
throw new Error("unrecognized name");
}
}
private function onFilterByGenderButtonClick(collection : ICollectionView, on:Boolean):void
{
if (on)
{
collection.filterFunction = this.filterByGenderFunction;
}
else
{
collection.filterFunction = null;
}
collection.refresh();
}
]]>
</mx:Script>
<mx:ListCollectionView id="firstCollection" list="{this.underlyingList}"/>
<mx:ListCollectionView id="secondCollection" list="{this.underlyingList}"/>
<mx:DataGrid dataProvider="{this.underlyingList}"/>
<mx:Button label="Add" click="this.onAddButtonClick()"/>
<mx:HBox>
<mx:VBox>
<mx:DataGrid dataProvider="{this.firstCollection}"/>
<mx:Button label="Sort by age (ASC)" click="this.onSortByAgeButtonClick(this.firstCollection, false)"/>
<mx:Button label="Sort by age (DESC)" click="this.onSortByAgeButtonClick(this.firstCollection, true)"/>
<mx:Button label="Sort by name (ASC)" click="this.onSortByNameButtonClick(this.firstCollection, false)"/>
<mx:Button label="Filter by gender" click="this.onFilterByGenderButtonClick(this.firstCollection, true)"/>
<mx:Button label="Unfilter by gender" click="this.onFilterByGenderButtonClick(this.firstCollection, false)"/>
</mx:VBox>
<mx:VBox>
<mx:DataGrid dataProvider="{this.secondCollection}"/>
<mx:Button label="Sort by age (ASC)" click="this.onSortByAgeButtonClick(this.secondCollection, false)"/>
<mx:Button label="Sort by age (DESC)" click="this.onSortByAgeButtonClick(this.secondCollection, true)"/>
<mx:Button label="Sort by name (ASC)" click="this.onSortByNameButtonClick(this.secondCollection, false)"/>
<mx:Button label="Filter by gender" click="this.onFilterByGenderButtonClick(this.secondCollection, true)"/>
<mx:Button label="Unfilter by gender" click="this.onFilterByGenderButtonClick(this.secondCollection, false)"/>
</mx:VBox>
</mx:HBox>
</mx:Application>
Labels: design patterns, flex, presentation model