Sub topic 3
Defining columns
This chapter assumes you can build a basic Lightning Web Component, wire an Apex method to it, and work with JavaScript arrays and objects. It also assumes you know how to pass data and a key-field to lightning-datatable, as covered in the previous chapter.
What a column definition is
The columns attribute takes an array of plain objects. Each object describes one column. The table reads the array in order, so the first object becomes the leftmost column.
A column definition has three properties you will use almost every time:
labelis the text in the column header.fieldNameis the name of the property to read from each row object.typeis the data type that controls formatting. If you leave it out, the column usestext.
import { LightningElement, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text', sortable: true },
{ label: 'Email', fieldName: 'Email', type: 'email' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' },
{ label: 'Birthdate', fieldName: 'Birthdate', type: 'date-local' }
];
export default class ContactTable extends LightningElement {
columns = COLUMNS;
data = [];
error;
@wire(getContacts)
wiredContacts({ data, error }) {
if (data) {
this.data = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.data = [];
}
}
}The array is declared as a constant outside the class. It never changes, so it is built once and shared by every instance of the component. The class then assigns it to a field named columns, which the template binds with columns={columns}.
How fieldName connects columns to data
For each row, the table looks up row[fieldName] and shows the value. The match is an exact property lookup, so it is case-sensitive. A column with fieldName: 'email' shows empty cells when the row property is Email. The table does not raise an error. It simply finds nothing.
Other common column properties
You can add more properties to a definition as you need them. The following table lists the ones this tutorial uses later.
| Property | Purpose |
|---|---|
| `sortable` | Lets the user click the header to fire a sort event. |
| `editable` | Turns on inline editing for the column. |
| `initialWidth` | Sets a starting width in pixels. |
| `wrapText` | Wraps long text instead of truncating it. |
| `typeAttributes` | Passes settings to the column type. |
| `cellAttributes` | Sets alignment and CSS classes for cells. |
| `hideDefaultActions` | Hides the header dropdown menu. |
Common errors
- Related fields show blank.
fieldNamedoes not follow dot notation, soAccount.Namefinds nothing. Copy the value to a flat property before you assign the rows, then point the column at that property. - Wrong key casing. Match the field name exactly as Apex returns it, for example
Name, notname. - Columns that need no field. An
actioncolumn has nofieldName. It usestypeAttributesinstead, which a later chapter covers. - Unexpected formatting. A date stored as a string looks wrong under the
texttype. Choose the type that matches the data, which the next chapter covers.
Flattening a related field looks like this:
const rows = data.map((contact) => ({
...contact,
AccountName: contact.Account?.Name
}));
const COLUMNS = [
{ label: 'Name', fieldName: 'Name' },
{ label: 'Account', fieldName: 'AccountName' }
];If you use this approach, the Apex query must also select Account.Name.
Exercise
A Contact table shows an empty Account column. The column is defined as { label: 'Account', fieldName: 'Account.Name' }. Why is it empty, and how do you fix it?
Show answer
The datatable reads fieldName as a single property name and does not follow dots, so no property called "Account.Name" exists on the row. Copy the value to a flat property, for example AccountName: contact.Account?.Name, when you map the rows, and set fieldName to 'AccountName'.