简体   繁体   English

带有项目渲染器和自定义组件的flex自定义事件

[英]flex custom event with item renderer and custom component

OK you Flex experts, I need some help. 好吧,Flex专家,我需要一些帮助。 I have a datagrid in my main application with an itemrenderer (mxml). 我的主应用程序中有一个带有itemrenderer(mxml)的数据网格。 When you press the image in the ir, a custom component (mxml) opens. 在ir中按下图像时,将打开一个自定义组件(mxml)。 The cc has a button that is supposed to call a function in the main application which updates the arraycollection (dataprovider) and therefore the datagrid updates. cc有一个按钮,该按钮应该在主应用程序中调用一个函数,该函数更新arraycollection(数据提供程序),从而更新datagrid。 I've tried several variations of parentDocument, outerDocument, and custom events, but I cannot get that function to work from the button. 我尝试了parentDocument,outerDocument和自定义事件的几种变体,但无法从按钮上使用该功能。 I think it's b/c I have the cc nested in the ir. 我认为这是b / c,我将cc嵌套在ir中。 Anything that I call directly from within the ir works. 我直接从ir作品中调用的任何内容。

Does anyone have any suggestions or even better a working example I could take a look at? 有没有人有任何建议,或者甚至更好的是我可以看的工作示例?

Here's what I tried: 这是我尝试过的:

//in main application //在主应用程序中

public function creationComplete_handler(event:FlexEvent):void{
   dgList.addEventListener("ceRD", fnt_ceRD);
}
public function fnt_ceRD():void {
    Alert.show("called");
}


<mx:AdvancedDataGrid id="dgList" dataProvider="{acLists}" designViewDataType="flat">
   <mx:columns>
      <mx:AdvancedDataGridColumn headerText="Roster" sortable="false" itemRenderer="c_CO.AppLocal.ListManager.iRenderers.irADVStudents" />   </mx:columns>
</mx:AdvancedDataGrid>

In itemrenderer, used the popupmanager so that I could center on top of application as opposed to button in datagrid 在itemrenderer中,使用了popupmanager,这样我就可以将应用程序放在顶部,而不是datagrid中的按钮

public function btnRoster(event:MouseEvent):void{
    var rShow:rosterDetails = new rosterDetails();
    PopUpManager.addPopUp(rShow, FlexGlobals.topLevelApplication.mainContent, true);
    PopUpManager.centerPopUp(rShow);
}

In custom component: 在自定义组件中:

 <fx:Metadata>
        [Event("ceRD", true, false)]
    </fx:Metadata>

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

            protected function btnSave(event:MouseEvent):void {

            var i_ceRD:Event = new Event("ceRD");
            dispatchEvent(i_ceRD);
            PopUpManager.removePopUp(this);

        }

        ]]>
    </fx:Script>

You are experiencing trouble because the listener for "ceRD" is added to the DataGrid while the event is being dispatched from the Roster Details component. 您正在遇到麻烦,因为在从名册详细信息组件分派事件时,“ ceRD”的侦听器已添加到DataGrid。 There is no relationship between the DataGrid and RosterDetails. DataGrid和RosterDetails之间没有关系。

Consider moving the logic to create and display the RosterDetails out of the ItemRenderer. 考虑将逻辑创建和显示RosterDetails移出ItemRenderer。 I would suggest having the irADVStudents renderer dispatch an 'imageClick' event which can be handled in your main application. 我建议让irADVStudents渲染器调度一个'imageClick'事件,该事件可以在您的主应用程序中处理。

Inline renderers are helpful for adding simple event handling: 内联渲染器有助于添加简单的事件处理:

<mx:AdvancedDataGrid dataProvider="{acLists}">
    <mx:columns>
        <mx:AdvancedDataGridColumn headerText="Roster" sortable="false">
            <mx:itemRenderer>
                <mx:Component>
                    <local:irADVStudents imageClick="outerDocument.onRoster(event)"/>
                </mx:Component>
            </mx:itemRenderer>
        </mx:AdvancedDataGridColumn>
    </mx:columns>
</mx:AdvancedDataGrid>

Then you can simply add a listener for your 'save' event. 然后,您只需为“保存”事件添加一个侦听器。 Ensure to remove the listener as this is a potential memory leak. 确保删除侦听器,因为这可能会导致内存泄漏。 (A weak listener is also an option). (弱听者也是一种选择)。

public function onRoster(event : Event) : void
{
    var rosterDetails : RosterDetails = new RosterDetails();
    rosterDetails.addEventListener("save", onSave);

    PopUpManager.addPopUp(rosterDetails, this, true);
    PopUpManager.centerPopUp(rosterDetails);
}

protected function onSave(event:Event):void
{
    Alert.show("SAVED");
}

Custom events can be used to pass data around the application, such as which item was clicked on from the renderer. 自定义事件可用于在应用程序周围传递数据,例如从渲染器中单击了哪个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM