In this post we will talk about Component Event in Lightning Component. To use Component Event we need to perform below steps
1) Create Event Component
2) Register the Event ( in Child Component )
3) Fire the Event ( from Child Component)
4) Handle the Event (In Parent Event)
Here is Code For you
Component Event
AccEvent
AccEvent
<aura:event type="COMPONENT">
<aura:attribute name="AccRecord" type="Account"/>
</aura:event>
<aura:attribute name="AccRecord" type="Account"/>
</aura:event>
Notifier Component
<!-- c:AccNotifier -->
Handler Component
<!-- c:AccNotifier -->
<aura:component>
<aura:attribute name="newAccount" type="Account"
default="{ 'sobjectType': 'Account','Name': 'Test Name'}"/>
<aura:registerEvent name="cmpAccEvent" type="c:AccEvent"/>
<p>
<form class="slds-form--stacked">
<lightning:input aura:id="newAccountform"
label="Account Name" name="AccAction"
value="{!v.newAccount.Name}" />
<lightning:button label="Add Acc"
class="slds-m-top--medium"
variant="brand"
onclick="{!c.fireComponentEvent}"/>
</form>
</p>
</aura:component>
/* AccNotifierController */<aura:attribute name="newAccount" type="Account"
default="{ 'sobjectType': 'Account','Name': 'Test Name'}"/>
<aura:registerEvent name="cmpAccEvent" type="c:AccEvent"/>
<p>
<form class="slds-form--stacked">
<lightning:input aura:id="newAccountform"
label="Account Name" name="AccAction"
value="{!v.newAccount.Name}" />
<lightning:button label="Add Acc"
class="slds-m-top--medium"
variant="brand"
onclick="{!c.fireComponentEvent}"/>
</form>
</p>
</aura:component>
({
/* AccNotifierController */
fireComponentEvent : function(cmp , event) {
var cmpEvent = cmp.getEvent("cmpAccEvent");
// Get the value from Component and set in Event
cmpEvent.setParams( { "AccRecord" : cmp.get("v.newAccount") } );
cmpEvent.fire();
}
})
/* AccNotifierController */
fireComponentEvent : function(cmp , event) {
var cmpEvent = cmp.getEvent("cmpAccEvent");
// Get the value from Component and set in Event
cmpEvent.setParams( { "AccRecord" : cmp.get("v.newAccount") } );
cmpEvent.fire();
}
})
Handler Component
<aura:component>
<aura:attribute name="AccListFromEvent" type="Account[]"/>
<!-- Note that name="cmpAccEvent" in aura:registerEvent in AccNotifier.cmp -->
<aura:handler name="cmpAccEvent" event="c:AccEvent" action="{!c.handleComponentEvent}"/> <!-- handler contains the notifier component -->
<c:AccNotifier />
<!-- Disply Record -->
<div>
<lightning:card title="List Account">
<p class="slds-p-horizontal--small">
<aura:iteration items="{!v.AccListFromEvent}" var="acc">
<br/>
{!acc.Name}
</aura:iteration>
</p>
</lightning:card>
</div>
</aura:component>
<aura:attribute name="AccListFromEvent" type="Account[]"/>
<!-- Note that name="cmpAccEvent" in aura:registerEvent in AccNotifier.cmp -->
<aura:handler name="cmpAccEvent" event="c:AccEvent" action="{!c.handleComponentEvent}"/> <!-- handler contains the notifier component -->
<c:AccNotifier />
<!-- Disply Record -->
<div>
<lightning:card title="List Account">
<p class="slds-p-horizontal--small">
<aura:iteration items="{!v.AccListFromEvent}" var="acc">
<br/>
{!acc.Name}
</aura:iteration>
</p>
</lightning:card>
</div>
</aura:component>
/* AccHandlerCMPController.js */
({
/* AccHandlerCMPController.js */
handleComponentEvent : function(cmp, event) {
// Get value from Event
var accRec = event.getParam("AccRecord");
// Get the List of Existing Account ListOfAcc
var ListOfAcc = cmp.get("v.AccListFromEvent");
// Add Record in List
ListOfAcc.push(accRec);
// set the handler attributes based on event data
cmp.set("v.AccListFromEvent", ListOfAcc);
}
})
/* AccHandlerCMPController.js */
handleComponentEvent : function(cmp, event) {
// Get value from Event
var accRec = event.getParam("AccRecord");
// Get the List of Existing Account ListOfAcc
var ListOfAcc = cmp.get("v.AccListFromEvent");
// Add Record in List
ListOfAcc.push(accRec);
// set the handler attributes based on event data
cmp.set("v.AccListFromEvent", ListOfAcc);
}
})
Please check below post
Please check below post for more detail
Thanks,
Amit Chaudhary
Thank you Amit for this post, it's so cool, ,just my opinion- why we need this Event(comp/App level).It would be great if you can describe concept-wise
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNice Explanation. Keep it up.
ReplyDeleteGreate amit
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI just copied the files with the correct names. It didn't work.
ReplyDeleteThanks for this example
ReplyDelete