Sub topic 9
Adding row actions
This chapter assumes you can already render a lightning-datatable with a columns array and a key-field. It also assumes you know how to wire an Apex method and store the wired result, as in the earlier loading chapter.
What a row action is
A row action is a menu item attached to a single row. Row actions suit tasks such as viewing or deleting a record, where a checkbox selection would be awkward. The table shows a menu button at the end of each row. When the user picks an item, the table fires a rowaction event. Your component decides what the action does.
Defining the action column
Row actions live in a column of type action. The column has no fieldName because it does not display a field. Its typeAttributes.rowActions property lists the menu items. Each item needs a label and a name. The name is the value your handler checks.
const COLUMNS = [
{ label: 'Name', fieldName: 'Name' },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{
type: 'action',
typeAttributes: {
rowActions: [
{ label: 'View', name: 'view' },
{ label: 'Delete', name: 'delete' }
]
}
}
];Each action can also set iconName and disabled. You can set menuAlignment in typeAttributes to control which edge the menu opens from.
Handling the event
Bind a handler to onrowaction in the template.
<template>
<lightning-card title="Contacts" icon-name="standard:contact">
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
hide-checkbox-column
onrowaction={handleRowAction}>
</lightning-datatable>
</lightning-card>
</template>The event detail has two properties. detail.action.name is the name of the chosen item. detail.row is the row object from data, so row.Id is available as long as your query selected Id.
import { LightningElement, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { deleteRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
// COLUMNS is the array defined above.
export default class ContactTable extends NavigationMixin(LightningElement) {
columns = COLUMNS;
data = [];
error;
wiredResult;
@wire(getContacts)
wiredContacts(result) {
this.wiredResult = result;
const { data, error } = result;
if (data) {
this.data = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.data = [];
}
}
async handleRowAction(event) {
const actionName = event.detail.action.name;
const row = event.detail.row;
switch (actionName) {
case 'view':
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: row.Id,
objectApiName: 'Contact',
actionName: 'view'
}
});
break;
case 'delete':
try {
await deleteRecord(row.Id);
await refreshApex(this.wiredResult);
} catch (e) {
this.error = e;
}
break;
default:
}
}
}The delete branch calls deleteRecord, then refreshApex on the stored wired result. Without the refresh, the deleted row stays in the table.
Choosing actions per row
rowActions can also be a function. The table calls it for each row with (row, doneCallback). Pass the list of actions for that row to doneCallback. For example, you can disable Delete on rows that should be protected. Assign the function in JavaScript, not in a static array, and build the column array in the constructor or a getter so it can reference this.
Common mistakes
- Forgetting
Idin the SOQL query, sorow.Idisundefined. - Omitting the
nameproperty on an action, so theswitchnever matches. - Leaving out
refreshApexafter a delete, so the table looks unchanged.
Exercise
Your handler reads event.detail.row.Id, but it logs undefined. The action column is defined correctly. What is the most likely cause?
Show answer
The Apex query does not select the Id field, so the row objects have no Id property. Add Id to the SOQL SELECT list.
Exercise
Which two properties of event.detail does the rowaction event provide, and what does each contain?
Show answer
action holds the chosen menu item, so action.name is its name value. row holds the row object from the table's data.