数据网格中的Itemclick事件

时间:2009-06-09 20:30:21

标签: flex events datagridview click method-dispatch

问题可以概括为当点击datagrid中的项目时,文本区域显示项目的值,但是这里的组件是独立的,因此需要调度事件。

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应用程序文件:

<?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>

1 个答案:

答案 0 :(得分:0)

我认为studyId可能更好地引用dataGrid而不是引用studyId的dataGrid。您可以将其添加到主mxml:

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

这应该有效,因为TextArea.text将响应DataGrid.selectedItem的属性更改事件,因此每当选择更改时它都会更改。

编辑:调度事件:

您可以从代码中的任何位置发送事件,并且听众将能够收听该事件。例如:

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

现在您可以收听该事件:

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

当您从customComponent触发StudyEvent.STUDYSELECTED事件时,会发生什么,它将被您的main函数捕获,并且将调用studySelectedListener。