从itemRenderer获取Datagrid中的selectedIndex

时间:2013-05-10 11:54:13

标签: actionscript-3 flex flex4.5 flexbuilder

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

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

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)"/>

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用gridselectedIndex吗?

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

答案 1 :(得分:0)

您可以通过它的所有者属性访问它:

(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的事件中添加属性。并且您必须将值传递给渲染器内的事件(例如构造函数中的第二个参数)此示例仅调度事件