• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

Building an Inline Editable Lightning Data Table in LWC

Building an Inline Editable Lightning Data Table in LWC
  • August 22, 2024
  • Tech Blogger
  • Salesforce Development & Implementation, Salesforce Lightning
  • 0

Introduction :

In this blog, we’ll walk you through the process of creating an inline editable Lightning Data Table in a Lightning Web Component (LWC). Inline editing is a powerful feature that allows users to edit records directly within the table without navigating to a different page or modal. By the end of this tutorial, you’ll have a functional Lightning Data Table that fetches contact records based on user input, allows inline editing, and updates the records in Salesforce.

Start by creating the HTML template for our LWC. The template consists of a lightning-card that contains a search input and a lightning-datatable component.

 Here is the code below.

<template>

<lightning-card title=”Search Contacts” icon-name=”custom:custom63″>

<div class=”slds-m-around_medium”>

<lightning-input

type=”search”

onchange={handleKeyChange}

class=”slds-m-bottom_small”

label=”Search”>

</lightning-input>

<template if:true={contacts}>

<div style=”height: 300px;”>

<lightning-datatable

key-field=”Id”

data={contacts}

columns={columns}

hide-checkbox-column=”true”

show-row-number-column=”true”

onsave={handleSave}

draft-values={draftValues}>

</lightning-datatable>

</div>

</template>

<template if:true={error}>

{error}>

</template>

</div>

</lightning-card>

</template>

In this template:

  • lightning-input: Captures the search input from the user.
  • lightning-datatable: Displays the contact records in a table format, with inline editing enabled on specific columns.

Now, let’s move on to the JavaScript controller where we’ll handle the data fetching, inline editing, and updating logic.

import { LightningElement, track } from ‘lwc’;

import getContacts from ‘@salesforce/apex/LWCDataTableExample.getContacts’;

import { updateRecord } from ‘lightning/uiRecordApi’;

import { refreshApex } from ‘@salesforce/apex’;

import { ShowToastEvent } from ‘lightning/platformShowToastEvent’;

 

const columns = [

{ label: ‘Id’, fieldName: ‘Id’ },

{ label: ‘First Name’, fieldName: ‘FirstName’, editable: true },

{ label: ‘Last Name’, fieldName: ‘LastName’, editable: true }

];

 

export default class LwcLightningDataTableDemo extends LightningElement {

@track contacts;

@track error;

@track columns = columns;

@track draftValues = [];

 

handleKeyChange(event) {

const strLastName = event.target.value;

if (strLastName) {

getContacts({ strLastName })

.then(result => {

this.contacts = result;

})

.catch(error => {

this.error = error;

});

} else {

this.contacts = undefined;

}

}

 

handleSave(event) {

const recordInputs = event.detail.draftValues.slice().map(draft => {

const fields = Object.assign({}, draft);

return { fields };

});

 

const promises = recordInputs.map(recordInput => updateRecord(recordInput));

 

Promise.all(promises).then(contacts => {

this.dispatchEvent(

new ShowToastEvent({

title: ‘Success’,

message: ‘All Contacts updated’,

variant: ‘success’

})

);

this.draftValues = []; // Clear draft values

return refreshApex(this.contacts); // Refresh the datatable with updated data

}).catch(error => {

this.dispatchEvent(

new ShowToastEvent({

title: ‘Error’,

message: ‘An error occurred while updating records’,

variant: ‘error’

})

);

});

}

}

Explanation of the JavaScript Controller:

  • @track: Used to track changes in properties that the template relies on. Here, we track contacts, error, columns, and draftValues.
  • handleKeyChange: This method is triggered when the user types in the search input. It calls an Apex method getContacts with the entered search term and updates the contacts property with the result.
  • handleSave: This method handles the save action when the user makes inline edits. It maps the draft values to record inputs, calls updateRecord to save the changes in Salesforce, and then refreshes the datatable to display the updated data.Here’s the XML configuration:<?xml version=”1.0″ encoding=”UTF-8″?>

 

<LightningComponentBundle xmlns=”http://soap.sforce.com/2006/04/metadata”   fqn=”lwcLightningDataTableDemo”>

<apiVersion>47.0</apiVersion>

<isExposed>true</isExposed>

<targets>

<target>lightning__HomePage</target>

<target>lightning__Tab</target>

</targets>

</LightningComponentBundle>

 

See how FieldAx can transform your Field Operations.

Try it today! Book Demo

You are one click away from your customized FieldAx Demo!

Conclusion :

we’ve created a Lightning Data Table with inline editing functionality using LWC. This feature enhances user experience by allowing quick edits directly within the table, without needing to navigate away from the page. You can extend this component further by adding additional columns, incorporating more complex logic, or enhancing error handling.

Author Bio

Tech Blogger
+ Recent Posts
  • May 9, 2025
    Mastering Attachment Compression for Salesforce Developers
  • May 2, 2025
    Salesforce API Integrations: Connect with Slack, Zoom, and Teams
  • Guide to Streamlining Sales Success
    April 17, 2025
    Mastering Salesforce CPQ: A Comprehensive Guide to Streamlining Sales Success
  • April 4, 2025
    Unlocking Salesforce Data Cloud: Unify and Activate Customer Data
Tags: LWCSalesforce Lightning Development
  • Previous How to Schedule Batch Jobs with Different CRON Expressions and Abort Batch Jobs
  • Next Top Salesforce Customization Trends to Watch in 2024
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Mastering Attachment Compression for Salesforce Developers May 9, 2025
Salesforce API Integrations: Connect with Slack, Zoom, and Teams May 2, 2025
Guide to Streamlining Sales Success
Mastering Salesforce CPQ: A Comprehensive Guide to Streamlining Sales Success April 17, 2025

Copyright @2023 Merfantz Technologies, All rights reserved