Example of component type event (aura:event type=”COMPONENT”) where passing the multiple attribute value from child component to parent component on click button in aura lighting component Salesforce | how to Get/Set component type event attribute value on click button in lightning component Salesforce

1,641 views


Hey guys, today in this post we are going to learn component event (aura:event type=”COMPONENT”) where passing the attribute value from Child Component to Parent Component on Click button in aura lighting component Salesforce.

Real time scenarios:- Create a component event in Salesforce lightning, and pass the event attribute value from child component to parent component on click button uses of register event and event handler.
After Get/Set the event attribute value, a Modal Popup should be open on parent component.

How to Communicate with Events in Lightning Aura Components

In the Aura Components programming model, events are fired from JavaScript controller actions. Events can contain attributes that can be set before the event is fired and read when the event is handled.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

Events are declared by the aura:event tag in a .evt resource, and they can have one of two types: component or application.

There are two type of event in lightning component in Salesforce

  • Component Events:- A component event is fired from an instance of a component. A component event can be handled by the component that fired the event or by a component in the containment hierarchy that receives the event. To know more details about Component Events, Click Here.
  • Application Events:- Application events follow a traditional publish-subscribe model. An application event is fired from an instance of a component. All components that provide a handler for the event are notified. To know more details about Application Events, Click Here

 

Files we used in this post example (Component type event):-

parentComponentEventApp.app Lightning Application It is used for call the component to preview on browser.

childComponentEvent.cmp

Lightning Component It is a child component and hold a click button.

childComponentEventController.js

JavaScript Controller File It is used for click function to To get the component event attribute value and fire the event.
parentComponentEvent.cmp Lightning Component It is a parent component and call the event handler (aura:handler) actiion function in lightning component
parentComponentEventController.js JavaScript Controller File It is set the attribute value of event from child component to parent.
myComponentEvent.evt Lightning Event Creat a application event, It’s hold two attribute as string type value.

Live Demo

Passing mutiple attributes value to component type event -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  • Find the below steps

Create a component type event. Use type=”COMPONENT” in the aura:event tag for an component event.

Step 1:- Create component type event : myComponentEvent.evt

From Developer Console >> File >> New >> Lightning Event

myComponentEvent.evt [Lightning Event]

  1.    <aura:event type="COMPONENT" description="Learn component event from w3web.net tutorial">
  2.     <aura:attribute name="cmpMsg" type="String" default="Welcome w3web.net" />
  3.     <aura:attribute name="popFadeEvnt" type="String" default="slds-hide" />
  4. </aura:event>

Create Lightning Component

Step 2:- Create Lightning Component : childComponentEvent.cmp

From Developer Console >> File >> New >> Lightning Component

childComponentEvent.cmp [Lightning Component File]

  1.    <aura:component >
  2. 	<aura:registerEvent name="myEvent" type="c:myComponentEvent" />
  3.     <lightning:button label="Example of Component Event Click Here.." variant="brand" onclick="{!c.childEvntAction}"/>
  4. <br/><br/>
  5.    <!--Start RelatedTopics Section-->
  6. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  7.  
  8.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  9.  
  10.             <br/><br/>
  11.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
  12.             <div style="display:block; overflow:hidden;"> 
  13.                 <div style="width: 50%; float:left; display:inline-block">
  14.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  15.                         <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  16.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  17.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  18.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  19.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  20.                     </ul>
  21.             </div>
  22.  
  23.             <div style="width: 50%; float:left; display:inline-block">
  24.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  25.                         <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  26.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  27.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  28.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  29.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  30.                     </ul>
  31.                 </div>
  32.                <div style="clear:both;"></div> 
  33.                <br/>
  34.                 <div class="youtubeIcon">
  35.                     <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  36.                 </div>
  37.     </div>
  38.  
  39. </div>
  40.  
  41.   <!--End RelatedTopics Section-->
  42. </aura:component>

Create JavaScript Controller

Step 3:- Create Lightning Component : childComponentEventController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

childComponentEventController.js [JavaScript Controller]

  1.    ({
  2. 	childEvntAction : function(component, event,helper) {         
  3.         var myEvent = component.getEvent("myEvent");         
  4.         myEvent.setParams({
  5.             "cmpMsg" : "Learn Step-by-step Salesforce Tutorial From -- ",
  6.             "popFadeEvnt" : "slds-show"
  7.         });        
  8.         myEvent.fire(); 
  9.     }
  10. })

Create Lightning Component

Step 4:- Create Lightning Component : parentComponentEvent.cmp

From Developer Console >> File >> New >> Lightning Component

