3 Replies Latest reply on Jul 23, 2011 3:38 AM by kokorito

    Event dispatching from item renderer in datagrid

    asifdegr8

      I have to create a custom item renderer for my datagrid that contains a button control and dispatch the button click event. Its doesn't work for me. The code for main application file and custom item renderer is given.

       

      ******** MainApp.mxml ********

       

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

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

         xmlns:s="library://ns.adobe.com/flex/spark"

         xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

         >

      <fx:Script>

      <![CDATA[

      import mx.controls.Alert;

      import mx.events.ListEvent;

       

      protected function itemRendererButtonClickHandler():void

      {

      Alert.show("button is clicked and event is listened in main application ");

      }

       

      ]]>

      </fx:Script>

       

      <fx:Declarations>

      <fx:Array id="arr">

      <fx:Object label="one" />

      <fx:Object label="two" />

      <fx:Object label="three" />

      <fx:Object label="four" />

      <fx:Object label="five" />

      </fx:Array>

       

      <s:ArrayCollection id="arrColl1" source="{arr}" />

      <s:ArrayCollection id="arrColl2" />

      </fx:Declarations>

       

      <mx:DataGrid id="dataGrid1" x="51" y="28" dataProvider="{arrColl1}" creationComplete="dataGrid1.addEventListener('buttonClicked', itemRendererButtonClickHandler);"  >

      <mx:columns>

      <mx:DataGridColumn dataField="label" headerText="Label"/>

      <mx:DataGridColumn  headerText="Add" itemRenderer="buttonRenderer"/>

      </mx:columns>

      </mx:DataGrid>

      </s:Application>

      ******** buttonRenderer.mxml ***********
      <?xml version="1.0" encoding="utf-8"?>
      <s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        focusEnabled="true"
        >
      <!-- <s:Label id="lblData" top="0" left="0" right="0" bottom="0" text="{dataGridListData.label}" />-->
      <fx:Metadata>
      [Event(name="buttonClicked")]
      </fx:Metadata>
      <fx:Script>
      <![CDATA[
      import mx.controls.Alert;
      protected function button1_clickHandler(event:MouseEvent):void
      {
      dispatchEvent(new Event("buttonClicked", true, true));
      }
      ]]>
      </fx:Script>
      <s:Button label="{data.label}" top="0" left="0" right="0" bottom="0" toolTip="Add to plan" click="button1_clickHandler(event)" />
      </s:MXDataGridItemRenderer>