2011-07-18 22 views
0

我使用一個DataGrid其中有包含圖像的的itemRenderer:從ItemRenderer的組件內部調用clickHandler事件(圖)

protected static function hbox1_clickHandler(event:MouseEvent):void 
     { 
      //some action 
     } 


<mx:DataGrid id="reportDG" dataProvider="{CDODReportsModel.instance.reportDataArrayCollectionObject}" color="black" horizontalScrollPolicy="on"> 
    <mx:columns>       
     <mx:DataGridColumn headerText="info"> 
      <mx:itemRenderer> 
       <fx:Component> 
        <mx:HBox horizontalAlign="center"> 
           <mx:Image source="assets/images/i_info.png" scaleX="0.6" scaleY="0.6" click="hbox1_clickHandler(event)"/> 
        </mx:HBox> 
       </fx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 
     <mx:DataGridColumn dataField="NAME" headerText="NAME"/>       
     <mx:DataGridColumn dataField="TOTAL" headerText="TOTAL"/> 

    </mx:columns> 

</mx:DataGrid> 

我想派遣上點擊一個事件,所以當我點擊圖像我做了一個動作。但是,這樣做會給我一個錯誤。我做了一些搜索和建議的答案是使用outerDocument和ParentDoecument ..都沒有工作。

如何訪問點擊處理函數(hbox1_clickHandler()在我的代碼中)?

回答

3

我不認爲這是可能的,如果你在應用程序類。 如果這是其他一些類,比你可以賴特是這樣的:

click="MyClass.hbox1_clickHandler()" 

而且這不是萊特內聯項目渲染最好的主意。最好的方法是擴展基礎項目渲染器並製作自己的渲染器。你也可以擴展Flash Event類並製作你自己的。這樣做可以爲您的事件發送一些額外的數據。

但無論如何,你使用的方法的代碼將是這樣的:

protected function reportDG_initializeHandler(event:FlexEvent):void 
{ 
    reportDG.addEventListener("clicked", hbox1_clickHandler); 
    function hbox1_clickHandler(event:Event):void 
    { 
    //some action 
    } 
} 

<mx:DataGrid initialize="reportDG_initializeHandler(event)"> 
    <mx:columns> 
     <mx:DataGridColumn> 
      <mx:itemRenderer> 
       <fx:Component> 
        <mx:HBox> 
         <mx:Image click="dispatchEvent(new Event('clicked', true))"/> 
        </mx:HBox> 
       </fx:Component> 
      </mx:itemRenderer> 
     </mx:DataGridColumn> 
    </mx:columns> 
</mx:DataGrid> 
+0

+1「最好的辦法是擴大基地項目渲染器,讓你自己一個人」,本來要表明它太 – Ryan

2

爲函數聲明爲「公共」它將與outerDocument.functionName只要工作。在你的情況下,如果你將你的函數聲明從保護變爲公共,它將起作用。下面是示例代碼示例[工作]:

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"> 
    <s:layout> 
     <s:VerticalLayout/> 
    </s:layout> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 
      import mx.controls.Alert; 
      public function hbox1_clickHandler(event:MouseEvent):void 
      { 
       Alert.show("ite works"); 
      } 
     ]]> 
    </fx:Script> 
    <mx:DataGrid id="reportDG" dataProvider="{new ArrayCollection(['A','B'])}" color="black" horizontalScrollPolicy="on"> 
     <mx:columns>       
      <mx:DataGridColumn headerText="info"> 
       <mx:itemRenderer> 
        <fx:Component> 
         <mx:HBox horizontalAlign="center"> 
          <mx:Button label="Button" click="outerDocument.hbox1_clickHandler(event)"/> 
         </mx:HBox> 
        </fx:Component> 
       </mx:itemRenderer> 
      </mx:DataGridColumn> 
      <mx:DataGridColumn dataField="NAME" headerText="NAME"/>       
      <mx:DataGridColumn dataField="TOTAL" headerText="TOTAL"/> 

     </mx:columns> 

    </mx:DataGrid> 
</s:WindowedApplication>