/ Published in: MXML
- How to sort an ArrayCollection
- How to add an XML Item or an Data Item
- Changing from an ArrayCollection to an XMLListCollection
Expand |
Embed | Plain Text
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="600" height="400"> <!-- A. Collection Classes You may have data from multiple sources, from multiple formats these classes are meant to provide a single API for using/manipulating data no matter that the data is 1. Collection Interfaces (Programming Interface) a. Brief Explination 1. An interface is like a Class but with no implementation 2. An interface is a layer of abstraction above a class b. ICollectionView Interface 1.Contains the definitions of sorting and filtering interfaces c. IList Interface 1. Provides methods for adding, removing, or searching for items in your data 2. Sorting a Collection (Cheat Sheet) a. Class Sort contains the methods on how our visual displa of item/data will be sorted/changed b. Within the Sort Class (Sort.fields) is an Array of SortFields 1. A SortField is one definition of how to sort your data SortField( name, caseSensitivity:Boolean=true, Descending:Boolean=true, numeric:Boolean=true ) 2. Sort.fields = Array of SortFields 3. collection.sort = A Sort Object, which contains an array of Fields 4. collection.refresh() => Which will change the visual display of the collection 3. Adding Items i. addItem() ii. addItemAt() iii. setItemAt() iv. removeAll() v. removeItemAt() vi. getItemIndex() vii. getItemAt() 4. Swapping Collections a. --> <mx:Script> <![CDATA[ import mx.collections.XMLListCollection; import mx.collections.IList; import mx.collections.SortField; import mx.collections.Sort; import mx.collections.ICollectionView; import mx.collections.ArrayCollection; [Bindable] private var myAC:ArrayCollection = new ArrayCollection([ { firstName: 'Patrick', lastName: 'Reynolds', ID:0 }, { firstName: 'Captain', lastName: 'Schmo', ID:1 }, { firstName: 'Willy', lastName: 'Wonka', ID:2 }, { firstName: 'Ned', lastName: 'Flanders', ID:3 } ]); [Bindable] public var xml:XML = <names> <object> <firstName>Patrick</firstName> <lastName>Reynolds</lastName> <id>0</id> </object> <object> <firstName>Captain</firstName> <lastName>Schmo</lastName> <id>1</id> </object> <object> <firstName>Willy</firstName> <lastName>Wonka</lastName> <id>2</id> </object> <object> <firstName>Ned</firstName> <lastName>Flanders</lastName> <id>3</id> </object> </names>; [Bindable] private var xmlListCollection:XMLListCollection = new XMLListCollection( new XMLList( xml.object ) ); //ICollectionView is an interface implemented by our Collection Classes private function sort( collection:ICollectionView ):void { //Sort Class will display data visually var sort:Sort = new Sort(); //Within Sort is an Array of SortFields sort.fields = new Array( new SortField('lastName', true) );; //Taking the collection we pass in and setting a sort value on it collection.sort = sort; //Refresh the View collection.refresh(); } private function add( collection:IList, item:Object ):void { collection.addItem( item ); } private function addAC():void { add( dataGrid.dataProvider as IList, { firstName: 'Matt', lastName: 'Chotin', ID:0 } ); } private function addXML():void { var xmlObject:XMLList = new XMLList( <object> <firstName>chris</firstName> <lastName>aiv</lastName> <id>6</id> </object> ); add( dataGrid.dataProvider as IList, xmlObject ); } ]]> </mx:Script> <mx:HBox> <mx:DataGrid id="dataGrid" dataProvider="{myAC}" width="400" height="400"> <mx:columns> <mx:DataGridColumn dataField="firstName" /> <mx:DataGridColumn dataField="lastName" /> </mx:columns> </mx:DataGrid> <!-- DataProvider is List-Based Classes is simply a Generic Object so you should Cast it as an ICollectionView --> <mx:VBox> <mx:Button click="sort(dataGrid.dataProvider as ICollectionView)" label="Sort Data Provider" /> <mx:Button click="addAC()" label="Add Object" /> <mx:Button click="addXML()" label="Add XML" /> <mx:Button click="dataGrid.dataProvider = xmlListCollection" label="Set to XMLListCollection DB" /> <mx:Button click="dataGrid.dataProvider = myAC" label="Set to ArrayCollection DB" /> </mx:VBox> </mx:HBox> </mx:Application>
You need to login to post a comment.
