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

flex 中子窗口通过事件刷新父窗口中的数据

发布时间:2020-12-15 01:15:02 所属栏目:百科 来源:网络整理
导读:经常听到有人问子窗口操作完成之后,要刷新父窗口的数据怎么办??有人用过parentDocument; 有人使用过在子窗口中 new 一个父窗口的对象,然后通过这个对象访问父窗口的方法或属性; 。。。。。 第一种,也可以实现。但是第二种就没法理解了,严格来说子窗
经常听到有人问子窗口操作完成之后,要刷新父窗口的数据怎么办??有人用过parentDocument;
有人使用过在子窗口中new一个父窗口的对象,然后通过这个对象访问父窗口的方法或属性;
。。。。。

第一种,也可以实现。但是第二种就没法理解了,严格来说子窗口是不能new父窗口的实例的。。。。最好的方法是用事件,今天就弄一个事件的例子吧,大牛们可以不看。新童鞋务必看看,有帮助的。。。。附件可直接下载

运行页面如下


点击新增

点击保存


代码如下:
总共3个文件,一个名为Parent的application,一个名为Child的titleWindow,还有一个是自定义的一个事件。
Parent.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"?
  3. ? ?? ?? ?? ?? ?? ?? ?? ?? ?xmlns:s="library://ns.adobe.com/flex/spark"?
  4. ? ?? ?? ?? ?? ?? ?? ?? ?? ?xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
  5. ? ?? ???<fx:Script>
  6. ? ?? ?? ?? ?? ? <![CDATA[
  7. ? ?? ?? ?? ?? ?? ?? ?? ?import mx.collections.ArrayCollection;
  8. ? ?? ?? ?? ?? ?? ?? ?? ?import mx.managers.PopUpManager;
  9. ? ?? ?? ?? ?? ?? ?? ?? ?
  10. ? ?? ?? ?? ?? ?? ?? ?? ?[Bindable]
  11. ? ?? ?? ?? ?? ?? ?? ?? ?public var ac:ArrayCollection=new ArrayCollection([
  12. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"周结",sex:"男",age:"23"},
  13. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"小静",sex:"女",age:"19"},serif; font-size:12px; line-height:1.8em"> ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"小二",age:"20"},serif; font-size:12px; line-height:1.8em"> ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"大牛",age:"44"}
  14. ? ?? ?? ?? ?? ?? ?? ?? ?]);?
  15. ? ?? ?? ?? ?? ?? ?? ?? ?private var child:Child = new Child();
  16. ? ?? ?? ?? ?? ?? ?? ?? ?protected function button1_clickHandler(event:MouseEvent):void
  17. ? ?? ?? ?? ?? ?? ?? ?? ?{
  18. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???PopUpManager.addPopUp(child,this,true);
  19. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???PopUpManager.centerPopUp(child);
  20. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???child.addEventListener(MyEvent.SAVE_OK,saveSuccess);
  21. ? ?? ?? ?? ?? ?? ?? ?? ?}
  22. ? ?? ?? ?? ?? ?? ?? ?? ?private function saveSuccess(e:MyEvent):void
  23. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???ac.addItem(e.data);
  24. ? ?? ?? ?? ?? ? ]]>
  25. ? ?? ???</fx:Script>

  26. ? ?? ???<fx:Declarations>
  27. ? ?? ?? ?? ?? ? <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  28. ? ?? ???</fx:Declarations>
  29. ? ?? ???
  30. ? ?? ???<mx:AdvancedDataGrid id="adg1" designViewDataType="flat" dataProvider="{ac}" width="455" height="240" x="307" y="79">
  31. ? ?? ?? ?? ?? ? <mx:columns>
  32. ? ?? ?? ?? ?? ?? ?? ?? ?<mx:AdvancedDataGridColumn headerText="姓名" dataField="name"/>
  33. ? ?? ?? ?? ?? ?? ?? ?? ?<mx:AdvancedDataGridColumn headerText="年龄" dataField="age"/>
  34. ? ?? ?? ?? ?? ?? ?? ?? ?<mx:AdvancedDataGridColumn headerText="性别" dataField="sex"/>
  35. ? ?? ?? ?? ?? ? </mx:columns>
  36. ? ?? ???</mx:AdvancedDataGrid>
  37. ? ?? ???<s:Button label="新增一条" click="button1_clickHandler(event)" x="518" y="365"/>
  38. </s:Application>
复制代码
Child.mxml
  1. <s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"?
  2. ? ?? ?? ?? ?? ?? ?? ?? ?? ?xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
  3. ? ?? ???<s:layout>
  4. ? ?? ?? ?? ?? ? <s:BasicLayout/>
  5. ? ?? ???</s:layout>
  6. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???var obj:Object = new Object();
  7. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???obj.name = dname.text;
  8. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???obj.age = age.text;
  9. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???obj.sex = sex.text;
  10. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???this.dispatchEvent(new MyEvent(MyEvent.SAVE_OK,obj));
  11. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???PopUpManager.removePopUp(this);
  12. ? ?? ???<s:TextInput x="135" y="45" id="dname"/>
  13. ? ?? ???<s:TextInput x="135" y="83" id="age"/>
  14. ? ?? ???<s:TextInput x="135" y="127" id="sex"/>
  15. ? ?? ???<s:Button x="164" y="196" label="保存" click="button1_clickHandler(event)"/>
  16. </s:TitleWindow>
复制代码
MyEvent.as
  1. package event
  2. {
  3. ? ?? ???import flash.events.Event;
  4. ? ?? ???public class MyEvent extends Event
  5. ? ?? ???{
  6. ? ?? ?? ?? ?? ? public static const SAVE_OK:String="saveOk";
  7. ? ?? ?? ?? ?? ? private var _data:Object;
  8. ? ?? ?? ?? ?? ? public function MyEvent(type:String,data:Object=null,bubbles:Boolean=false,cancelable:Boolean=false)
  9. ? ?? ?? ?? ?? ? {
  10. ? ?? ?? ?? ?? ?? ?? ?? ?this._data=data;
  11. ? ?? ?? ?? ?? ?? ?? ?? ?super(type,bubbles,cancelable);
  12. ? ?? ?? ?? ?? ? }
  13. ? ?? ?? ?? ?? ? public function get data():Object
  14. ? ?? ?? ?? ?? ?? ?? ?? ?return _data;
  15. ? ?? ?? ?? ?? ? public function set data(value:Object):void
  16. ? ?? ?? ?? ?? ?? ?? ?? ?_data = value;
  17. ? ?? ???}
  18. }
复制代码
转自:http://www.ityangba.com/thread-302-1-1.html

(编辑:李大同)

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

    推荐文章
      热点阅读