从 ItemRenderer 组件(图像)内部调用 clickHandler

发布于 2024-11-25 05:37:51 字数 1248 浏览 3 评论 0原文

我正在使用一个 Datagrid,它有一个包含图像的 itemRenderer:

protected static function hbox1_clickHandler(event:MouseEvent):void
        {
            //some action
        }


<mx:DataGrid id="reportDG" dataProvider="{CDODReportsModel.instance.reportDataArrayCollectionObject}"  color="black" horizontalScrollPolicy="on">
    <mx:columns>                        
        <mx:DataGridColumn headerText="info">
            <mx:itemRenderer>
                <fx:Component>
                    <mx:HBox horizontalAlign="center">
                                <mx:Image source="assets/images/i_info.png" scaleX="0.6" scaleY="0.6" click="hbox1_clickHandler(event)"/>
                    </mx:HBox>
                </fx:Component>
            </mx:itemRenderer>
        </mx:DataGridColumn>
        <mx:DataGridColumn dataField="NAME" headerText="NAME"/>                         
        <mx:DataGridColumn dataField="TOTAL" headerText="TOTAL"/>

    </mx:columns>

</mx:DataGrid>

我想在单击时调度一个事件,因此当我单击图像时我会执行一个操作。但是,这样做时它给了我一个错误。我做了一些搜索,建议的答案是使用 externalDocument 和 ParentDoecument .. 两者都不起作用。

如何访问点击处理函数(我的代码中的 hbox1_clickHandler() )?

I'm using a Datagrid which has an itemRenderer that contains an image :

protected static function hbox1_clickHandler(event:MouseEvent):void
        {
            //some action
        }


<mx:DataGrid id="reportDG" dataProvider="{CDODReportsModel.instance.reportDataArrayCollectionObject}"  color="black" horizontalScrollPolicy="on">
    <mx:columns>                        
        <mx:DataGridColumn headerText="info">
            <mx:itemRenderer>
                <fx:Component>
                    <mx:HBox horizontalAlign="center">
                                <mx:Image source="assets/images/i_info.png" scaleX="0.6" scaleY="0.6" click="hbox1_clickHandler(event)"/>
                    </mx:HBox>
                </fx:Component>
            </mx:itemRenderer>
        </mx:DataGridColumn>
        <mx:DataGridColumn dataField="NAME" headerText="NAME"/>                         
        <mx:DataGridColumn dataField="TOTAL" headerText="TOTAL"/>

    </mx:columns>

</mx:DataGrid>

I want to dispatch an event on click, so when I click on the image I do an action. However, It is giving me an error when do that. I did some search and the suggested answers were using outerDocument and ParentDoecument .. both didn't work.

How can I access the click handler function (hbox1_clickHandler() in my code) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如若梦似彩虹 2024-12-02 05:37:52

我认为如果您在应用程序类中这是不可能的。
如果这是其他类,那么您可以编写如下内容:

click="MyClass.hbox1_clickHandler()"

此外,这不是编写内联项目渲染的最佳主意。最好的方法是扩展基本项目渲染器并制作您自己的渲染器。您还可以扩展 flash 事件类并创建您自己的事件类。这样做使您可以随事件发送一些附加数据。

但无论如何,使用你的方法代码将是这样的:

 protected function reportDG_initializeHandler(event:FlexEvent):void
 {
  reportDG.addEventListener("clicked", hbox1_clickHandler);
  function hbox1_clickHandler(event:Event):void
  {
   //some action
  }
 }

<mx:DataGrid initialize="reportDG_initializeHandler(event)">
    <mx:columns>
        <mx:DataGridColumn>
            <mx:itemRenderer>
                <fx:Component>
                    <mx:HBox>
                        <mx:Image click="dispatchEvent(new Event('clicked', true))"/>
                    </mx:HBox>
                </fx:Component>
            </mx:itemRenderer>
        </mx:DataGridColumn>
    </mx:columns>
</mx:DataGrid>

I dont think that this is possible if you are in Application class.
If this is some other class, than you can just wright something like this:

click="MyClass.hbox1_clickHandler()"

Also this is not the best idea to wright inline item renderes. The best approach is to extend base item Renderer and make your own one. Also you can extend flash Event class and make your own. Doing this gives you possibility to send some additional data with your event.

But anyway, using yours approach code will be like this:

 protected function reportDG_initializeHandler(event:FlexEvent):void
 {
  reportDG.addEventListener("clicked", hbox1_clickHandler);
  function hbox1_clickHandler(event:Event):void
  {
   //some action
  }
 }

<mx:DataGrid initialize="reportDG_initializeHandler(event)">
    <mx:columns>
        <mx:DataGridColumn>
            <mx:itemRenderer>
                <fx:Component>
                    <mx:HBox>
                        <mx:Image click="dispatchEvent(new Event('clicked', true))"/>
                    </mx:HBox>
                </fx:Component>
            </mx:itemRenderer>
        </mx:DataGridColumn>
    </mx:columns>
</mx:DataGrid>
魂ガ小子 2024-12-02 05:37:52

只要该函数声明为“public”,它就可以与 externalDocument.functionName 一起使用。就您而言,如果您将函数声明从 protected 更改为 public,它将起作用。这是示例代码[工作]:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            public function hbox1_clickHandler(event:MouseEvent):void
            {
                Alert.show("ite works");
            }
        ]]>
    </fx:Script>
    <mx:DataGrid id="reportDG" dataProvider="{new ArrayCollection(['A','B'])}"  color="black" horizontalScrollPolicy="on">
        <mx:columns>                        
            <mx:DataGridColumn headerText="info">
                <mx:itemRenderer>
                    <fx:Component>
                        <mx:HBox horizontalAlign="center">
                            <mx:Button label="Button" click="outerDocument.hbox1_clickHandler(event)"/>
                        </mx:HBox>
                    </fx:Component>
                </mx:itemRenderer>
            </mx:DataGridColumn>
            <mx:DataGridColumn dataField="NAME" headerText="NAME"/>                         
            <mx:DataGridColumn dataField="TOTAL" headerText="TOTAL"/>

        </mx:columns>

    </mx:DataGrid>
</s:WindowedApplication>

It will work with outerDocument.functionName as long as the function is declared as 'public'. In your case, if you change your function declaration from protected to public, it will work. Here is the sample example code [working]:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            public function hbox1_clickHandler(event:MouseEvent):void
            {
                Alert.show("ite works");
            }
        ]]>
    </fx:Script>
    <mx:DataGrid id="reportDG" dataProvider="{new ArrayCollection(['A','B'])}"  color="black" horizontalScrollPolicy="on">
        <mx:columns>                        
            <mx:DataGridColumn headerText="info">
                <mx:itemRenderer>
                    <fx:Component>
                        <mx:HBox horizontalAlign="center">
                            <mx:Button label="Button" click="outerDocument.hbox1_clickHandler(event)"/>
                        </mx:HBox>
                    </fx:Component>
                </mx:itemRenderer>
            </mx:DataGridColumn>
            <mx:DataGridColumn dataField="NAME" headerText="NAME"/>                         
            <mx:DataGridColumn dataField="TOTAL" headerText="TOTAL"/>

        </mx:columns>

    </mx:DataGrid>
</s:WindowedApplication>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文