Sub topic 11
Editing cells inline and saving drafts
This chapter assumes you can already render a lightning-datatable from a wired Apex method, with a columns array and a key-field. You should also be comfortable with LWC event handlers and JavaScript destructuring.
How inline editing works
The table never writes to Salesforce. When a user edits a cell, the table keeps the change as a draft value. A draft is a small object that holds the key-field value and only the fields that changed. For example, { Id: '003xx0000012345', Phone: '555-0100' }.
While drafts exist, the table shows a Save and Cancel bar. Clicking Save fires a save event with all drafts in event.detail.draftValues. Your component saves them and then clears the drafts. Clicking Cancel discards the drafts and fires cancel.
Make columns editable
Add editable: true to each column the user may change. Leave it off for columns such as Name if you want them read-only.
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text', sortable: true },
{ label: 'Email', fieldName: 'Email', type: 'email', editable: true },
{ label: 'Phone', fieldName: 'Phone', type: 'phone', editable: true },
{ label: 'Birthdate', fieldName: 'Birthdate', type: 'date-local', editable: true }
];In the template, bind draft-values and handle onsave. Binding draft-values is what lets your component clear the bar after a successful save.
<template>
<lightning-card title="Contacts" icon-name="standard:contact">
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
draft-values={draftValues}
onsave={handleSave}
hide-checkbox-column>
</lightning-datatable>
</lightning-card>
</template>Save the drafts
Each draft already contains Id, because it is the key-field. That makes it a valid fields object for updateRecord. The handler below saves all drafts in parallel. On success, it clears the drafts and refreshes the wired Apex result.
import { updateRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
draftValues = [];
async handleSave(event) {
const updates = event.detail.draftValues.map((draft) => ({ fields: { ...draft } }));
try {
await Promise.all(updates.map((record) => updateRecord(record)));
this.draftValues = [];
await refreshApex(this.wiredResult);
} catch (e) {
this.error = e;
}
}refreshApex needs the whole wired result object. That is why the wire handler from the loading chapter stores this.wiredResult = result. A wired Apex method is cached, and updateRecord does not invalidate that cache. Without the refresh, the table shows the old values after the draft bar disappears.
Common errors
- Clearing drafts too early. If you set
draftValues = []before the saves finish, a failed save loses the user's edits. Clear drafts only after success. In thecatchblock, leave them in place so the user can retry. - Partial failure.
Promise.allrejects as soon as one update fails, but the other updates may already have been saved. The drafts still list every row. Re-saving them is harmless, but your error message should not claim that nothing changed. - Missing or wrong key-field. The draft's
Idcomes fromkey-field. If it names a field that is not a record Id,updateRecordfails. - Forgetting the refresh. The data looks stale after saving because the Apex result is cached.
- Editing a field the user cannot update. The edit appears to work, but the save fails on field-level security. The error from
updateRecordshows the cause. Mark a column as editable only when users can actually update that field. - Editing the wired data directly. Wired data is read-only. If you update the table optimistically, work on a copy, as you did when sorting.
Exercise
A user edits the Phone of two contacts and clicks Save. The first updateRecord call succeeds and the second fails. What should happen to draftValues, and why?
Show answer
Leave draftValues unchanged. The catch block sets the error and does not clear the drafts. The user keeps their edits and can retry. Clearing the drafts is safe only after every update succeeds. If you clear them before then, the failed edit is lost.