Sub topic 2
Passing data and a key-field
This chapter covers the data and key-field attributes of lightning-datatable. It assumes you can build a basic Lightning Web Component (LWC), read JavaScript arrays and objects, and wire an Apex method to a component. It also assumes you know basic Salesforce object and field names.
What the table needs
lightning-datatable needs three attributes to show records: data, columns, and key-field. This chapter covers data and key-field. The next chapter covers columns.
datais an array of plain objects. Each object becomes one row.key-fieldis the name of a property that gives each row a unique value.columnssays which properties to show and how to format them.
The table reads each column's fieldName from every object in data. If an object has no such property, the cell is empty.
Getting the data
The Apex method below returns Contact records. It selects Id because the table uses it as the key. The cacheable=true setting lets the component wire the method.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts() {
return [
SELECT Id, Name, Email, Phone, Birthdate
FROM Contact
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}
}The component wires this method and stores the result in data.
import { LightningElement, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text', sortable: true },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' },
{ label: 'Birthdate', fieldName: 'Birthdate', type: 'date-local' }
];
export default class ContactTable extends 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 = [];
}
}
}Three details matter here:
datastarts as an empty array. The table then renders with no rows instead of failing before the wire responds.- The wired function keeps the whole
resultinwiredResult. A later chapter passes it torefreshApex. - On an error,
datais reset to an empty array so stale rows do not stay on screen.
The minimal template
<template>
<lightning-card title="Contacts" icon-name="standard:contact">
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
hide-checkbox-column>
</lightning-datatable>
</lightning-card>
</template>key-field="Id" is a string, not a binding. It names the property to read from each row. hide-checkbox-column removes the selection checkboxes, which this table does not need yet.
Why the key-field matters
The table uses the key-field value to tell rows apart. It relies on this identity when it:
- tracks which rows are selected,
- reports which row a row action came from,
- matches draft edits to the right row.
Each value must be unique within data. Id is the usual choice for records. If two rows share a key, selection and edits can apply to the wrong row. If the property is missing from the data, rows have no identity. For that reason, always include the key field in your SOQL SELECT list, even if no column displays it.
The diagram shows how the pieces connect.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Table shows no rows | `data` is `undefined` or the wire failed | Initialize `data = []` and check `error` |
| Cells are blank in one column | `fieldName` does not match the property name | Match the case exactly, such as `Email` |
| Selection or edits hit the wrong row | Duplicate or missing key values | Use a unique field and select it in SOQL |
| Error when changing the rows | Code mutates the wired array | Copy it first, for example `[...this.data]` |
Wired data is read-only. When you need to change it, as the sorting chapter does, work on a copy.
Exercise
A developer's Apex query is SELECT Name, Email FROM Contact, and the template uses key-field="Id". What is wrong, and how do they fix it?
Show answer
The rows have no Id property, so the table has no unique identity for them. Selection, row actions, and edits cannot work reliably. Add Id to the SELECT list: SELECT Id, Name, Email FROM Contact.
Exercise
Why does the component set data = [] in the error branch instead of leaving the old value?
Show answer
Leaving the old value would keep showing rows from an earlier successful response next to an error. Resetting it to an empty array keeps the table consistent with the error state.