How to display a Modal/Popup on click button using aura method in lightning component | In lightning component how to display modal popup with a form inside using aura method in salesforce component

3,567 views

Hey guys, today in this post we are going to learn about how to display a Modal/Popup on click button with a form inside a lightning component using aura method.

Files we used in this post example:-

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

modalPopupParent.cmp

Lightning Component It is parent component of "childPopup.cmp".

modalPopupParentController.js

Lightning Component Controller It is used for click functionality that gets the value from child component to aura method.
childPopup.cmp Lightning Component It is a child component of "modalPopupParent.cmp" and hold aura method with action method.
childPopupController.js Lightning Component Controller It is used for display and close the Modal/Popup.

Final Output

show modal popup in lightning component -- w3web.net

 

You can download file directly from github by Click Here.

 

Other related post that would you like to learn in Salesforce

 

 

Step 1:- Create Lightning Application : modalPopupaApp.app

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

modalPopupaApp.app [Component Application File]

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

Step 2:- Create Lightning Component : modalPopupParent.cmp

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

modalPopupParent.cmp [Lightning Component File]

  1.    <aura:component>    
  2.     <c:childPopup aura:id="eventPopId"/>  
  3.     <div class="slds-p-around--medium">
  4.           <button class="slds-button slds-button--brand" onclick=" 
  5.       {!c.OpenEventPopup}">Open Event Popup</button>
  6.  
  7. <br/><br/>
  8.    <!--Start RelatedTopics Section-->
  9. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  10.  
  11.             <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>
  12.  
  13.             <br/><br/>
  14.             <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>
  15.             <div style="display:block; overflow:hidden;"> 
  16.                 <div style="width: 50%; float:left; display:inline-block">
  17.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  18.                         <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>
  19.                         <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>
  20.                         <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>
  21.                         <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>
  22.                         <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>
  23.                     </ul>
  24.             </div>
  25.  
  26.             <div style="width: 50%; float:left; display:inline-block">
  27.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  28.                         <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>
  29.                         <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>
  30.                         <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>
  31.                         <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>
  32.                         <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>
  33.                     </ul>
  34.                 </div>
  35.                <div style="clear:both;"></div> 
  36.                <br/>
  37.                 <div class="youtubeIcon">
  38.                     <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>
  39.                 </div>
  40.     </div>
  41.  
  42. </div>
  43.  
  44.   <!--End RelatedTopics Section-->
  45.  
  46.      </div>
  47.   </aura:component>

 

Step 3:- Create Lightning Component : modalPopupParentController.js

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

modalPopupParentController.js [JavaScript Controller]

  1.    ({
  2. 	OpenEventPopup : function(component, event, helper) {		
  3.         var modalFade1 = component.find('eventPopId');    
  4.         component.find("eventPopId").submitDR(modalFade1);
  5. 	},    
  6.   })

 

Step 4:- Create Lightning Component : childPopup.cmp

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

