加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

CollectionEvent对象监听Collection类的修改

发布时间:2020-12-15 01:20:37 所属栏目:百科 来源:网络整理
导读:在Flex中,Collection类实现了IList接口,这个接口提供一些方法(adding,removing,updating)来修改集合中的元素。可以使用IList接口的方法和属性在ArrayCollection类,XMLList类,和标准Flex控件的dataProvider 属性上。可以使用IList的addItem(),removeItem(),

在Flex中,Collection类实现了IList接口,这个接口提供一些方法(adding,removing,updating)来修改集合中的元素。可以使用IList接口的方法和属性在ArrayCollection类,XMLList类,和标准Flex控件的dataProvider 属性上。可以使用IList的addItem(),removeItem(),和setItemAt() 方法分别增加,删除和更新元素数据。addItemAt() and removeItemAt() methods,和the setItemAt()方法提供第二个参数,下标位置,来指定要在集合中影响的位置。IList接口的length属性返回集合中元素的数量。?



Flex的集合机制也包括描述数据改变的事件。实现了IList 或者 ICollectionView 接口的类,无论何时数据发生改变,都分发CollectionEvent类事件所有集合时间都包含类型属性值CollectionEvent.COLLECTION_CHANGE。?

CollectionEvent对象有kind属性标志着集合被改变的方式。通过kind属性与CollectionEventKind的常量的对比,你可以测试集合所发生的改变。主要的常量包括ADD,REMOVE和 UPDATE。?


CollectionEvent对象包含一个items属性这个属性是一个对象的数组,这个数组的类型依赖于对象分发的事件的类型。对于ADD和REMOVE时间,这个数组包含added和removed数组。对于UPDATE事件,这个items属性包含PropertyChangeEvent事件对象数组。这些对象的属性显示出改变的类型和属性改变之前和之后的值。例如,PropertyChangeEvent类的kind属性显示出属性被改变的方式;你可以测试改变的类型通过把kind属性与PropertyChangeEventKind的常量UPDATE或DELETE.?


下边的例子监听DataGrid的改变事件,来创建一个概览——详细关系。在这个关系中,选择一个DataGrid中的一行后,数据会显示在几个form控件中,然后你就可以编辑数据了。(使用概览——详细关系可以使DataGrid控件具有可编辑功能)。通过IList接口的addItem(),and setItemAt()方法,可以对DataGrid中的数据增加,删除,修改。这个例子也监听ArrayCollection上的collectionChange时间保持对数据增删改的日志记录。
?

Java代码??

收藏代码

  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"?layout="absolute">??
  3. ???<mx:Script>??
  4. ????<![CDATA[??
  5. ????????import?mx.events.*;??
  6. import?mx.collections.*;??
  7. public?function?collectionEventHandler(event:CollectionEvent):void???
  8. ????????{??
  9. ????????????switch(event.kind)??
  10. ????????????{??
  11. ????????????????case?CollectionEventKind.ADD:??
  12. ?????????????????addLog("item"+event.location?+"ADD");??
  13. ?????????????????break;??
  14. case??CollectionEventKind.REMOVE:??
  15. ?????????????????addLog("item"+event.location?+"REMOVE");??
  16. case??CollectionEventKind.UPDATE:??
  17. ?????????????????addLog("item"+event.location?+"UPDATE");??
  18. case??CollectionEventKind.REPLACE:??
  19. ?????????????????addLog("item"+event.location?+"REPLACE");??
  20. ????????????}??
  21. ????????}??
  22. public?function?addLog(str:String): ????????????????log.text?+=?str?+?"n";??
  23. ????????????}??????????????
  24. public?function?addPerson(): ????????????????ac.addItem({first:firstInput.text,??
  25. ????????????????????????????last:lastInput.text,250); line-height:18px"> ????????????????????????????email:emailInput.text});??
  26. ????????????????clearInputs();??
  27. public?function?removePerson():void?{??
  28. if(dg.selectedIndex?>=?0){??
  29. ????????????????????ac.removeItemAt(dg.selectedIndex);??
  30. ????????????????}??
  31. public?function?updatePerson():if(dg.selectedItem?!==?null)??
  32. ????????????????{??
  33. ????????????????????ac.setItemAt({first:firstInput.text,?last:lastInput.text,250); line-height:18px"> ????????????????????????email:emailInput.text},?dg.selectedIndex);??
  34. public?function?dgChangeHandler(): ??
  35. ????????????????firstInput.text?=?dg.selectedItem.first;??
  36. ????????????????lastInput.text?=?dg.selectedItem.last;??
  37. ????????????????emailInput.text?=?dg.selectedItem.email;??
  38. public?function?clearInputs(): ????????????????firstInput.text?=?"";??
  39. ????????????????lastInput.text?=?"";??
  40. ????????????????emailInput.text?=?"";??
  41. ????]]>??
  42. ???</mx:Script>??
  43. ????<mx:ArrayCollection?id="ac"??
  44. ????????????collectionChange="collectionEventHandler(event)">??
  45. ????????<mx:source>??
  46. ????????????<mx:Object?first="Matt"?last="Matthews"?email="matt@myco.com"/>??
  47. ????????????<mx:Object?first="Sue"?last="Sanderson"?email="sue@myco.com"/>??
  48. ????????????<mx:Object?first="Harry"?last="Harrison"?email="harry@myco.com"/>??
  49. ????????</mx:source>??
  50. ????</mx:ArrayCollection>??
  51. ????<mx:Panel?title="Master-Detail?View"?width="100%">??????????
  52. ????????<mx:DataGrid?width="100%"?id="dg"?dataProvider="{ac}"??
  53. ????????????????change="dgChangeHandler()">??
  54. ????????????<mx:columns>??
  55. ????????????????<mx:DataGridColumn?dataField="first"?headerText="First?Name"/>??
  56. ????????????????<mx:DataGridColumn?dataField="last"?headerText="Last?Name"/>??
  57. ????????????????<mx:DataGridColumn?dataField="email"?headerText="Email"/>???
  58. ????????????</mx:columns>??
  59. ????????</mx:DataGrid>??
  60. ????????<mx:Form?label="test"?width="100%">??
  61. ???????????<mx:FormItem?label="First?Name"?width="100%">??
  62. ????????????????<mx:TextInput?id="firstInput"?width="100%"/>??
  63. ???????????</mx:FormItem>??
  64. ???????????<mx:FormItem?label="Last?Name"?width="100%">??
  65. ????????????????<mx:TextInput?id="lastInput"?width="100%"/>??
  66. ???????????<mx:FormItem?label="Email"?width="100%">??
  67. ????????????????<mx:TextInput?id="emailInput"?width="100%"/>??
  68. ????????</mx:Form>??????
  69. ????????<mx:ControlBar?horizontalAlign="center">??
  70. ????????????<mx:Button?label="Add?New"?click="addPerson()"/>??
  71. ????????????<mx:Button?label="Update?Selected"?click="updatePerson()"/>??
  72. ????????????<mx:Button?label="Remove?Selected"?click="removePerson()"/>??
  73. ????????????<mx:Button?label="Clear"?click="clearInputs()"/>??
  74. ????????</mx:ControlBar>??
  75. ????</mx:Panel>??
  76. ????<mx:Panel?title="Change?log"?width="100%"?height="125"?y="333">??????????
  77. ????????<mx:TextArea?id="log"?width="100%"?height="100%"/>??
  78. ????</mx:Panel>????
  79. </mx:Application>??

from:http://www.iteye.com/topic/607521

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读