0

我有一个 DataGrid,第一列单元格有一个 itemRenderer,其中包含嵌入图像和链接的 doubleClick 事件。

因此,当发生双击事件时,此渲染器会捕获事件和处理程序。问题是我想用 Datagrid 中所选项目的索引发送一个事件,但我不知道如何获取它。

s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true"
                width="50" height="30">

<fx:Script>
    <![CDATA[
        import events.EditItemEvent;

        import mx.controls.Alert;
        override public function prepare(hasBeenRecycled:Boolean):void {}

        protected function btn_edit_doubleClickHandler(event:MouseEvent):void {
            /* here I dispatchEvent with the 'index' of the selectedItem of the datagrid */
        }

    ]]>
</fx:Script>
<s:Image id="btn_edit" horizontalCenter="0" source="@Embed('assets/images/edit_icon.png')"
         verticalCenter="0"
         doubleClickEnabled="true"
         doubleClick="btn_edit_doubleClickHandler(event)"/>

任何想法?

4

2 回答 2

1

你能用网格吗?选定索引

老实说,我认为这是一个坏主意。渲染器不应该知道有关 DataGrid 的任何信息,包括 selectedIndex。如果您想调度一个自定义事件,以便您可以在显示列表层次结构中的某个位置对渲染器数据进行操作;你应该包括数据,而不是索引。您的处理程序方法对数据进行操作。

于 2013-05-10T13:24:47.383 回答
0

您可以通过它的 owner 属性访问它:

(this.owner as DataGrid).selectedIndex

调度自定义事件的简单 itemRenderer:

<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx">

<fx:Script>
    <![CDATA[   

        override public function prepare(hasBeenRecycled:Boolean):void {
            lblData.text = data[column.dataField]
        }
        protected function onClickButton(ev:MouseEvent):void {
            trace("ButtonDispatchItemRenderer::onClickButton");
            //dispatch the Event which has a static const GET_SELECTED_INDEX for the event type
            this.owner.dispatchEvent(new MyEvent(MyEvent.GET_SELECTED_INDEX));
        }
    ]]>
</fx:Script>

<s:HGroup verticalAlign="baseline">
    <s:Label id="lblData" top="9" left="7"/>
    <s:Button label="dispatch" click="onClickButton(event)"/>
</s:HGroup>

</s:GridItemRenderer>

带有监听事件的 DataGrid 的简单应用程序

<mx: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" creationComplete="onCreationComplete()">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        import spark.components.gridClasses.GridColumn;


        [Bindable]
        public static var initDG:ArrayCollection = new ArrayCollection([
            {label:"firstItem"}, {label:"secondItem"}, {label:"thirdItem"}

        ]); 


        protected function onCreationComplete():void {

            dataGrid.addEventListener(MyEvent.GET_SELECTED_INDEX, onRendererButtonClick, false, 0, true);
        }
        protected function onRendererButtonClick(ev:MyEvent):void {
            trace("Application::RenderButtonClick");
        }

    ]]>

</fx:Script>
<s:VGroup>
    <s:DataGrid id="dataGrid" verticalCenter="0" horizontalCenter="0"  dataProvider="{initDG}">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="label" itemRenderer="renderers.ButtonDispatchItemRenderer"></s:GridColumn>                   
            </s:ArrayList>
        </s:columns>            
    </s:DataGrid>
</s:VGroup>

</mx:Application>

您必须向包含 selectedIndex 的事件添加一个属性。你必须将值传递给渲染器内的事件(例如构造函数中的第二个参数)这个例子只是调度事件

于 2013-05-10T13:53:51.037 回答