How to create custom expandable/collapsible accordion section uses of lightning-accordion tags in Lightning Web Components — LWC | How to create custom expandable/collapsible section in lwc

6,615 views

Hey guys, today in this post we are going to learn about how to create custom expandable/collapsible accordion section uses of lightning-accordion tags in LWC (Lightning web components).

Here I am display a vertically lightning-accordion sections of content that you can expand and collapse.

Click a section’s vertically tab to expand its related content. You can control the visible only one section at a time.

Why Should You Schedule Meeting?

🎯 If You Are Facing Any Of These 6 Challenges. Schedule Meeting With Me.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

Files we used in this post example

lwcAccordionSection.html Lightning Web Component HTML Template HTML file for used to write HTML element for user interface.
lwcAccordionSection.js Lightning Web Component JavaScript It is holding onclick functionality for Create Custom Expandable/Collapsible Section.
lwcAccordionSection.js-meta.xml XML Meta File It is used for where this lightning web component you want to exposed.
lwcAccordionSection.css Style CSS File It is used for fixed the alignmet issues of Expandable/Collapsible box and Image.

Final Output

custom accordion section in lwc -- www.w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

Create Lightning Web Component HTML

Step 1:- Create Lightning Web Component HTML ➑ lwcAccordionSection.html

SFDX:Lightning Web Component ➑ New ➑ lwcAccordionSection.html

