Sub topic 10
Displaying data with lightning-datatable
This chapter assumes you can build a basic Lightning web component (template, JavaScript class, CSS file) and that you know how to pass data to a base component through a bound attribute such as data={contacts}. You should also be comfortable with component events from the previous chapters.
What lightning-datatable does
lightning-datatable is a base component that renders an HTML table from two inputs: an array of row objects (data) and an array of column definitions (columns). It handles formatting, sorting indicators, row selection, and inline editing. It also gives you accessible table markup without extra work.
The component needs three attributes to render anything useful:
key-fieldnames the property that uniquely identifies each row.datais the array of row objects.columnsis the array of column definitions.
A minimal table
The template binds the table to two properties of the controller.
<template>
<lightning-datatable
key-field="id"
data={contacts}
columns={columns}
hide-checkbox-column
></lightning-datatable>
</template>The controller supplies the columns and the rows. Each column maps a fieldName to a property of the row objects, and its type controls how the value is formatted.
import { LightningElement } from 'lwc';
export default class ContactTable extends LightningElement {
columns = [
{ label: 'Name', fieldName: 'name', type: 'text' },
{ label: 'Phone', fieldName: 'phone', type: 'phone' },
{ label: 'Email', fieldName: 'email', type: 'email' }
];
contacts = [
{ id: '1', name: 'Ada Lovelace', phone: '5551230001', email: '[email protected]' },
{ id:Grace Hopper', phone: '5551230002', email: '[email protected]' }
];
}The phone type ren as a phone link, and email renders a mailto link. Other common types include date, currency, number, boolean, url, and button.
Common errors
- Missing or duplicate
key-fieldvalues. If rows share a key, or the property does not exist, selection and updates behave unpredictably. Pick a property that is always present and unique, such as a record Id. fieldNamethat does not match a row property. The cell renders empty and no error appears. Check the spelling and case.- Mutating
datain place. The component re-renders when you assign a new array. Replace the array (this.contacts = [...this.contacts, newRow]) instead of callingpushon it. - Forgetting a height. A table with many rows grows with its content. Wrap it in an element with a fixed height and add
enable-infinite-loadingonly if you handleonloadmore.
Handling events
The datatable reports user actions through events. You attach handlers in the template and read the details from event.detail.
<template>
<lightning-datatable
key-field="id"
data={contacts}
columns={columns}
onrowselection={handleRowSelection}
></lightning-datatable>
</template>import { LightningElement } from 'lwc';
export default class SelectableContactTable extends LightningElement {
columns = [{ label: 'Name', fieldName: 'name', type: 'text' }];
contacts = [
{ id: '1', name: 'Ada Lovelace' },
{ id: '2', name: 'Grace Hopper' }
];
selectedNames = [];
handleRowSelection(event) {
this.selectedNames = event.detail.selectedRows.map((row) => row.name);
}
}event.detail.selectedRows is an array of the full row objects, not just their keys. Other useful events are onsort (the detail holds fieldName and sortDirection), onsave (inline edits, with draftValues), and onrowaction (row-level buttons).
Exercise
A datatable shows the right number of rows, but the Phone column is empty. The column is defined as { label: 'Phone', fieldName: 'Phone', type: 'phone' } and the row objects use phone. What is wrong and how do you fix it?
Show answer
The fieldName is case sensitive and must match the row property exactly. The column asks for Phone but the rows have phone. Change the column to fieldName: 'phone', or rename the row property.
Exercise
In handleRowSelection, what does event.detail.selectedRows contain, and how would you get only the row Ids?
Show answer
It contains the full row objects for the selected rows. Use event.detail.selectedRows.map((row) => row.id) to get the Ids.