Sub topic 1
What lightning-datatable does
lightning-datatable is a base component that renders records as a table. You give it an array of row objects and an array of column definitions, and it draws the headers, rows, and cells. It also handles sorting indicators, row selection, row actions, inline editing, and column resizing, and it raises events when the user interacts with them.
Prerequisites
This chapter assumes you already know the following:
- Basic Lightning Web Components: templates, decorators, and event handlers.
- JavaScript arrays, objects, and destructuring.
- How to wire an Apex method or a Lightning Data Service adapter to a component.
- Salesforce object and field basics.
When to use it
Use lightning-datatable when users need to scan, sort, select, or edit many records of the same shape. It suits related lists, search results, and admin tools.
Consider something else in these cases:
- You need a layout that is not a grid, such as cards or a timeline. Build it with your own template and
for:each. - You need only a few records with rich content in each row. A custom list can be simpler than the table.
- You need a column type the table does not offer. A custom column type is possible, but it adds work (a later chapter covers it).
The three core attributes
The table needs three attributes to display anything useful:
| Attribute | What it holds |
|---|---|
| `data` | An array of row objects, such as Contact records. |
| `columns` | An array of column definitions that say which field each column shows, how it is labeled, and how it is formatted. |
| `key-field` | The name of a field that is unique in each row, usually `Id`. |
The next two chapters cover key-field and column definitions in detail. For now, notice how they fit together: each column definition has a fieldName, and the table reads that property from every object in data.
A minimal example
The component class defines the columns and supplies the data. Here the data is hard-coded so you can see the table without Apex. Later chapters replace it with records from Apex.
import { LightningElement } from 'lwc';
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' }
];
export default class ContactTable extends LightningElement {
columns = COLUMNS;
data = [
{ Id: '003000000000001', Name: 'Ada Lovelace', Email: '[email protected]', Phone: '5551230001' },
{ Id: '003000000000002', Name: 'Grace Hopper', Email: '[email protected]', Phone: '5551230002' }
];
}The template binds those properties to the table. hide-checkbox-column removes the selection checkboxes, which this example does not need.
<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>How the pieces connect
The component class owns the data and the column definitions. The table renders them and sends events back for the class to handle.
The table does not change your data. When a user sorts or edits, the table only reports what happened. Your handlers decide what to do, such as sorting a copy of the array or saving a record. This is why the table works with read-only wired data once you copy it before sorting.
Common errors
- An empty table with no error.
dataisundefinedor empty, or nofieldNamematches a property on the row objects. Check spelling and case, since field names are case-sensitive in the lookup. - A warning about missing keys, or rows that behave oddly.
key-fieldis missing or names a field that is not unique. IncludeIdin your query and use it as the key. - Blank cells for a related field. The table reads flat properties. A value such as
Account.Nameis nested, so it does not appear unless you flatten the data first.
Exercise
A developer sets data and columns on lightning-datatable, but the table shows only column headers. The Apex query returned records. Name two likely causes.
Show answer
The fieldName values in the column definitions do not match the property names on the row objects, or the data property was never assigned because the wire result was not copied into it (for example, the handler did not set this.data). A missing key-field is a third possibility to check, since every row needs a unique identifier.