lwcAccordionSection.html [Lightning Web Component HTML]

  1.   <template>
  2.    <div class="slds-p-around--medium"> 
  3.     <h3 class="slds-text-heading_medium"><strong style="color:#270086;">Create a  Vertical Accordion Section in Lightning Web Component (LWC) </strong></h3> 
  4.  
  5.     <lightning-accordion onsectiontoggle={accordionToggleSectionW3web} active-section-name="lightningComponent">
  6.  
  7.         <lightning-accordion-section name="javascriptJQuery" label="Javascript & JQuery">
  8.             <div class="tabContent">
  9.                 <div class="postImage">					
  10.                     <a href="https://www.w3web.net/delete-cross-object-records-of-selected-row-in-salesforce/" target="_blank" rel="noopener noreferrer">
  11.                         <img src="https://www.w3web.net/wp-content/uploads/2020/10/addRemoveRowsFeature.png" width="200" height="150"/>
  12.                     </a>
  13.                 </div> 
  14.               <div class="imgRt"> 
  15.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Javascript and JQuery</strong></h3> 
  16.                 <p class="titleTag"><b><a href="https://www.w3web.net/delete-cross-object-records-of-selected-row-in-salesforce/" target="_blank" rel="noopener noreferrer">Add Or Remove Row Dynamically in JavaScript JQuery</a></b></p><br/>
  17.                 <p>Hey guys, today in this post we are going to learn about How to Add Or Remove Row Dynamically in HTML Order List Using javascript jquery With Row Count (index) Value.</p><br/>                      
  18.                 <p>Creating HTML Order List table and delete row on cross button...<span class="readMore"><a href="https://www.w3web.net/delete-cross-object-records-of-selected-row-in-salesforce/" target="_blank" rel="noopener noreferrer">Read more...</a></span></p>
  19.               </div>    
  20.             </div>
  21.         </lightning-accordion-section>
  22.  
  23.         <lightning-accordion-section name="lightningComponent" label="Lightning Component">
  24.  
  25.             <div class="tabContent">
  26.                 <div class="postImage">					
  27.                     <a href="https://www.w3web.net/update-count-of-child-record-based-on-parent-object-value/" target="_blank" rel="noopener noreferrer">
  28.                         <img src="https://www.w3web.net/wp-content/uploads/2020/08/employeeSizeTrigger.png" width="200" height="150"/>
  29.                     </a>
  30.                 </div> 
  31.                <div class="imgRt"> 
  32.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Lightning Component</strong></h3>    
  33.                 <p class="titleTag"><b><a href="https://www.w3web.net/update-count-of-child-record-based-on-parent-object-value/" target="_blank" rel="noopener noreferrer">Trigger to update count of child records of parent object</a></b></p><br/>
  34.                 <p>Hey guys, today 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/>
  35.                 <p>Real time scenarios:- Write a trigger on parent object where create a custom field as Employee (Number type).</p><br/>
  36.                 <p>If user insert the parent record, the child record should be automatic inserted equal to the same value of employee...<span class="readMore"><a href="https://www.w3web.net/update-count-of-child-record-based-on-parent-object-value/" target="_blank" rel="noopener noreferrer">Read more...</a></span></p>
  37.                 </div>    
  38.             </div>
  39.         </lightning-accordion-section>  
  40.  
  41.         <lightning-accordion-section name="salesforceLWC" label="Salesforce LWC">            
  42.             <div class="tabContent">
  43.                 <div class="postImage">					
  44.                     <a href="https://www.w3web.net/deleting-multiple-records-with-checkbox-in-lwc/" target="_blank" rel="noopener noreferrer">
  45.                         <img src="https://www.w3web.net/wp-content/uploads/2020/12/deleteMultipleRecordFeature.jpg" width="200" height="150"/>
  46.                     </a>
  47.                 </div> 
  48.                <div class="imgRt"> 
  49.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Salesforce LWC</strong></h3>  
  50.                 <p class="titleTag"><b><a href="https://www.w3web.net/deleting-multiple-records-with-checkbox-in-lwc/" target="_blank" rel="noopener noreferrer">Deleting multiple records dynamically with checkbox in LWC</a></b></p><br/>
  51.                 <p>Hey guys, today in this post we are going to learn about Deleting multiple records dynamically functionality with checkbox on delete button in lightning web component Salesforce β€” LWC.</p><br/>
  52.  
  53.                 <p>Here user can delete multiple record using selected checkbox ...<span class="readMore"><a href="https://www.w3web.net/deleting-multiple-records-with-checkbox-in-lwc/" target="_blank" rel="noopener noreferrer">Read more...</a></span></p>
  54.               </div>     
  55.             </div>
  56.         </lightning-accordion-section>
  57.  
  58.         <lightning-accordion-section name="visualforcePage" label="Visualforce Page">
  59.  
  60.             <div class="tabContent">
  61.                 <div class="postImage">					
  62.                     <a href="https://www.w3web.net/create-custom-controller-extension-in-visualforce/" target="_blank" rel="noopener noreferrer">
  63.                         <img src="https://www.w3web.net/wp-content/uploads/2021/02/createAccVisualforceFeature.jpg" width="200" height="150"/>
  64.                     </a>
  65.                 </div> 
  66.               <div class="imgRt"> 
  67.                 <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Visualforce Page</strong></h3>
  68.                 <p class="titleTag"><b><a href="https://www.w3web.net/create-custom-controller-extension-in-visualforce/" target="_blank" rel="noopener noreferrer">Create Account Record and get the data from server in Visualforce.</a></b></p><br/>
  69.                 <p>Hey guys, today in this post we are going to learn about How to Create Account Record and get the data from server Using Standard Controllers and Extension in Salesforce Visualforce Page.</p><br/>
  70.                 <p>Real time scenarios:- Create account record and get data using Visualforce Controller Extension. <span class="readMore"><a href="https://www.w3web.net/create-custom-controller-extension-in-visualforce/" target="_blank" rel="noopener noreferrer">Read more...</a></span></p><br/>
  71.               </div> 
  72.             </div>
  73.         </lightning-accordion-section>
  74.  
  75.         <template if:true={isDVisible}>
  76.             <lightning-accordion-section name="apexTrigger" label="Apex Trigger">
  77.  
  78.                 <div class="tabContent">
  79.                     <div class="postImage">					
  80.                         <a href="https://www.w3web.net/trigger-to-send-email-when-record-is-created/" target="_blank" rel="noopener noreferrer">
  81.                             <img src="https://www.w3web.net/wp-content/uploads/2021/01/triggerEmailTemplFeature.jpg" width="200" height="150"/>
  82.                         </a>
  83.                     </div> 
  84.                   <div class="imgRt">  
  85.                     <h3 class="slds-text-heading_medium slds-text-color--error"><strong>Apex Trigger</strong></h3> 
  86.                     <p class="titleTag"><b><a href="https://www.w3web.net/trigger-to-send-email-when-record-is-created/" target="_blank" rel="noopener noreferrer">Trigger to Send a Custom Email Template</a></b></p><br/>
  87.                     <p>Hey guys, today in this post we are going to learn about Apex Trigger to Send a Custom Visualforce Component Email Template when Record is Created on Custom Object in Salesforce.</p><br/>                      
  88.                     <p>Real time scenarios:- Write a trigger on Custom object (Registration__c) and Send a Custom Visualforce Component Email Template when Record is Created Condition is the Student Name should not be null....<span class="readMore"><a href="https://www.w3web.net/trigger-to-send-email-when-record-is-created/" target="_blank" rel="noopener noreferrer">Read more...</a></span></p>
  89.                   </div>   
  90.                 </div>
  91.             </lightning-accordion-section>            
  92.         </template>        
  93.     </lightning-accordion>
  94.     <div class="slds-text-align_center slds-m-top--medium"> <lightning-button variant="brand" onclick={toggleSectionJquery} label="Toggle Section"></lightning-button></div>
  95.  
  96. <br/><br/>
  97.    <!--Start RelatedTopics Section-->
  98. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  99.  
  100.             <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>
  101.  
  102.             <br/><br/>
  103.             <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>
  104.             <div style="display:block; overflow:hidden;"> 
  105.                 <div style="width: 50%; float:left; display:inline-block">
  106.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  107.                         <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>
  108.                         <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>
  109.                         <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>
  110.                         <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>
  111.                         <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>
  112.                     </ul>
  113.             </div>
  114.  
  115.             <div style="width: 50%; float:left; display:inline-block">
  116.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  117.                         <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>
  118.                         <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>
  119.                         <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>
  120.                         <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>
  121.                         <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>
  122.                     </ul>
  123.                 </div>
  124.                <div style="clear:both;"></div> 
  125.                <br/>
  126.                 <div class="youtubeIcon">
  127.                     <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>
  128.                 </div>
  129.     </div>
  130.  
  131. </div>
  132.  
  133.   <!--End RelatedTopics Section-->
  134. </div>  
  135. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➑ lwcAccordionSection.js

