Flexviewer两个widget间的如何传值

本例子是通过widgetA的按钮打开widgetB,并把widgetA的某个值传值到widgetB中,代码如下:

widgetA中的代码:

<?xml version="1.0" encoding="utf-8"?>

<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
       xmlns:s="library://ns.adobe.com/flex/spark"
       xmlns:mx="library://ns.adobe.com/flex/mx"
       xmlns:viewer="com.esri.viewer.*"
       widgetConfigLoaded="init()">
 <fx:Script>
  <![CDATA[
   import com.esri.viewer.AppEvent;
   import com.esri.viewer.BaseWidget;
   import com.esri.viewer.IBaseWidget;
   import com.esri.viewer.ViewerContainer;
   
   import mx.collections.ArrayCollection;
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   
   private function init():void
   {
   }
   
   var i:int=1;
   protected function button1_clickHandler(event:MouseEvent):void
   {
    var msArr:ArrayCollection = new ArrayCollection(); 
    msArr.addItem("第"+i.toString()+"次传值"); 
    addSharedData("test", msArr); 
    var id:Number = ViewerContainer.getInstance().widgetManager.getWidgetId("widgetB");
    var bWidget :IBaseWidget = ViewerContainer.getInstance().widgetManager.getWidget(id, true) as IBaseWidget;//打开widgetB
    i++;
   }
  ]]>
 </fx:Script>
 <viewer:WidgetTemplate id="aa"
         width="300" height="300">
  <viewer:layout>
   <s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
  </viewer:layout>
  <s:Button label="测试" click="button1_clickHandler(event)"/>
  
 </viewer:WidgetTemplate>
</viewer:BaseWidget>

widgetB中代码:

<?xml version="1.0" encoding="utf-8"?>

<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
       xmlns:s="library://ns.adobe.com/flex/spark"
       xmlns:mx="library://ns.adobe.com/flex/mx"
       xmlns:viewer="com.esri.viewer.*"
       initialize="myBaseWidgetInitHandler(event)">
 <fx:Script>
  <![CDATA[
   import com.esri.viewer.AppEvent;
   import com.esri.viewer.IBaseWidget;
   import com.esri.viewer.ViewerContainer;
   import mx.collections.ArrayCollection;
   
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   import com.esri.viewer.utils.Hashtable;
   
   protected function myBaseWidgetInitHandler(event:FlexEvent):void
   {
    AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdatedFrist);//widgetA第一次点击触发
    AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdatedSecond);//widgetA第二次点击后触发
    fetchSharedData();
   }

   /***widgetA第一次点击触发的事件***/
   private function sharedDataUpdatedFrist(event:AppEvent):void 
   {
    var dataTable:Hashtable = event.data as Hashtable; 
    if (dataTable.containsKey("test")) 
    {
     var recAC:ArrayCollection = dataTable.find("test") as ArrayCollection;
     if (recAC[0]){
      lbl.text=recAC[0].toString();
     }
    }
   }
   
   /***widgetA第二次点击后触发的事件***/
   private function sharedDataUpdatedSecond(event:AppEvent):void 
   {
    var data:Object = event.data;
    if (data.key == "test")
    {
     var obj:Object = data.collection[0];
     var arr:ArrayCollection = new ArrayCollection(); 
     arr.addItem(obj);
     var str:String=arr.getItemAt(0).toString();
     this.lbl.text=str;
    }
   }
  ]]>
 </fx:Script>
 <viewer:WidgetTemplate id="bb"
         width="300" height="300">
  <viewer:layout>
   <s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
  </viewer:layout>
  <s:Label id="lbl" text="标签"/>
 </viewer:WidgetTemplate>
</viewer:BaseWidget>

运行结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值