2011-05-02 75 views
0

我遇到以下問題:我爲我的DataGridColumn實現了一個自定義headRenderer。這個想法是當點擊列標題時有一個下拉菜單。問題是我不知道如何添加一個監聽器或將一個變量傳遞給渲染器,因爲渲染器類由ClassFactory實例化。如何聆聽由Flex中的渲染器調度的自定義事件?

渲染器類:

<?xml version="1.0"?> 
<!-- itemRenderers\dataGrid\myComponents\RendererDGHeader.mxml --> 
<mx:HBox xmlns:mx="library://ns.adobe.com/flex/mx" 
xmlns:fx="http://ns.adobe.com/mxml/2009" 
width = "100%" 
> 
<fx:Script> 


// ... 

// Event handler for the Menu control's change event. 
     private function menuHandler(evt:MenuEvent):void { 
      //Alert.show("Label: " + [email protected], "Clicked menu item"); 

      if ([email protected] == "typeLat"){ 
      typeIcon.load("assets/type_location.jpg"); 
      evt.item.toggled=true; 
      //variable.setColumnType(myLabel.text, "type"); // how can I pass "variable" to this renderer?     
      } else if ([email protected] == "typeLng"){ 
      typeIcon.load("assets/type_location.jpg"); 
      evt.item.toggled=true; 
      } else if ([email protected] == "typeTime"){ 
      typeIcon.load("assets/type_time.png"); 
       evt.item.toggled=true; 
      } 

      else if ([email protected] == "similarity"){ 
       dispatchEvent(new SimilarityEvent("selectSimilarity", null, null)); // how to listen to this event? 
      } 
     } 



</fx:Script> 

<fx:Declarations> 
    <fx:XML id="myMenuData"> 
     <root> 
      <menuitem label="Select type..." eventName="copy"> 
       <menuitem label="Other..." eventName="typeOther"/> 
       <menuitem type="separator"/> 
       <menuitem label="Order" type="check" eventName="typeOther"/> 
       <menuitem label="Range" type="check" eventName="typeOther"/> 
       <menuitem label="Date/Time" type="check" eventName="typeTime"/> 
       <menuitem label="Latitude" type="check" eventName="typeLat"/> 
       <menuitem label="Longitude" type="check" eventName="typeLng"/> 
      </menuitem> 
      <menuitem label="Define similarity..." eventName="similarity"/> 
      <menuitem type="separator"/> 
      <menuitem label="Filter objects..." eventName="similarity"/> 
      <menuitem label="Order" eventName="similarity"> 
       <menuitem label="Ascending" eventName="typeOther"/> 
       <menuitem label="Descending" eventName="typeOther"/> 
      </menuitem> 
      <menuitem type="separator"/> 
      <menuitem label="Remove" eventName="paste"/> 
     </root> 
    </fx:XML> 
</fx:Declarations> 



<mx:Label id="myLabel" click="showMenu();"/> 
<mx:Image height="20" id = "typeIcon"/> 

</mx:HBox> 

這裏是我實例演示器:

  var newColumn:DataGridColumn = new DataGridColumn(); 
      newColumn.dataField = attr; 
      newColumn.headerText = attr; 
      newColumn.sortable = false; 
      newColumn.editable = false; 
      newColumn.draggable = false; 
      newColumn.headerRenderer = new ClassFactory(newviews.RendererDataHeader); 
      cols.push(newColumn); 

任何幫助,將不勝感激

回答

1

添加事件監聽器來控制(數據網格),並事件泡沫,它將被捕獲。

+0

+1。你可以在這裏找到一個例子(http://nwebb.co.uk/blog/?p=16)。 – Constantiner 2011-05-02 17:26:04