Sub topic 3
Displaying Data with Lightning Base Components
Assume you know basic HTML, CSS, JavaScript, and the structure of an LWC. This chapter focuses on lightning-datatable and the decisions around it.
Displaying Data with `lightning-datatable`
lightning-datatable displays rows from a JavaScript array. Each column connects a visible label and format to a property in a row. The key-field value must identify each row uniquely.
<template>
<lightning-datatable
key-field="id"
data={contacts}
columns={columns}
hide-checkbox-column
onrowaction={handleRowAction}
></lightning-datatable>
</template>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: ' Lovelace', phone: '5551230001', email: '[email protected]' },
{ id: '2', name: 'Grace Hopper', phone: '5551230002', email: '[email protected]' }
];
handleRowAction(event) {
const row = event.detail.row;
console(row.id);
}
}The component formats phone and email values, the event handler receives the selected row. For server data, ensure every returned object has a stable unique key.
Customizing Appearance with Styling Hooks
Use SLDS classes for surrounding layout and styling hooks for supported base-component variations.
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}Avoid selectors that target a base component's internal markup. Those details belong to its implementation and can change.
Shadow DOM and Component CSS
LWC style sheets are scoped to the component. Shadow DOM also prevents ordinary CSS from crossing into a child component's internal markup. Styling hooks provide an intentional boundary for supported customization.
> callout note > A styling hook is safer than overriding internal classes, but verify the hook in the current base-component documentation.
Accessibility
Give every input a meaningful label. Provide alternative text for informative icons. Do not use color alone to communicate table status. Keep keyboard interaction available, and use the datatable's built-in semantics instead of recreating a table with generic elements.
Choosing a Component
| Need | Prefer |
|---|---|
| Standard table, sorting, or selection | `lightning-datatable` |
| Standard control with a small visual adjustment | Base component plus styling hook |
| Unusual layout or interaction | Custom component |
Testing with Jest
Jest tests should verify your component's data, rendered attributes, and event handling. Base components are usually replaced with Jest stubs. Test that your component passes the expected data and columns; do not test Salesforce's internal rendering.
Exercise
Why should a datatable use a stable unique value for key-field?
Show answer
The value lets LWC identify each row consistently when data changes, which supports correct rendering and row events.