function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Engine ForceEngine Force 

Component Handling Its Own Event - get id from the iteration

On this documentation link:

it says, a component can also handle its own event. Simply include an aura:registerEvent and aura:handler in that component.

Let's say I have a list of accounts and on clicking the account name, I want to display its contact list.

<aura:iteration items="{!v.accounts}" var="acc">    
        <div onclick="{!c.showContacts}" class="row primary">{!acc.Name}</div>     

In the showContacts controller action, how do I set the parameter to the event which takes an account object?
// How do I set acct below?
var updateEvent = component.getEvent("showContacts");
      updateEvent.setParams({ "account": acct }).fire();

var acct = component.get("");
this doesn't work.

I know how to do this via a sub-component that shows the account name, register the onclick event and handle it in the main component. Just wondering how you do this in the component itself since the logic is really simple and do not want to use a subcomponent for that one line of code.

Please advise.


You can do this using <aura:methods>

<aura:method name="sampleMethod" action="{!c.doAction}" access="PUBLIC" 
  description="Sample method with parameters"> 
    <aura:attribute name="param1" type="String" default="parameter 1"/> 
<aura:iteration items="{!v.accounts}" var="acc">    
        <div onclick="sampleMethod({!})" class="row primary">{!acc.Name}</div>     
    doAction : function(cmp, event) {
        var params = event.getParam('arguments');
        if (params) {
            var param1 = params.param1;
            // add your code here

Refre this:
Kt YadavKt Yadav
I am new to lightning and i need to understand  the scenarios or use cases where "Component Handling Its Own Event" is required.Why do we need that because inisde the same component every attribute is available so what purpose we are achieving here using own event?