SFDX:Lightning Web Component ➑ New ➑ lwcAccordionSection.js

lwcAccordionSection.js [LWC JavaScript File]

  1.  import { LightningElement } from 'lwc';
  2.  
  3. export default class LightningExampleAccordionConditional extends LightningElement {
  4.     activeSectionMessage = '';
  5.     isDVisible = false;
  6.  
  7.     accordionToggleSectionW3web(event) {
  8.         this.activeSectionMessage =
  9.             'Open section name:  ' + event.detail.openSections;
  10.     }
  11.  
  12.     toggleSectionJquery() {
  13.         this.isDVisible = !this.isDVisible;
  14.     }
  15.  
  16.     get isMessageVisible() {
  17.         return this.activeSectionMessage.length > 0;
  18.     }
  19. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➑ lwcAccordionSection.js-meta.xml

SFDX:Lightning Web Component ➑ New ➑ lwcAccordionSection.js-meta.xml

lwcAccordionSection.js-meta.xml [LWC Meta Data XML]

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

Create Lightning Web Component Style CSS

Step 4:- Create Lightning Web Component Style CSS ➑ lwcAccordionSection.css

SFDX:Lightning Web Component ➑ New ➑ lwcAccordionSection.css

lwcAccordionSection.css [Style CSS]

  1.   .slds-vertical-tabs__nav .slds-vertical-tabs__nav-item.slds-is-active, .THIS .slds-vertical-tabs__nav .slds-vertical-tabs__nav-item a:focus{color:rgb(194, 57, 52); font-weight:bold; text-decoration:none;}
  2. h3.slds-text-heading_medium{padding:5px 0 5px 10px; background:#eee; margin-bottom:10px; display:block;}
  3. .slds-tabs_scoped__item.slds-is-active .slds-tabs_scoped__link{color:#b70e01; text-decoration:none;}
  4. .tabContent{display:block; padding:15px 0 5px 0px; font-size:14px; overflow: hidden;}
  5. .tabContent .postImage{display: inline-block; float: left; margin-right: 20px;}
  6. .readMore{font-size:14px; font-weight:bold; display:inline-block; padding:0 0 0 10px;}
  7. .readMore a{color:#ff0000; text-decoration:none;}
  8. .readMore a:hover{color:#04a5ca; text-decoration:underline;}
  9. .titleTag{font-size:18px;}
  10. .imgRt{width:55%; float:left;}

Create Lightning Application

Step 5:- Create Lightning Application : lwcAccordionSectionApp.app

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

lwcAccordionSectionApp.app [Component Application File]

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

lwc custom accordion expandable/collapsible section -- www.w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

Why is collapsed by default greyed out?

Why is collapsed by default greyed out? Place the cursor in a heading formatted with the heading level you want to collapse (such as Heading 1) and click the 'Paragraph Settings' dialog box button in the β€œParagraph” section of the β€œHome” tab.

What are the three different custom tabs you can create?

You can create three different kinds of custom tabs: custom object tabs, Web tabs, and Visualforce page tabs. Custom object tabs allow you to find, add, change, and delete the data in your custom objects.

How do I remove the Expand Group collapse folder?

Open File Explorer and navigate to the folder where you wish to disable file grouping. Right-click anywhere in the folder. Select Group by > None.

Related Topics | You May Also Like

 
  

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