Sub topic 5
Cell alignment and styling with cellAttributes
This chapter assumes you have a working lightning-datatable with a columns array, a key-field, and data loaded from a wired Apex method, as in the earlier chapters. You should also know basic JavaScript array methods and the Salesforce Lightning Design System (SLDS) utility classes.
What cellAttributes does
Each column definition can include a cellAttributes object. It changes how the cells in that column are drawn, without changing the column type or the data. Two properties cover most needs:
| Property | Effect | Example value |
|---|---|---|
| `alignment` | Aligns cell content horizontally | `'left'`, `'center'`, `'right'` |
| `class` | Adds CSS classes to the cell | `'slds-text-color_weak'` |
Other properties exist for specific types. For example, iconName adds an icon beside the value. This chapter covers alignment and class.
Aligning a column
Add cellAttributes next to the other column properties:
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{
label: 'Phone',
fieldName: 'Phone',
type: 'phone',
cellAttributes: { alignment: 'center' }
},
{
label: 'Birthdate',
fieldName: 'Birthdate',
type: 'date-local',
cellAttributes: { alignment: 'right' }
}
];Numeric and currency columns already default to right alignment, and text defaults to left. Set alignment only when you want something different.
Applying a fixed class
A string in class applies to every cell in the column:
{
label: 'Email',
fieldName: 'Email',
type: 'email',
cellAttributes: { class: 'slds-text-color_weak' }
}Use SLDS utility classes where you can. They follow the org's theme and work without extra CSS.
Styling cells based on row data
A fixed class is the same for every row. To style rows differently, point class at a field in the row object:
cellAttributes: { class: { fieldName: 'birthdateClass' } }The table reads birthdateClass from each row and uses its value as the class string. The field has no column of its own. It exists only to carry the class name. Apex does not return it, so you add it in JavaScript.
Wired data is read-only. Build new row objects with the spread operator instead of changing the originals:
import { LightningElement, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{
label: 'Birthdate',
fieldName: 'Birthdate',
type: 'date-local',
cellAttributes: {
alignment: 'right',
class: { fieldName: 'birthdateClass' }
}
}
];
function isBirthdayMonth(birthdate) {
if (!birthdate) {
return false;
}
const month = Number(birthdate.split('-')[1]) - 1;
return month === new Date().getMonth();
}
export default class ContactTable extends LightningElement {
columns = COLUMNS;
data = [];
error;
@wire(getContacts)
wiredContacts({ data, error }) {
if (data) {
this.data = data.map((contact) => ({
...contact,
birthdateClass: isBirthdayMonth(contact.Birthdate)
? 'slds-text-color_success slds-text-title_bold'
: ''
}));
this.error = undefined;
} else if (error) {
this.error = error;
this.data = [];
}
}
}The template needs no changes. It is the same lightning-datatable with key-field="Id", data={data}, and columns={columns}.
Contacts whose birthday falls in the current month now appear in green bold text, right-aligned. The code splits the date string instead of calling new Date(birthdate). A date-only string such as 2000-05-01 is parsed as UTC, which can shift the month in some time zones.
Common mistakes
- Putting
alignmentorclassdirectly on the column instead of insidecellAttributes. The table ignores it. - Setting the
classfield value toundefined. Use an empty string for rows with no styling. - Recomputing the class field only once. If row values change, for example after an inline edit and a refresh, rebuild the mapped array so the classes stay current.
- Expecting
cellAttributesto change the header. It styles body cells only.
Exercise
A column shows a numeric Amount. You want negative amounts in red, and the Apex query cannot return a style value. Which column setting do you use, and where does the class name come from?
Show answer
Set cellAttributes: { class: { fieldName: 'amountClass' } } on the column. In the JavaScript that receives the data, map each row to a new object with amountClass set to 'slds-text-color_error' when Amount < 0 and to an empty string otherwise. The table reads that field for each row.