Creating a Lightning Datatable with Row Actions and Display a Modal Popup on Click View Icon Button in Salesforce Lightning Web Component – LWC | how to create lightning datatable row actions in lwc

12,377 views

Hey guys, today in this post we are going to learn about how to Create a lightning-datatable with Row Actions and Display a Modal Popup on Click View Icon Button in Salesforce Lightning Web Component — LWC.

Files we used in this post example

lwcDataTableRowAction.html Lightning Web Component HTML The Templae HTML file for used to write HTML element for user interface.
lwcDataTableRowAction.js Lightning Web Component JavaScript It is holding onclick functionality for display modal popup through click on icon view button .
lwcDataTableRowAction.js-meta.xml XML Meta File It is used for where this lightning web component you want to exposed.
lwcAppExampleApex.cls Apex Controller It is used for Fetching Contact records through @wire Decorators from database.
lwcApp.app Lightning Application It is used for call the component and preview on browser.

Live Demo

create lightning datatable row actions in lwc -- 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 Apex Controller : lwcAppExampleApex.cls

SFDX:Create Apex Class >> New >> lwcAppExampleApex.cls

lwcAppExampleApex.cls [Apex Class]

  1.   public WITH sharing class lwcAppExampleApex {
  2.  
  3.  
  4.     // CREATE a lightning-datatable AND display modal ON rowaction
  5.     @AuraEnabled(cacheable=TRUE)
  6.     public static List<Contact> getDataFromContact(){
  7.       List<Contact> conList = [SELECT Id, FirstName, LastName, Email, Phone FROM Contact LIMIT 10];
  8.       try{
  9.          RETURN conList;
  10.       }
  11.       catch(Exception e){
  12.          throw NEW AuraHandledException(e.getMessage());
  13.       }
  14.     }
  15.  
  16. }

Step 2:- Create Lightning Web Component : lwcDataTableRowAction.html

SFDX:Lightning Web Component >> New >> lwcDataTableRowAction.html

lwcDataTableRowAction.html [Lightning Web Component HTML]

  1.   <template>
  2.     <lightning-card title="Create a lightning-datatable and display modal popup on rowaction in LWC" icon-name="custom:custom14">
  3.  
  4.         <lightning-datatable data={wireContact.data} columns={columns} key-field="id" hide-checkbox-column="true"  onrowaction={handleRowAction}></lightning-datatable>
  5.  
  6.         <template if:true={modalContainer}>
  7.             <section  class="slds-modal slds-fade-in-open">
  8.                 <div class="slds-modal__container">
  9.                     <header class="slds-modal__header">
  10.                         <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" title="Close" onclick={closeModalAction}>
  11.                            <lightning-icon icon-name="utility:close" alternative-text="close" variant="inverse" size="small" ></lightning-icon>
  12.                         </button>
  13.                         <h2 class="slds-text-heading_medium slds-hyphenate">Contact Record Detail</h2>
  14.                      </header>
  15.  
  16.                      <div class="slds-modal__content slds-p-around_medium">
  17.                         <table class="slds-table slds-table_bordered slds-table_col-bordered slds-table_cell-buffer">
  18.                            <thead> 
  19.                             <tr>
  20.                                 <th>First Name</th>
  21.                                 <th>Last Name</th>
  22.                                 <th>Email Name</th>
  23.                                 <th>Phone Name</th>
  24.                             </tr>
  25.                         </thead>
  26.                         <tbody> 
  27.                             <tr>
  28.                                 <td>{contactRow.FirstName}</td>
  29.                                 <td>{contactRow.LastName}</td>
  30.                                 <td>{contactRow.Email}</td>
  31.                                 <td>{contactRow.Phone}</td>
  32.                             </tr>
  33.                         </tbody>   
  34.                         </table>
  35.                      </div>
  36.                      <footer class="slds-modal__footer">
  37.                         <lightning-button variant="brand" label="Close" title="Close"  onclick={closeModalAction}></lightning-button>
  38.                    </footer> 
  39.                 </div>
  40.             </section>
  41.             <div class="slds-backdrop slds-backdrop_open"></div>
  42.         </template>
  43.  
  44.         <br/><br/>
  45.    <!--Start RelatedTopics Section-->
  46. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  47.  
  48.             <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>
  49.  
  50.             <br/><br/>
  51.             <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>
  52.             <div style="display:block; overflow:hidden;"> 
  53.                 <div style="width: 50%; float:left; display:inline-block">
  54.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  55.                         <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>
  56.                         <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>
  57.                         <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>
  58.                         <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>
  59.                         <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>
  60.                     </ul>
  61.             </div>
  62.  
  63.             <div style="width: 50%; float:left; display:inline-block">
  64.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  65.                         <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>
  66.                         <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>
  67.                         <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>
  68.                         <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>
  69.                         <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>
  70.                     </ul>
  71.                 </div>
  72.                <div style="clear:both;"></div> 
  73.                <br/>
  74.                 <div class="youtubeIcon">
  75.                     <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>
  76.                 </div>
  77.     </div>
  78.  
  79. </div>
  80.  
  81.   <!--End RelatedTopics Section-->
  82.     </lightning-card>
  83. </template>

