Sub topic 4
Column types and typeAttributes
This chapter assumes you can define a columns array and pass data and key-field to lightning-datatable. Both are covered in the previous chapters.
How type controls display
Each column definition has a type. The type decides how the table renders the raw field value. If you leave it out, the column uses text. The type changes only the display. The value in your data array is not modified.
Common built-in types:
| Type | Renders | Expects |
|---|---|---|
| `text` | Plain text | String |
| `number` | Formatted number | Number |
| `currency` | Number with currency formatting | Number |
| `percent` | Number with a percent sign | Fraction, where 0.25 shows as 25% |
| `date` | Date and time | ISO string or timestamp |
| `date-local` | Date without time zone shift | Date string such as `1990-04-12` |
| `boolean` | Checked or unchecked icon | Boolean |
| `email`, `phone` | Clickable link | String |
| `url` | Link | URL string |
Other types, such as button, button-icon, and action, add controls to rows. Later chapters cover them.
Configuring a type with typeAttributes
typeAttributes is an object of settings for the chosen type. Which settings are valid depends on the type.
const COLUMNS = [
{
label: 'Opportunity',
fieldName: 'RecordUrl',
type: 'url',
typeAttributes: {
label: { fieldName: 'Name' },
target: '_blank'
}
},
{
label: 'Amount',
fieldName: 'Amount',
type: 'currency',
typeAttributes: {
currencyCode: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0
}
},
{
label: 'Probability',
fieldName: 'ProbabilityFraction',
type: 'percent'
},
{
label: 'Close Date',
fieldName: 'CloseDate',
type: 'date-local',
typeAttributes: {
day: '2-digit',
month: 'short',
year: 'numeric'
}
}
];Three points in this example are worth noting.
- Reading a value from another field. For the
urltype,fieldNameholds the link target. The visible text comes fromlabel, and{ fieldName: 'Name' }tells the table to read that text from the row'sNamefield. AnytypeAttributesproperty that accepts a field reference works this way. - Rounding. The currency attributes set the digits shown. A value of 12500.75 displays as
$12,501with these settings. - Date formatting.
date-localaccepts the sameday,month, andyearoptions asdate. Those options use theIntl.DateTimeFormatconventions.
The component needs to supply the two derived fields, RecordUrl and ProbabilityFraction. Build them from the wired result in a new array and leave the wired data untouched:
import { LightningElement, wire } from 'lwc';
import getOpportunities from '@salesforce/apex/OpportunityController.getOpportunities';
export default class OpportunityTable extends LightningElement {
columns = COLUMNS;
data = [];
@wire(getOpportunities)
wiredOpportunities({ data }) {
if (data) {
this.data = data.map((opp) => ({
...opp,
RecordUrl: `/${opp.Id}`,
ProbabilityFraction: opp.Probability / 100
}));
}
}
}Here COLUMNS is the array defined in the previous snippet. Because fieldName points at the derived properties, they must exist on every row.
Common mistakes
- Passing 25 to a
percentcolumn. The column shows 2500%. Divide a Salesforce percent field by 100 first. - Using
datefor a calendar date. A date-only value such as a birthdate can show as the previous day for users in some time zones. Usedate-localfor fields without a time component. - Misspelled or unsupported attributes. The table ignores them without an error. If formatting does not change, check the attribute name against the type.
- Using a field reference where a literal is expected. Only attributes that accept
{ fieldName: ... }can read from the row. Others need a fixed value.
Exercise
A Salesforce field stores a discount as 15 to mean 15%. The column uses type: 'percent' with fieldName: 'Discount__c', and the table shows 1500%. What change fixes it?
Show answer
The percent type expects a fraction, so 0.15 displays as 15%. Add a derived field to each row, for example DiscountFraction: row.Discount__c / 100, and point the column's fieldName at it.