How can I know when a Button in a Flex DataGrid itemRenderer is clicked?

馋奶兔 提交于 2019-12-01 08:33:24

You need to make the itemRenderer a class, and then reference your DataGrid from within that class utilizing the methods described here. You can then dispatch events from the DataGrid, which are easy to listen for in the container that holds it. What you don't want to do is rely on bubbling or attempt to listen to the itemRenderer directly. You will probably want to create a custom event that carries the data property of the DataGrid row so that your event listener can quickly access this information.

Thanks Joel. Here's the final solution I came up with after reading that article (which I've read before). I want to add the item whose Button was clicked to an Array which is a property of another item, so I pass the "other item" into the DataGrid Component as a property, and perform actions against it in the function call from the itemRenderer:

/* CustomDataGrid.mxml */
<mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            public var otherData:Object;

            public function takeAction(item:Object):void
            {
                otherData["children"][0] = item;
            }
        ]]>
    </mx:Script>

    <mx:columns>
        <mx:DataGridColumn dataField="firstName" headerText="First Name" 
            width="75" />

        <mx:DataGridColumn dataField="LastName" headerText=" Last Name" 
            width="150" />

        <mx:DataGridColumn dataField="phone" headerText="Phone" 
            width="120" />

        <mx:DataGridColumn headerText="" width="110" 
            itemRender="ActionButtonItemRenderer" />
    </mx:columns>
</mx:DataGrid>

/* ActionButtonItemRenderer.as */
package
{
    import flash.events.MouseEvent;

    import mx.controls.Button;

    public class ActionButtonItemRenderer extends Button
    {
        public function ActionButtonItemRenderer()
        {
            super();

            label = "Take Action";
        }

        override protected function clickHandler(event:MouseEvent):void
        {
            super.clickHandler(event);

            var owner:CustomDataGrid = listData.owner as CustomDataGrid;

            owner.takeAction(data);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!