Hello friends, today we are going to discuss Add Buttons In LWC Datatable Salesforce. Here we will show the buttons in one column so users can perform different types of actions like edit, delete, and view records.
Also, check this: Call Lightning Component method from Visualforce Page
Key Highlights :
- Show a group of buttons.
- Perform actions on row level.
- Add icons in Buttons.
- Use button type columns.
Code :
AccountDataController.cls:
public class AccountDataController { @AuraEnabled(Cacheable = true) public static List<Account> getAccounts(){ return [SELECT Id, Name,Industry, Type, Phone, Rating, AccountNumber FROM Account ORDER BY Name LIMIT 10]; } }
buttonsInLWC.HTML:
<template> <lightning-card title="Add Buttons In LWC Datatable Salesforce" icon-name="standard:account"> <template if:true={data}> <lightning-datatable key-field="Id" data={data} columns={columns} hide-checkbox-column="true" show-row-number-column="true" onrowaction={callRowAction}> </lightning-datatable> </template> <template if:true={error}> {error}> </template> </lightning-card> </template>
buttonsInLWC.JS:
import { LightningElement, track, wire } from 'lwc'; import getAccounts from '@salesforce/apex/AccountDataController.getAccounts'; import { NavigationMixin } from 'lightning/navigation'; import { deleteRecord } from 'lightning/uiRecordApi'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import { refreshApex } from '@salesforce/apex'; const columns = [ { label: 'Name', fieldName: 'Name' }, { label: 'Phone', fieldName: 'Phone' }, { label: 'Type', fieldName: 'Type' }, { label: 'Rating', fieldName: 'Rating' }, { type: "button", label: 'View', initialWidth: 100, typeAttributes: { label: 'View', name: 'View', title: 'View', disabled: false, value: 'view', iconPosition: 'left', iconName:'utility:preview', variant:'Brand' } }, { type: "button", label: 'Edit', initialWidth: 100, typeAttributes: { label: 'Edit', name: 'Edit', title: 'Edit', disabled: false, value: 'edit', iconPosition: 'left', iconName:'utility:edit', variant:'Brand' } }, { type: "button", label: 'Delete', initialWidth: 110, typeAttributes: { label: 'Delete', name: 'Delete', title: 'Delete', disabled: false, value: 'delete', iconPosition: 'left', iconName:'utility:delete', variant:'destructive' } } ]; export default class ButtonsInLWC extends NavigationMixin(LightningElement) { @track data; @track wireResult; @track error; columns = columns; @wire(getAccounts) wiredAccounts(result) { this.wireResult = result; if (result.data) { this.data = result.data; } else if (result.error) { this.error = result.error; } } callRowAction(event) { const recId = event.detail.row.Id; const actionName = event.detail.action.name; if (actionName === 'Edit') { this.handleAction(recId, 'edit'); } else if (actionName === 'Delete') { this.handleDeleteRow(recId); } else if (actionName === 'View') { this.handleAction(recId, 'view'); } } handleAction(recordId, mode) { this[NavigationMixin.Navigate]({ type: 'standard__recordPage', attributes: { recordId: recordId, objectApiName: 'Account', actionName: mode } }) } handleDeleteRow(recordIdToDelete) { deleteRecord(recordIdToDelete) .then(result => { this.showToast('Success!!', 'Record deleted successfully!!', 'success', 'dismissable'); return refreshApex(this.wireResult); }).catch(error => { this.error = error; }); } showToast(title, message, variant, mode) { const evt = new ShowToastEvent({ title: title, message: message, variant: variant, mode: mode }); this.dispatchEvent(evt); } }
buttonsInLWC.JS-meta.xml:
<?xml version="1.0"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>57.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__HomePage</target> </targets> </LightningComponentBundle>
Output :
Reference :
What’s your Reaction?
+1
14
+1
2
+1
1
+1
+1
7
+1