Sub topic 3
Adding row actions
Assume you know LWC templates, JavaScript event handlers, Apex wiring, and Salesforce record fields. lightning-datatable can expose actions for each row, such as View or Delete.
Row actions and layout
Add an action column with typeAttributes.rowActions. The event contains both the chosen action and its row.
const COLUMNS = [
{ label: 'Name', fieldName: 'Name' },
{
type: 'action',
typeAttributes: {
rowActions: [
{ label: 'View', name: 'view' },
{ label: 'Delete', name: 'delete' }
]
}
}
];
handleRowAction(event) {
const { name } = event.detail.action;
const row = event.detail.row;
if (name === 'view') {
console.log(`Open ${row.Id}`);
} else if (name === 'delete') {
console.log(`Delete ${row.Id}`);
}
}<lightning-datatable
key-field="Id"
data={data}
columns={columns}
onrowaction={handleRowAction}
column-widths-mode="auto"
min-column-width="80"
max-column-width="300"
show-row-number-column>
</lightning-datatable>column-widths-mode="auto" sizes columns from their content. Use fixed widths when stable alignment matters. Row numbers improve scanning but consume horizontal space.
Inline editing and infinite loading
Set editable: true on supported columns. The table places changes in draftValues; save them with updateRecord, then clear drafts and refresh the wired result.
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 (error) {
this.error = error;
}
}For large result sets, set enable-infinite-loading and handle onloadmore. Request the next page from Apex, append it to data, and set isLoading while the request runs. Stop loading when the server reports no more records.
Errors and custom types
Keep data empty when loading fails and render a useful message from error. For row-specific failures, add an errors object to the datatable and identify the affected row by its key field.
Built-in types cover common text, email, phone, URL, number, currency, and date values. Use a custom data type only when built-in formatting and typeAttributes cannot express the required cell markup or behavior. Custom types add maintenance and accessibility responsibilities.
Exercise
Which event supplies the action name and record for a row action?
Show answer
The rowaction event supplies them in event.detail.action.name and event.detail.row.