问题
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>
///////////////////////////////////////////////////////////////
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>
回答1:
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}"/>
This should work because TextArea.text will respond to the property changed event of DataGrid.selectedItem, so it will change whenever the selection changes.
EDIT: Dispatching events:
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.
}
Hope this helps!
<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/>
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.
来源:https://stackoverflow.com/questions/972334/itemclick-event-in-datagrid