关于数组对象
数组对象包含了一个数据对象,比如一个array或者xmllist对象,并且提供了一系列方法使我们可以访问,排序,过滤和更改数据对象里面的item。值得说一下的是当数组对象充当一些组件的数据源的时候,array会自动被转成ArrayCollection对象,当使用xml或者xmllist的时候,会被自动转化成为xmllistcollection对象。
关于数据提供者组件。所有基于列表组件的都被称为数据提供者组件。它们都有一个dataprovider属性,由ArrayCollection或者XMLListCollection对象赋值。下面的组件均有dataProvider属性:
- ButtonBar
- ColorPicker
- ComboBox
- DataGrid
- DateField
- HorizontalList
- LinkBar
- List
- Menu
- MenuBar
- PopUpMenuButton
- Repeater
- TabBar
- TileList
- ToggleButtonBar
- Tree
关于array和arrayCollection哪个充当数据源更好呢?一般来说,数据如果动态变化的话,用于后者优于前者。
<?xml version="1.0"?>
<!-- dpcontrols/DataGridValidateNowSelindex.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="initData();">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var DGArray:ArrayCollection = new ArrayCollection([
{Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99},
{Artist:'Pavement', Album:'Brighten the Corners', Price:11.99}]);
// Initialize initDG ArrayCollection variable from the ArrayCollection.
public function initData():void {
myGrid.dataProvider = DGArray;
myGrid.validateNow();
myGrid.selectedIndex=1;
}
]]>
</mx:Script>
<mx:DataGrid id="myGrid">
<mx:columns>
<mx:DataGridColumn dataField="Album"/>
<mx:DataGridColumn dataField="Price"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
下面的例子增加了排序的特点
<?xml version="1.0"?>
<!-- dpcontrols/SimpleDP.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="600" initialize="sortAC()">
<mx:Script>
<![CDATA[
import mx.collections.*;
// Function to sort the ArrayCollection in descending order.
public function sortAC():void {
var sortA:Sort = new Sort();
sortA.fields=[new SortField("label")];
myAC.sort=sortA;
//Refresh the collection view to show the sort.
myAC.refresh();
}
// Function to add an item in the ArrayCollection.
// Data added to the view is also added to the underlying Array.
// The ArrayCollection must be sorted for this to work.
public function addItemToMyAC():void {
myAC.addItem({label:"MD", data:"Annapolis"});
}
]]>
</mx:Script>
<!-- An ArrayCollection with an array of objects -->
<mx:ArrayCollection id="myAC">
<!-- Use an mx:Array tag to associate an id with the array. -->
<mx:Array id="myArray">
<mx:Object label="MI" data="Lansing"/>
<mx:Object label="MO" data="Jefferson City"/>
<mx:Object label="MA" data="Boston"/>
<mx:Object label="MT" data="Helena"/>
<mx:Object label="ME" data="Augusta"/>
<mx:Object label="MS" data="Jackson"/>
<mx:Object label="MN" data="Saint Paul"/>
</mx:Array>
</mx:ArrayCollection>
<mx:HBox width="100%">
<!-- A ComboBox populated by the underlying Array object.
This control shows that Array retains its original order
and MD is inserted at the end of the Array. -->
<mx:ComboBox id="cb2" rowCount="10" dataProvider="{myArray}"/>
<!-- A ComboBox populated by the collection view of the Array. -->
<mx:ComboBox id="cb1" rowCount="10" dataProvider="{myAC}"/>
<mx:Button id="b1" label="Add MD" click="addItemToMyAC();"/>
</mx:HBox>
</mx:Application>
关于数组的排序主要使用了排序类Sort,以及内置的更新方法refresh。