Flex中如何在ArrayCollection创建一个可视化的游标(cursor)的例子

接下来的例子演示了如何在ArrayCollection中,创建一个可视化的游标(cursor)用来查找一个特别的项。 例子很简单,注意区分大小写。
让我们先来看一下Demo(可以右键View Source或 点击这里察看源代码 ):
Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white"
  6.         creationComplete="init();">
  7.     <mx:Array id="arr">
  8.         <mx:String>One</mx:String>
  9.         <mx:String>Two</mx:String>
  10.         <mx:String>Three</mx:String>
  11.         <mx:String>Four</mx:String>
  12.         <mx:String>Five</mx:String>
  13.     </mx:Array>
  14.     <mx:ArrayCollection id="arrColl" source="{arr}" />
  15.     <mx:Script>
  16.         <![CDATA[
  17.             import mx.collections.IViewCursor;
  18.             import mx.collections.Sort;
  19.             import mx.collections.SortField;
  20.             [Embed("assets/accept.png")]
  21.             public var acceptIcon:Class;
  22.             [Embed("assets/exclamation.png")]
  23.             public var exclamationIcon:Class;
  24.             private var cursor:IViewCursor;
  25.             private function init():void {
  26.                 var sortField:SortField = new SortField(null, true);
  27.                 var sort:Sort = new Sort();
  28.                 sort.fields = [sortField];
  29.                 arrColl.sort = sort;
  30.                 arrColl.refresh();
  31.                 cursor = arrColl.createCursor();
  32.             }
  33.             private function button_click(evt:MouseEvent):void {
  34.                 var found:Boolean = cursor.findAny(textInput.text);
  35.                 if (found) {
  36.                     img.source = acceptIcon;
  37.                     list.selectedItem = cursor.current;
  38.                 } else {
  39.                     img.source = exclamationIcon;
  40.                     list.selectedItem = null;
  41.                 }
  42.             }
  43.         ]]>
  44.     </mx:Script>
  45.     <mx:ApplicationControlBar dock="true">
  46.         <mx:Canvas>
  47.             <mx:TextInput id="textInput" />
  48.             <mx:Image id="img" right="3" verticalCenter="0" />
  49.         </mx:Canvas>
  50.         <mx:Button id="button"
  51.                 label="Find"
  52.                 click="button_click(event);" />
  53.     </mx:ApplicationControlBar>
  54.     <mx:List id="list"
  55.             dataProvider="{arrColl}"
  56.             width="100"
  57.             rowCount="{arrColl.length}" />
  58. </mx:Application>

你可能感兴趣的:(职场,休闲)