Step 3:- Create Lightning Web Component : lwcDataTableRowAction.js

SFDX:Lightning Web Component >> New >> lwcDataTableRowAction.js

lwcDataTableRowAction.js [LWC JavaScript File]

  1.   import { LightningElement, track, wire } from 'lwc';
  2. import getDataFromContact from '@salesforce/apex/lwcAppExampleApex.getDataFromContact';
  3. const columns=[
  4.     {
  5.         label: 'View',
  6.         type: 'button-icon',
  7.         initialWidth: 75,
  8.         typeAttributes: {
  9.             iconName: 'action:preview',
  10.             title: 'Preview',
  11.             variant: 'border-filled',
  12.             alternativeText: 'View'
  13.         }
  14.       },
  15.       {
  16.         label: 'First Name',
  17.         fieldName: 'FirstName'
  18.     },
  19.     {
  20.         label: 'Last Name',
  21.         fieldName: 'LastName'
  22.     },
  23.     {
  24.         label: 'Email',
  25.         fieldName: 'Email'
  26.     },
  27.     {
  28.         label: 'Phone',
  29.         fieldName: 'Phone'
  30.     }
  31. ];
  32.  
  33. export default class LwcDataTableRowAction extends LightningElement {
  34.   @track columns = columns;
  35.   @track contactRow={};
  36.   @track rowOffset = 0;  
  37.   @track modalContainer = false;
  38.    @wire(getDataFromContact) wireContact;
  39.  
  40.    handleRowAction(event){
  41.       const dataRow = event.detail.row;
  42.       window.console.log('dataRow@@ ' + dataRow);
  43.       this.contactRow=dataRow;
  44.       window.console.log('contactRow## ' + dataRow);
  45.       this.modalContainer=true;
  46.    }
  47.  
  48.    closeModalAction(){
  49.     this.modalContainer=false;
  50.    }
  51.  
  52. }

Step 4:- Create Lightning Web Component : lwcDataTableRowAction.js-meta.xml

SFDX:Lightning Web Component >> New >> lwcDataTableRowAction.js-meta.xml

lwcDataTableRowAction.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>

Step 5:- Create Lightning Application : lwcApp.app

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

lwcApp.app [Component Application File]

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

create lightning datatable row actions in lwc -- w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

What is lightning table in Salesforce?

Show up to 200 records and 10 columns and from any field in a source report's report type. After grouping data in a Lightning table, you can add measures (sum, average, minimum, and maximum) to the grouped data.

What is Datatable in LWC?

lightning-datatable displays tabular data where each column renders the content based on the data type. The default data type on a column is text .

How do you sort data in lightning Datatable?

Lightning datatable provides an onsort attribute which allow us to implement the sorting in lightning datatable. To enable the sorting on row you need to set sortable to true for the column and set sorted-By to match the fieldName attribute on the column.

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