为ArrayCollection添加排序和获取数据
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <!--[CDATA[ import mx.controls.AdvancedDataGrid; import mx.controls.Alert; import mx.messaging.AbstractConsumer; import mx.collections.Sort; import mx.collections.SortField; import mx.collections.ArrayCollection; private var coll:ArrayCollection=new ArrayCollection( [{name:"Tom",age:25}, {name:"Jim",age:15}, {name:"Lily",age:23} ]);//创建一个集合 public function init():void//初始化数据,绑定 { addItem();//调用添加函数 a.rowCount=coll.length; a.dataProvider=coll;//为DataGrid绑定数据源 } public function addItem():void//为集合添加元素 { var a:Object=new Object();//建立一个对象,插入到集合中 a.name=t_name.text; a.age=t_age.text; coll.addItemAt(a,0);//a为插入的元素,0为插入的位置,也就是集合的开头 } public function checkItem():void//查找数据 { var sort:Sort=new Sort();//建立SORT对象 var index:int;//用于显示集合索引号 sort.fields=[new SortField("age",true)];//按AGE进行排序 coll.sort=sort; coll.refresh(); index=sort.findItem(coll.source,{name:t_name1.text,age:t_age1.text},Sort.ANY_INDEX_MODE); mx.controls.Alert.show("您找到的是"+index); } ]]--> </mx:Script> <mx:Canvas width="464" height="329" horizontalCenter="0" verticalCenter="0"> <mx:DataGrid x="0" y="0" id="a" width="100%"> <mx:columns> <mx:DataGridColumn headerText="name" dataField="name"/> <mx:DataGridColumn headerText="age" dataField="age"/> </mx:columns> </mx:DataGrid> <mx:Button label="添加" click="init()" bottom="10" left="97"/> <mx:TextInput x="47" y="237" id="t_name"/> <mx:TextInput x="47" y="267" id="t_age"/> <mx:Button label="查找" bottom="10" left="344" click="checkItem()"/> <mx:TextInput x="294" y="237" id="t_name1"/> <mx:TextInput x="294" y="267" id="t_age1"/> <mx:Text x="247" y="239" text="name:"/> <mx:Text x="247" y="269" text="age:"/> <mx:Text x="0" y="239" text="name:"/> <mx:Text x="0" y="269" text="age:"/> </mx:Canvas> </mx:Application>