views:

1768

answers:

1

Hi,

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>
A: 

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.

CookieOfFortune
hey...Thanks for the suggestion....But, what about the 'Study' being an object which consists of "StudyId, name and userId". I have to show only studyId. Also, is my ItemClick event correct ?? i.e. if I make the change in my main mxml file and keep the itemclick event as it is, will the code work ??
If your object has StudyId and UserId as members, then you can replace selectedItem.Study with selectedItem.StudyId, since the selectedItem will refer to your object(Study). Your itemClickHandler is correct, except you don't have access to the textArea. What you could do if you wanted to stick with your event handler, is to dispatch an event from your itemClickHandler. Your main should catch that event and respond by setting the textArea. This is similar to what I have suggested, the handlers are just hidden by using bindable properties.
CookieOfFortune
Another option is to extend DataGrid with a subclass that takes your main class as a property. Then it can have access to all the components contained in main.
CookieOfFortune
Hi...I have shown the main MXML application file also now.....As you said, I am supposed to dispatch an event from itemClickHandler....I don't know how to dispatch an event and display the text in the main screen.i.e. catch the output....I will higly appreciate if you could help me with the dispatch event code and the modifications for same in the main application..... I have provided all file codes in the question now.....Please ..I request you to help me with the same... Thanks...
The reason for dispatch event code is that I am asked to use dispatch event code....
Format your code with 4 spaces. In any case, text="{dataGrid.selectedItem.StudyId}" will handle everything for you. What happens is every time the selectedItem in dataGrid changes, text will change along with it. More about bindable properties: http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_8.html
CookieOfFortune
I have formatted the code...I need to use Dispatch events ... Can you write the code for dispatch events.. Also, I can take full Study object ..no need to split internal details... Please help ...Thanks..
Can you help me with the code please...I dont know how to dispatch an event.....I will be really thankful to you..
oh, your code shows that you're already dispatching a StudyEvent.
CookieOfFortune
also, no, your code is not formatted. Look at how it displays on the page.
CookieOfFortune
Can you help me with the dispatch event part specific to my question...like how to display the study in text area ?
Yes, I have the StudyEvent in the <metadata> tag...
Can I write :private function itemClickEvent(event:ListEvent):void { var study:Object=event.currentTarget.selectedItem.Study; dispatchEvent(new StudyEvent(study)); }
sorry...I cannot format this part of code...
yeah, you can't format in comments, anyways, yeah, you can write something like that.
CookieOfFortune
but in that case, you can just listen for that event directly from main.
CookieOfFortune
I wrote the dispatch as shown above....how to write the listener to show the study details in the text area ??public function handler():void { studyid.addEventListener(MouseEvent.CLICK, StudyEvent); }Here the StudyEvent is the event in metadata tag.. How to get Study details to be displayed in textarea ?? Please..I think this is the final part of the answer...
How to append the Study data in my text area..after the listener is written as shown above..
Also,... studyid.addEventListener(MouseEvent.CLICK, StudyEvent); ....is this correct..cos..here..StudyEvent is the event not a function ??
Well, I think you'd probably have the addEventListener() function in your init() event for your main component, so it'll be attached at startup. The signature for addEventListener is addEventListener(eventType:String, listener:function). So listener should be the name of the function you want called when the event is caught. I like to name my event listeners as [EventName]Listener, so for this it would be "private function studyEventListener(event:StudyEvent):void {...}". Then in main have "studyid.addEventListener(StudyEvent.EVENT_TYPE, studyEventListener);"
CookieOfFortune
oops, I meant studySelectedListener. In any case, I formatted your code for you again.
CookieOfFortune
... Take a look at my edits to my answer, it explains it better.
CookieOfFortune
Thanks..for that explanation.....1> StudyEvent.STUDYSELECTED .. what does 'STUDYSELECTED' refer here to ? Is it referring to the event mentioned in the metatag i.e. <mx:Metadata>[Event(name="studySelected", type="one.StudyEvent")]</mx:Metadata> ...2> And if I need to extract individual data i.e. userId, name, etc. from the Study object, how do I split that in my "event.value" ?
Also : I am dispatching the event like this : dispatchEvent(new StudyEvent(_study)); .... Don't I have to GET the value of "_study" in my main component. Can I directly access it as you have shown ?
You used "studySelected" as the name of the event, it is common to create a public static string equal to that value. So you would just need public static var STUDYSELECTED = "studySelected" in your StudyEvent class.I changed the way I get the value of study to reflect what you have done in your code, check out the edit.
CookieOfFortune
Error: Type was not found or was not a compile-time constant: StudySelectedEvent. private function studySelectedListener(event:StudySelectedEvent):void
I got the above error...any idea of how to go about the same..
Warning: variable 'STUDYSELECTED' has no type declaration.public static var STUDYSELECTED = "studySelected";
And this warning for declaration in StudyEvent class....By the way, the StudyEvent Class has been named as StudySelectedEvent in the main package.....
? Where did you get StudySelectedEvent from, if you renamed it, you need to rename the class you declared. STUDYSELECTED is a string, I forgot to put in the type declaration, should be STUDYSELECTED:String = "studySelected".
CookieOfFortune
Ok...the warning has been removed..But the error.. : Error: Type was not found or was not a compile-time constant: studySelected.private function studySelectedListener(event:studySelected):void Is still there.... what should event : ?? be ...have tried all possible combinations.....And yes, I have renamed classes...with StudySelectedEvent
event:StudyEvent. I guess since you renamed ii, it should be event:StudySelectedEvent. I think you should read through more tutorials about the basics of Flex and Actionscript programming in general, since you're requesting pretty basic concepts here.
CookieOfFortune
Ok...I will check that out....I compiled the code...it built ...on the text area, the datagrid row selected is not being appended..
Set breakpoints in your event handlers to see if the message is being passed correctly.
CookieOfFortune
Thanks.....Its working now... It shows the details in text area....
Hey...there is some prob...The first row of datagrid if selected by itemclick is not displayed in the text area .... All other rows can be selected and displayed apprpriately... Can you tell the error ?
Is there come problem with row with index 0..i.e.the first row....But, am not using any index preferences in my code...it should ideally be correct..
I think the title row might have an index of -1, you would have to handle that in your event listeners.
CookieOfFortune
Cannot access a property or method of a null object reference. - This is the error I am getting in the background, but it shows the data of all ows except first one....Any modifications to the code ??
Maybe you didn't insert any data for row 0?
CookieOfFortune
Insert...am populating the data from a file...The data is being displayed in the datagrid as well..Problem is with the first row..Also, the other rows are being displayed but, in background this error is thrown...
The first row if clicked, data not displayed in text area which is not the case for other rows..
There is some problem in the way the object has been implemented .. Can you help figure out and solve that...