我怎么能知道在Flex的DataGrid的itemRenderer按钮被点击时?按钮、我怎么能、DataGrid、Flex

2023-09-08 13:44:09 作者:此ID未注册

我有一个显示数据的数列的DataGrid组件。它具有显示一个按钮,它允许用户采取的动作是关于记录一个附加列。

I have a DataGrid component that displays a few columns of data. It has one additional column that displays a Button that allows the user to take an action with regard to the record.

<mx:DataGrid dataProvider="{myData}">
    <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">
            <mx:itemRenderer>
                <mx:Component>
                    <mx:Box horizontalAlign="center" width="100%">
                        <mx:Button label="Take Action" />
                    </mx:Box>
                </mx:Component>
            </mx:itemRenderer>
        </mx:DataGridColumn>
    </mx:columns>
</mx:DataGrid>

我需要执行父组件的操作,使用其他数据可用那里,但无关的DataGrid中的数据。

I need to perform an action in the parent component, using other data that is available there, but unrelated to the data in the DataGrid.

什么是捕获按钮单击父组件,并且知道它对应的是什么记录?最好的办法

What is the best way to catch the Button click in the parent component, and know what record it corresponds to?

我应该使用一个自定义事件,或者一个的itemEditor,还是其他什么东西完全?

Should I use a Custom Event, or an itemEditor, or something else completely?

推荐答案

由于乔尔。下面是最终的解决方案,我想出了阅读的文章(我之前已经读)之后。我想补充的按钮被点击到一个数组是另一个项目的属性的项目,所以我通过其他项目进入DataGrid组件作为属性,并从itemRenderer的函数调用进行反对它的操作:

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);
        }
    }
}