1 Reply Latest reply on Dec 16, 2007 5:30 AM by peterent

    Tricky Passing Event problem

    WestSide Level 1

      I have two files here. 1) MyFavorites 2) MyFavoritesItemRenderer

      I have a datagrid with an item renderer. The item renderer is essentially a delete image. I am trying to write a function so when the user clicks the image, it will remove the row from the datagrid. The problem I am having is the item renderer sits in its own file and the DataGrid is in another.

      Do I write a private function within the item renderer.mxml file that will dispatch an event? I think this is only way to do it as I only want my event dispatched when the user explicitly clicks the image.

      So in my itemRenderer I suppose I would write a function called "deleteRecord()", this function would dispatch an event and the file which actually contains the DataGrid would listen for the event and act accordingly. Is this right?

      If so, how would I pass a reference to which row I want to delete? The itemRenderer sits in its own file and seems to not have any knowledge of my <mx:DataGrid>

      My datagrid code is below which shows how I am using my itemRenderer

      <mx:DataGrid id="myFavoritesDG" x="0" y="0" width="330" height="110" dragEnabled="false" dropEnabled="true" >

      <mx:DataGridColumn width="55" dataField="title" headerText="Action" itemRenderer="renderers.MyFavoritesItemRenderer" />


      Any help appreciated.

        • 1. Re: Tricky Passing Event problem
          peterent Level 2
          Your question is a good one and one a lot of people have.

          You are correct in your strategy. Have your itemRenderer use dispatchEvent, but when you create the event, set its bubbles parameter to true. This will allow the event to climb up the component chain. You don't need to have the DataGrid listen for this event; you just need to listen for it in the place where you want to use it so you can delete the record.

          To get the information you want, make sure your itemRenderer implements IDropInListItemRenderer. Classes which implement this will have a listData property and that holds the rowIndex. Check the documentation for this. If your class doesn't have it, implement it yourself, the documentation shows you how to do this.

          Now that you have the information you'll need to dispatch a custom event which contains the information. Again, check the docs for that.

          Finally, you will need to use ActionScript to listen for the event. Let's say the file in which the DataGrid is declared is the place where you want to listen for the event. Your custom event won't be an event the DataGrid normally dispatches, so you can't use MXML to declare the event listener. Instead, do it in ActionScript:

          mygrid.addEventListener( "deleteRecord", handleDeleteRecord );

          and then declare handleDeleteRecord to have an event parameter of the type of your custom event.