Sub topic 2
Column types and typeAttributes
Assume you know basic Lightning Web Components, JavaScript arrays, Apex wiring, and Salesforce fields. lightning-datatable renders rows from data and formats each field according to a column definition.
Column types and typeAttributes
A column definition connects a label to a field. Its type controls formatting. typeAttributes supplies type-specific options.
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' },
{
label: 'Website',
fieldName: 'Website',
type: 'url',
typeAttributes: { label: { fieldName: 'Name' }, target: '_blank' }
},
{
label: 'Amount',
fieldName: 'Amount',
type: 'currency',
typeAttributes: { currencyCode: 'USD' }
},
{
label: 'Created',
fieldName: 'CreatedDate',
type: 'date-local',
typeAttributes: { year: 'numeric', month: 'short', day: '2-digit' }
}
];Common built-in types include text, number, currency, percent, date, date-local, email, phone, url, and action. The field values must match the expected format.
Cell alignment and styling
Use cellAttributes to control alignment or apply a class. A class can come from a field in each row.
{
label: 'Status',
fieldName: 'Status',
type: 'text',
cellAttributes: {
alignment: 'center',
class: { fieldName: 'statusClass' }
}
}The class must be available through the row data. Use this for simple presentation. A custom type is better when a cell needs interactive or complex markup.
Loading records from Apex
A cacheable Apex method can provide records to a wired component. Include Id, because the table needs a unique key for every row.
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
];
}
}import { LightningElement, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactTable extends LightningElement {
columns = COLUMNS;
data = [];
error;
sortedBy;
sortedDirection = 'asc';
selectedRows = [];
@wire(getContacts)
wiredContacts({ data, error }) {
if (data) {
this.data = data;
this.error = undefined;
} else if (error) {
this.data = [];
this.error = error;
}
}
}An empty result naturally renders no rows. Your template can conditionally show an empty-state message or an error message.
Sorting rows
Client-side sorting handles the rows already loaded. Sort a copy because wired data should not be mutated directly.
handleSort(event) {
const { fieldName, sortDirection } = event.detail;
const factor = sortDirection === 'asc' ? 1 : -1;
this.data = [...this.data].sort((a, b) => {
const x = a[fieldName] ?? '';
const y = b[fieldName] ?? '';
return x > y ? factor : x < y ? -factor : 0;
});
this.sortedBy = fieldName;
this.sortedDirection = sortDirection;
}Selecting rows
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
sorted-by={sortedBy}
sorted-direction={sortedDirection}
onsort={handleSort}
onrowselection={handleRowSelection}
hide-checkbox-column>
</lightning-datatable>The selection event contains the currently selected row objects.
handleRowSelection(event) {
this.selectedRows = event.detail.selectedRows;
}Exercise
Why must the datatable use key-field="Id" when displaying Contact records?
Show answer
Each row needs a stable, unique identity so the table can track rendering, selection, and row updates correctly.