How to Create Custom Accordion Expand, Collapse and Toggle Section in Lightning Component | create lightning component Accordion Expand/Collapse/Toggle section in salesforce lightning component

2,564 views


Hey guys, today in this post we are going to learn about how to create a custom accordion Expand Collapse and Toggle section using JavaScript in Salesforce lightning component.

Files we used in this post example:-

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

customAccordionCmp.cmp

Lightning Component It is used for create a accordion list item section.

customAccordionCmpcontroller.js

Lightning Component Controller It is used for click functionality that is Expand Collapse and Toggle section in Lightning component.
customAccordionCmp.CSS Component Style CSS It is used for create custom Style CSS for Expandable/Collapsible & Toggle (Accordion) section.

Final Output

custom accordion expand collapse and toggle lightning component -- w3web.net

 

You can download file directly from github by Click Here.

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

 

 

 

Other related post that would you like to learn in Salesforce

 

Step 1:- Create Lightning Application : customAccordionApp.app

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

customAccordionApp.app [Component Application File]

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

 

Step 2:- Create Lightning Component : customAccordionCmp.cmp

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

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

customAccordionCmp.cmp [Lightning Component File]

  1.    <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  2.     <div class="slds">
  3.         <div class="slds-grid slds-wrap">  
  4.             <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12">
  5.                 <ul class="slds-accordion w3webAccordion" id="w3webAccordionListOver">
  6.                     <li class="slds-accordion__list-item" id="w3webListItem0">
  7.                         <div class="slds-accordion__summary">                
  8.                             <h3 class="slds-summary-heading" name="w3webListItem0" onclick="{!c.accordionAction}">Editing, Saving and Removing rows Dynamically in Lightning component</h3>  
  9.                             <div class="accordionContent">
  10.                                 <div class="postImage">					
  11.                                 <a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/">
  12.                                     <img src="https://www.w3web.net/wp-content/uploads/2020/07/editDeleteSave.png" width="200" height="150"/>
  13.                                 </a>
  14.                             </div> 
  15.                                 <p>In this post we are going to learn about that how to edit row, saving row or removing row dynamically in Salesforce lightning component.</p><br/>
  16.                                 <p>In this example we will customize the same component and achieve to the editing row, saving row and removing rows functionality of dynamically on Custom sObject by help of wrapper apex class and JavaScript Controller in lightning component...<span class="readMore"><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/">Read more...</a></span></p>
  17.                             </div>
  18.                         </div>    
  19.                     </li>
  20.  
  21.                     <li class="slds-accordion__list-item" id="w3webListItem1">                
  22.                         <h3 class="slds-summary-heading" name="w3webListItem1" onclick="{!c.accordionAction}"> How to validate child component from parent component</h3>  
  23.                         <div class="accordionContent">
  24.                             <div class="postImage">					
  25.                                 <a href="https://www.w3web.net/how-to-validate-child-component-from-parent-component/">
  26.                                     <img src="https://www.w3web.net/wp-content/uploads/2020/08/stylishFormValidation.png" width="200" height="150"/>
  27.                                 </a>
  28.                             </div> 
  29.                             <p>In this post we are going to learn about how to validate child component from parent component on click button using aura method in Salesforce lightning component.</p><br/>
  30.                             <p>Real time scenarios:- Create a custom and stylish form validation and validate child component from parent component using aura method in lightning component...<span class="readMore"><a href="https://www.w3web.net/how-to-validate-child-component-from-parent-component/">Read more...</a></span> </p>
  31.                         </div>                
  32.                     </li>
  33.  
  34.                     <li class="slds-accordion__list-item" id="w3webListItem2">                
  35.                         <h3 class="slds-summary-heading" name="w3webListItem2" onclick="{!c.accordionAction}">Trigger to update count of child records with custom field of parent object.</h3>  
  36.                         <div class="accordionContent">
  37.                              <div class="postImage">					
  38.                                 <a href="https://www.w3web.net/roll-up-summary-trigger-on-custom-object/">
  39.                                     <img src="https://www.w3web.net/wp-content/uploads/2020/08/employeeSizeTrigger.png" width="200" height="150"/>
  40.                                 </a>
  41.                             </div>
  42.                             <p>In this post we are going to learn about how to update count of child records with custom field value into parent object using trigger map list in Salesforce.</p><br/>
  43.                             <p><strong>Real time scenarios:-</strong> Write a trigger on parent object where create a <strong>custom field</strong> as Employee (Number type).</p><br/>                            
  44.                             <p>Or if user update the value of employee <strong>less then</strong> the total number of child records, in this case the child records should be <strong>exist only equal to </strong> employee size, rest records of child object should be automatic removed. <span class="readMore"><a href="https://www.w3web.net/update-count-of-child-record-based-on-parent-object-value/">Read more...</a></span></p>
  45.                         </div>               
  46.                     </li>
  47.  
  48.                     <li class="slds-accordion__list-item" id="w3webListItem3">                
  49.                         <h3 class="slds-summary-heading" name="w3webListItem3" onclick="{!c.accordionAction}">Create rollup summary using Apex trigger on custom object</h3>  
  50.                         <div class="accordionContent">
  51.                             <div class="postImage">					
  52.                                 <a href="https://www.w3web.net/roll-up-summary-trigger-on-custom-object/">
  53.                                     <img src="https://www.w3web.net/wp-content/uploads/2020/08/rollupSummary-trigger.png" width="200" height="150"/>
  54.                                 </a>
  55.                             </div>
  56.                             <p>In this post we are going to learn about How to create roll-up summary trigger for <b>count child records</b> on custom object using Apex trigger in Salesforce.</p><br/>
  57.                             <p><strong>Real time scenarios:-</strong> Write a trigger on parent object where create a <strong>custom field</strong> as Employee (Number type).</p><br/>                            
  58.                             <p><strong>Real time scenarios:-</strong> Write a roll-up summary trigger for count child records on custom parent object. Create a custom field (Number Type) on parent object, <strong>calculate the total number</strong> of related <strong>child records</strong> and put into them...<span class="readMore"><a href="https://www.w3web.net/roll-up-summary-trigger-on-custom-object/">Read more...</a></span></p>
  59.                         </div>               
  60.                     </li>
  61.  
  62.                     <li class="slds-accordion__list-item" id="w3webListItem4">                
  63.                         <h3 class="slds-summary-heading" name="w3webListItem4" onclick="{!c.accordionAction}">How to fetch picklist values from apex controller in lightning component</h3>  
  64.                         <div class="accordionContent">
  65.                             <div class="postImage">					
  66.                                 <a href="https://www.w3web.net/roll-up-summary-trigger-on-custom-object/">
  67.                                     <img src="https://www.w3web.net/wp-content/uploads/2020/07/picklistValue.png" width="200" height="150"/>
  68.                                 </a>
  69.                             </div>
  70.                             <p>In this post we are going to learn about how to retrieve Picklist values from Apex controller in Lightning Component...<span class="readMore"><a href="https://www.w3web.net/fetch-picklist-values-dynamically/">Read more...</a></span></p>                           
  71.                         </div>               
  72.                     </li>
  73.  
  74.                 </ul>
  75.             </div>  
  76.         </div>  
  77. <br/> <br/>
  78.    <!--Start RelatedTopics Section-->
  79. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  80.  
  81.             <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>
  82.  
  83.             <br/><br/>
  84.             <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>
  85.             <div style="display:block; overflow:hidden;"> 
  86.                 <div style="width: 50%; float:left; display:inline-block">
  87.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  88.                         <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>
  89.                         <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>
  90.                         <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>
  91.                         <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>
  92.                         <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>
  93.                     </ul>
  94.             </div>
  95.  
  96.             <div style="width: 50%; float:left; display:inline-block">
  97.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  98.                         <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>
  99.                         <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>
  100.                         <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>
  101.                         <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>
  102.                         <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>
  103.                     </ul>
  104.                 </div>
  105.                <div style="clear:both;"></div> 
  106.                <br/>
  107.                 <div class="youtubeIcon">
  108.                     <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>
  109.                 </div>
  110.     </div>
  111.  
  112. </div>
  113.  
  114.   <!--End RelatedTopics Section-->
  115.  
  116.     </div>      
  117. </aura:component>

 

