Sub topic 8
Selecting rows
This chapter assumes you have a working lightning-datatable with data, columns, and a key-field, as built in the earlier chapters. It also assumes you can write an LWC event handler.
How selection works
By default, the table adds a checkbox column. Users can select rows one by one or all at once with the header checkbox. Each change fires a rowselection event. The event's detail.selectedRows property holds the full row objects that are selected after the change. It is not a list of changes.
Reading the selection
Add onrowselection to the table and store the rows in the handler.
<template>
<lightning-card title="Contacts" icon-name="standard:contact">
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
onrowselection={handleRowSelection}>
</lightning-datatable>
<p class="slds-p-around_small">Selected: {selectedCount}</p>
</lightning-card>
</template>import { LightningElement, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'email' }
];
export default class ContactTable extends LightningElement {
columns = COLUMNS;
data = [];
selectedRows = [];
@wire(getContacts)
wiredContacts({ data }) {
if (data) {
this.data = data;
}
}
handleRowSelection(event) {
this.selectedRows = event.detail.selectedRows;
}
get selectedCount() {
return this.selectedRows.length;
}
}Because each entry is a complete row, you can read any column from it, for example this.selectedRows.map((row) => row.Email). You do not need to look the record up again by Id.
Controlling the selection mode
Three attributes change how selection behaves.
| Attribute | Effect |
|---|---|
| `hide-checkbox-column` | Removes the checkbox column, so users cannot select rows. |
| `max-row-selection="1"` | Shows radio buttons instead of checkboxes, so only one row can be selected. |
| `max-row-selection="5"` | Allows at most five selected rows. The header select-all checkbox is hidden. |
Use max-row-selection="1" when the user must choose a single record, such as a parent record to attach something to.
Setting the selection from code
The selected-rows attribute takes an array of key-field values, not row objects. Use it to preselect rows or to clear the selection.
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
selected-rows={selectedIds}
onrowselection={handleRowSelection}>
</lightning-datatable>
<lightning-button label="Clear selection" onclick={handleClear}></lightning-button>selectedIds = [];
handleRowSelection(event) {
this.selectedRows = event.detail.selectedRows;
this.selectedIds = this.selectedRows.map((row) => row.Id);
}
handleClear() {
this.selectedRows = [];
this.selectedIds = [];
}Keeping selectedIds in sync with the event lets you clear or restore the selection later. Without it, the component has no way to tell the table what to deselect.
Common errors
- Nothing is selected after a refresh. If you replace
data, the old selection may no longer match. ResetselectedRowsandselectedIdswhen you reload the data. - The selection looks stale in a button handler. The handler reads
this.selectedRows, which only changes whenrowselectionfires. Update it in the event handler, not elsewhere. - Duplicate or missing keys. Selection tracks rows by
key-field. If two rows share a key, selecting one can select both. - Expecting a delta.
detail.selectedRowsis the complete current selection. Assign it. Do not push it onto an existing array.
Exercise
A user ticks three rows, and you want to untick them all from a "Clear" button. Which attribute do you bind, and what value do you give it?
Show answer
Bind selected-rows to a property and set that property to an empty array in the button handler. The attribute takes an array of key-field values, so [] means no row is selected. Keep the property in sync with event.detail.selectedRows in the rowselection handler so the table and component agree.