childPopup.cmp [Lightning Component File]

  1.    <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  2.  
  3.     <aura:method name="submitDR" action="{!c.openEventPopup}">        
  4.         <aura:attribute name="showModalFade" type="string"/>
  5.         <aura:attribute name="darkModalBackdrop" type="String"/>
  6.     </aura:method>
  7.  
  8.     <section role="dialog" class="slds-modal" aura:id="modalFade">
  9.         <div class="slds-modal__container">
  10.             <header class="slds-modal__header">
  11.                 <button class="slds-button slds-modal__close slds-button--icon-inverse" title="Close" onclick="{!c.closeEventPopup}">
  12.                     <c:svg class="slds-button__icon slds-button__icon--large" ariaHidden="true" xlinkHref="/resource/SLDS090/assets/icons/utility-sprite/svg/symbols.svg#close" />
  13.                 </button>
  14.  
  15.                 <h2 class="slds-text-heading_medium slds-hyphenate">Modal Header</h2>
  16.             </header>
  17.             <div class="slds-modal__content slds-p-around_medium">         
  18.                 <div class="slds">
  19.                     <div class="slds-grid slds-wrap">
  20.                         <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
  21.                             <div class="slds-form-element">
  22.                                 <label class="slds-form-element__label">Name</label>
  23.                                 <div class="slds-form-element__controller">
  24.                                     <ui:inputText class="slds-input" value="" aura:id="userName"/>
  25.                                 </div>
  26.                             </div>
  27.                         </div>            
  28.                     </div>
  29.  
  30.                     <div class="slds-grid slds-wrap">
  31.                         <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
  32.                             <div class="slds-form-element">
  33.                                 <label class="slds-form-element__label">Email</label>
  34.                                 <div class="slds-form-element__controller">
  35.                                     <ui:inputEmail class="slds-input" value="" aura:id="userEmail"/>
  36.                                 </div>
  37.                             </div>
  38.                         </div>
  39.  
  40.                     </div>
  41.  
  42.                     <div class="slds-grid slds-wrap">
  43.                         <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
  44.                             <div class="slds-form-element">
  45.                                 <label class="slds-form-element__label">Phone</label>
  46.                                 <div class="slds-form-element__controller">
  47.                                     <ui:inputPhone class="slds-input" value="" aura:id="userPhone"/>
  48.                                 </div>
  49.                             </div>
  50.                         </div>
  51.  
  52.                     </div>
  53.  
  54.                     <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
  55.                         <div class="slds-form-element">
  56.                             <label class="slds-form-element__label">State</label>
  57.                             <div class="slds-form-element__controller">
  58.                                 <ui:inputSelect class="slds-select" aura:id="userState" value="">
  59.                                     <ui:inputSelectOption text="--None--"/>
  60.                                     <ui:inputSelectOption text="Delhi"/>
  61.                                     <ui:inputSelectOption text="Haryana"/>
  62.                                     <ui:inputSelectOption text="Karnataka"/>
  63.                                 </ui:inputSelect>
  64.                             </div>
  65.                         </div>
  66.                     </div>
  67.  
  68.                     <div class="slds-form--inline slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
  69.                         <div class="slds-form-element" aura:id="redCheck">
  70.                             <div class="slds-form-element__control">
  71.                                 <div class="slds-checkbox">                      
  72.                                     <input type="checkbox" name="options" aura:id="checkbox1" id="checkbox1" value="" />
  73.                                     <label class="slds-checkbox__label" for="checkbox1">
  74.                                         <span class="slds-checkbox_faux"></span>
  75.                                         <span class="slds-form-element__label">Red</span>
  76.                                     </label>
  77.                                 </div>
  78.                             </div>                  
  79.                         </div>
  80.  
  81.                         <div class="slds-form-element" aura:id="greenCheck">
  82.                             <div class="slds-form-element__control">
  83.                                 <div class="slds-checkbox">                      
  84.                                     <input type="checkbox" name="options" aura:id="checkbox2" id="checkbox2" value=""  />
  85.                                     <label class="slds-checkbox__label" for="checkbox2">
  86.                                         <span class="slds-checkbox_faux"></span>
  87.                                         <span class="slds-form-element__label">Green</span>
  88.                                     </label>
  89.                                 </div>
  90.                             </div>                  
  91.                         </div>
  92.  
  93.                     </div>        
  94.  
  95.                     <div class="slds-form--inline slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
  96.                         <div class="slds-form-element">
  97.                             <div class="slds-form-element__control">
  98.                                 <span class="slds-radio">
  99.                                     <input type="radio" id="radio1" value="" name="default" checked="" />
  100.                                     <label class="slds-radio__label" for="radio1">
  101.                                         <span class="slds-radio_faux"></span>
  102.                                         <span class="slds-form-element__label">Radio Label One</span>
  103.                                     </label>
  104.                                 </span>                    
  105.                             </div>
  106.                         </div>
  107.  
  108.  
  109.                     </div>
  110.                 </div>
  111.  
  112.             </div>
  113.             <footer class="slds-modal__footer">
  114.                 <button class="slds-button slds-button--destructive" onclick="{!c.closeEventPopup}">Cancel</button>
  115.                 <button class="slds-button slds-button_brand">Save</button>
  116.             </footer>
  117.         </div>
  118.     </section>
  119.     <div class="slds-backdrop" aura:id="modalBackdrop"></div>
  120.  
  121. </aura:component>

 

Step 5:- Create Lightning Component : childPopupController.js

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

childPopupController.js [JavaScript Controller]

  1.    ({
  2. 	openEventPopup : function(component, event, helper) {
  3. 		var modalFade = component.find('modalFade');
  4.         var modalBackdrop = component.find('modalBackdrop');
  5.  
  6.         $A.util.addClass(modalFade,'slds-fade-in-open');
  7.         $A.util.addClass(modalBackdrop,'slds-backdrop_open');
  8. 	},
  9.  
  10.     closeEventPopup : function(component, event, helper) {
  11. 		var modalFade = component.find('modalFade');
  12.         var modalBackdrop = component.find('modalBackdrop');
  13.  
  14.         $A.util.removeClass(modalFade,'slds-fade-in-open');
  15.         $A.util.removeClass(modalBackdrop,'slds-backdrop_open');
  16. 	},    
  17.  
  18. })

 

 
 

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

How do I close the modal pop in Aura component?

The correct way to do that is to save the promise returned by showCustomModal() as an aura:attribute and use that to close the modal.

How do I create a modal popup in LWC?

Creating the modal popup is so easy in LWC and it's just few minutes of tasks. Modal Popup creates with the various methods will discuss only one of the way here. Create the Custom Component using VS code. In javascript, you only need to add or change in the value of the property.

What is force recordData?

A force:recordData component defines the parameters for accessing, modifying, or creating a record using Lightning Data Service. You have granular control on how you want to display or render the data in your custom component.

Related Topics | You May Also Like

  • Our Free Courses β†’
 
 


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