Step 3:- Create Lightning Component : customAccordionCmpcontroller.js

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

customAccordionCmpcontroller.js [JavaScript Controller]

  1.    ({
  2. 	accordionAction : function(component, event, helper) {
  3. 		var thisObj = event.target.name;
  4.         var w3webAccordionListOver = document.getElementById('w3webAccordionListOver');
  5.         var accordionListAll =  w3webAccordionListOver.querySelectorAll('.slds-accordion__list-item');
  6.         //alert(accordionListAll.length);
  7.  
  8.         var conainActive = document.getElementById(thisObj).classList.contains('activeRow');
  9.         for(var i=0; i<accordionListAll.length; i++){
  10.             accordionListAll[i].classList.remove('activeRow');
  11.         }
  12.  
  13.         if(conainActive == true){
  14.             document.getElementById(thisObj).classList.remove('activeRow');
  15.         }else{
  16.             document.getElementById(thisObj).classList.toggle('activeRow');        
  17.         }
  18.  
  19. 	}
  20. })

 

Step 4:- Create Lightning Component Style: customAccordionCmp.CSS

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

customAccordionCmp.CSS [Style CSS]

  1.    .THIS {
  2.     background:#fff !important;
  3.  }
  4.  
  5. .THIS .w3webAccordion {margin:0; padding:0; list-style:none;}
  6. .THIS .w3webAccordion li{padding:5px 0 5px 0;}
  7. .THIS .w3webAccordion li .slds-summary-heading{display:block; padding:0 0 0 20px; font-size:17px; position:relative;} 
  8. .THIS .w3webAccordion li .slds-summary-heading:before {content:''; width:17px; height:17px; display:inline-block; background:url(/resource/SLDS2016/assets/icons/utility/chevronright_60.png) no-repeat left top; background-size:cover; cursor: pointer; position:absolute; left:0; top:5px;}
  9.  
  10. .THIS .w3webAccordion li .slds-summary-heading:hover{color:#04a5ca; cursor:pointer;}
  11. .THIS .w3webAccordion .slds-accordion__summary{display:initial;}
  12. .THIS .w3webAccordion .slds-accordion__list-item .accordionContent{display:none; overflow:hidden;}
  13. .THIS .w3webAccordion li.activeRow .slds-summary-heading{color:#04a5ca;}
  14. .THIS .w3webAccordion li.activeRow .accordionContent{display:block; padding:5px 0 5px 20px; font-size:14px;}
  15. .THIS .w3webAccordion li.activeRow .accordionContent .postImage{display: inline-block; float: left; margin-right: 10px;}
  16. .THIS .w3webAccordion li.activeRow .slds-summary-heading:before {content:''; width:18px; height:17px; display:inline-block; background:url(/resource/SLDS2016/assets/icons/utility/chevrondown_60.png) no-repeat left top; background-size:cover; cursor: pointer; position:absolute; left:0; top:5px;}
  17. .THIS .readMore{font-size:14px; font-weight:bold; display:inline-block; padding:0 0 0 10px;}
  18. .THIS .readMore a{color:#ff0000; text-decoration:none;}
  19. .THIS .readMore a:hover{color:#04a5ca; text-decoration:underline;}

custom accordion expand collapse and toggle lightning component -- w3web.net

 

 

Further post that would you like to learn in Salesforce

 
 

 


 

FAQ (Frequently Asked Questions)

What is accordion in Salesforce?

An accordion allows a user to toggle the display of a section of content. Salesforce Lightning Accordion: lightning accordion component groups related content in a single container. Only one accordion section is expanded at a time. When you select a section, it's expanded or collapsed.

What is collapsible section in Salesforce?

Collapsible sections let users collapse or expand sections on their record detail pages by using the arrow icon next to the section heading. Sections remain expanded or collapsed until the user changes the settings for that tab.

What is collapse list?

A collapsible list will show and hide a given section of content when its heading is clicked. The collapsible list will appear initially as a series of Heading 5s or Heading 6s with an arrow next to each.

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