数据网格中的 Itemclick 事件 [英] Itemclick event in datagrid

查看:26
本文介绍了数据网格中的 Itemclick 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题可以归结为点击datagrid中的某一项时,文本区域显示该项的值,但这里的组件是分开的,因此需要调度事件.

The problem can be summarized as when clicking an item in datagrid, the text area shows the value of the item, but here the compoents are separate and hence events need to be dispatched.

My mxml component file :

<?xml version="1.0" encoding="utf-8"?>
<mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml" itemClick="itemClickEvent(event);"  creationComplete="init()">

<mx:Metadata>
  [Event(name="IdSelected", type="one.IdEvent")]
</mx:Metadata>

<mx:Script>
<![CDATA[     import genericReport.*;
              import crewUtilization.*;
              import utils.*;
              import studies.*;
              import mx.rpc.events.FaultEvent;
              import mx.rpc.events.ResultEvent;
              import mx.controls.Alert;
              import mx.events.ListEvent;


      private function itemClickEvent(event:ListEvent):void 
      {
          var _study:Object=event.currentTarget.selectedItem.study;
          dispatchEvent(new IDEvent(_ID));     
      }


]]>

</mx:Script>

<mx:columns>

 <mx:DataGridColumn dataField="name" />
 <mx:DataGridColumn dataField="userId" />
</mx:columns>
</mx:DataGrid>

//////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////

这是我的主要 MXML 应用程序文件:

This is my Main MXML Application file :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:custom="*">
<mx:TitleWindow label="Scenario Creation" xmlns:mx="http://www.adobe.com/2006/mxml"
 xmlns:ns1="ccCreation.*">

<mx:Label text="CC CREATION" width="100%" />
<mx:VBox width="100 %" styleName="scenariovboxStyle">

<custom:studySelector id="dg" />
</mx:VBox>
</mx:TitleWindow>   
</mx:Application>

推荐答案

我认为,studyId 引用 dataGrid 可能比 dataGrid 引用 studyId 更好.您可以将其添加到主 mxml 中:

I think it might be better for studyId to refer to the dataGrid rather than the dataGrid referring to studyId. You can add this to your main mxml:

<mx:TextArea id="studyId" text="{dataGrid.selectedItem.Study}"/>

这应该可以工作,因为 TextArea.text 将响应 DataGrid.selectedItem 的属性更改事件,因此只要选择更改,它就会更改.

This should work because TextArea.text will respond to the property changed event of DataGrid.selectedItem, so it will change whenever the selection changes.

调度事件:

您可以从代码中的任何位置分派事件,并且侦听器将能够侦听该事件.例如:

You can dispatch an event from any place in your code, and listeners will be able to listen in to that event. Eg:

<mypackage:MyComponent>
...
private function foo():void
{
    dispatchEvent(new MouseEvent(MouseEvent.CLICK)); // Dispatches a mouse event whenever foo is called.
}

现在您可以监听该事件:

Now you can listen for that event:

<mypackage:MyComponent id="myComponent"/>
...
myComponent.addEventListener(MouseEvent.CLICK, mouseClickHandler);

private function mouseClickHandler(event:MouseEvent):void
{
    ... // code to handle that event here.
}

希望这会有所帮助!

<mx:MainComponent creationComplete="init()" ...>
    ...
    private function init(event:Event):void
    {
        ...
        customComponent.addEventListener(StudyEvent.STUDYSELECTED, studySelectedListener);
        ...
    }

    private function studySelectedListener(event:StudyEvent):void
    {
        studyid.text = event.study.studyId; // or wherever you store your studyId value
        ...
    }
    ...
<mx:MainComponent/>

每当从您的自定义组件触发 StudyEvent.STUDYSELECTED 事件时,您的主函数都会捕获该事件并调用 studySelectedListener.

What happens is whenever a StudyEvent.STUDYSELECTED event is fired from your customComponent, it will be caught by your main function and studySelectedListener will be called.

这篇关于数据网格中的 Itemclick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