parentComponentEvent.cmp [Lightning Component]

  1.   <aura:component >
  2.     <aura:handler name="myEvent" event="c:myComponentEvent" action="{!c.parentEventAction}"/>
  3. 	<aura:attribute name="w3webMsg" type="String"/>     
  4.     <aura:attribute name="modalFade" type="String" default="slds-hide"/> 
  5.  
  6.     <div class="slds-m-around_medium">
  7.         <c:childComponentEvent />
  8.         <div class="{!v.modalFade}" aura:id="modalPopupId">
  9.             <section class="slds-modal slds-fade-in-open">
  10.                 <div class="slds-modal__container">
  11.                     <header class="slds-modal__header">
  12.                         <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" onclick="{!c.cancelPopup }" title="Close">
  13.                             <lightning:icon iconName="utility:close" alternativeText="Approved" />
  14.                         </button>
  15.                         <p style="color:#ff0000;">{!v.w3webMsg} <a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer"><strong>w3web.net</strong></a></p> 
  16.                         <h2 class="slds-text-heading_medium slds-hyphenate">Component Event Popup</h2>
  17.                     </header>
  18.                     <div class="slds-modal__content slds-p-around_medium">
  19.                         <div class="slds-form slds-form_stacked">
  20.                             <div class="slds-form-element">
  21.                                 <lightning:input type="text" name="NameStr" label="Name" value=""/>
  22.                             </div>
  23.                             <div class="slds-form-element">
  24.                                 <lightning:input type="text" name="Address" label="Address" value=""/>
  25.                             </div>  
  26.                             <div class="slds-form-element__control" >
  27.                                 <lightning:input type="text" label="City" name="city" value=""/>
  28.                             </div>
  29.                             <div class="slds-form-element__control" >
  30.                                 <lightning:input type="text" label="State" name="state" value=""/>
  31.                             </div>
  32.                             <div class="slds-form-element__control" >
  33.                                 <lightning:input type="text" label="Pincode" name="pincode" value=""/>
  34.                             </div>
  35.                         </div>
  36.                     </div>
  37.                     <footer class="slds-modal__footer">
  38.                         <div class="slds-grid slds-wrap">
  39.                             <div class="slds-col slds-size_10-of-12 slds-text-align--left">
  40.                                 <p style="font-size:11px;">{!v.w3webMsg} <a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer"><strong>w3web.net</strong></a></p> 
  41.                             </div>
  42.                             <div class="slds-col slds-size_2-of-12"><lightning:button class="slds-button slds-button_destructive" label="Cancel" title="Cancel" onclick="{! c.cancelPopup}"/></div>
  43.                         </div>
  44.  
  45.  
  46.                     </footer>
  47.                 </div>
  48.             </section>
  49.             <div class="slds-backdrop slds-backdrop_open"></div>
  50.         </div>
  51.     </div>
  52. </aura:component>

Create JavaScript Controller

Step 5:- Create Lightning Component : parentComponentEventController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

parentComponentEventController.js [JavaScript Controller]

  1.      ({
  2. 	parentEventAction : function(component, event, helper) {         
  3.         var cmpMsg = event.getParam("cmpMsg"); 
  4.         var popFadeEvnt = event.getParam("popFadeEvnt");  
  5.         //alert('cmpMsg ' + cmpMsg + 'popFadeEvnt ' + popFadeEvnt);
  6.         component.set("v.w3webMsg", cmpMsg);           
  7.         component.set("v.modalFade", popFadeEvnt);
  8.     },
  9.  
  10.     cancelPopup:function(component, event, helper){        
  11.         component.set("v.modalFade",'slds-hide');
  12.     }
  13.  
  14. })

Create Lightning Application

Step 6:- Create Lightning Application : parentComponentEventApp.app

From Developer Console >> File >> New >> Lightning Application

parentComponentEventApp.app [Component Application File]

  1.    <aura:application extends="force:slds">
  2.     <c:parentComponentEvent/>
  3. </aura:application>

Passing mutiple attributes value to component type event -- w3web.net

Further post that would you like to learn in Salesforce

Create a Flyout Account, It will help you to grow make more money for a better living.

List your blog on Flyout and start making money for publishing sponsored content on your blog. Here’s the invite link. Get Started for Free

Check Out:- Earn Unlimited Everyday With This Trick:- Click Here

 

 

FAQ (Frequently Asked Questions)

What is the use of component event in Salesforce?

A component event is an event that is fired by a lightning component. A component event can either be handled by the component itself or it can be handled by any other component which is present in the hierarchy that receives the event.

How many types of component event are there?

There are two types of events in the framework: Component events are handled by the component itself or a component that instantiates or contains the component. Application events are handled by all components that are listening to the event.

How do you create a component event?

Create a sample component type event. Use type=”COMPONENT” in the aura:event tag for an component event. The attribute type is the one that will differentiate Component event from Application event.

What is the difference between the component event and the application event?

Component events can only be handled by components above them in the containment hierarchy so their usage is more localized to the components that need to know about them. Application events are best used for something that should be handled at the application level, such as navigating to a specific record.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

đź“Ś Salesforce Administrators

đź“Ś Salesforce Lightning Flow Builder

đź“Ś Salesforce Record Trigger Flow Builder

👉 Get Free Course →

đź“Ś Aura Lightning Framework

đź“Ś Lightning Web Component (LWC)

đź“Ś Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment