Sub topic 4
Column types and typeAttributes
This chapter assumes you can already render a basic lightning-datatable with data, columns, and key-field. It also assumes you know what a column definition object looks like (label, fieldName, type). Here you will pick the right type for each field and configure it with typeAttributes.
How type and typeAttributes work together
Every column definition has an optional type. It tells the table how to render the raw value from fieldName. If you leave it out, the column uses text.
Some types need extra settings, such as which currency code to show or how many decimal places to keep. Those settings go in typeAttributes, an object on the same column definition. Each type accepts only its own attributes. Unknown attributes are ignored, so a typo fails quietly.
{
label: 'Amount',
fieldName: 'Amount',
type: 'currency',
typeAttributes: { currencyCode: 'EUR', minimumFractionDigits: 0 }
}The table formats the value for display only. The record data you pass in is not changed.
Built-in column types
| Type | Expects | Common typeAttributes |
|---|---|---|
| `text` | String | none |
| `number` | Number | `minimumFractionDigits`, `maximumFractionDigits`, `minimumIntegerDigits`, `minimumSignificantDigits`, `maximumSignificantDigits` |
| `currency` | Number | `currencyCode`, `currencyDisplayAs`, plus the number digit options |
| `percent` | Fraction (0.25 shows as 25%) | the number digit options |
| `date` | ISO 8601 date-time string, timestamp, or Date | `year`, `month`, `day`, `weekday`, `hour`, `minute`, `second`, `hour12`, `timeZone`, `timeZoneName`, `era` |
| `date-local` | Date string such as `2026-11-30` | `year`, `month`, `day` |
| `boolean` | `true` or `false` | none |
| `email` | Email address | none |
| `phone` | Phone number | none |
| `url` | URL string | `label`, `target`, `tooltip` |
| `location` | Latitude and longitude | `latitude`, `longitude` |
| `button` | Click target | `label`, `name`, `title`, `disabled`, `iconName`, `iconPosition`, `variant`, `class` |
| `button-icon` | Click target | `iconName`, `name`, `title`, `alternativeText`, `disabled`, `variant`, `iconClass` |
| `action` | Row menu | `rowActions` (covered in the row actions chapter) |
Two points about this table matter in practice. The email, phone, and boolean types take no typeAttributes. The email and phone types render clickable mailto: and tel: links, and boolean renders a checkmark.
Choosing a type
Start from the field's Salesforce data type and follow the diagram.
A worked example
This component shows opportunities with most of the types above. It uses hard-coded data so you can paste it into a scratch component and run it without Apex.
import { LightningElement } from 'lwc';
const COLUMNS = [
{
label: 'Opportunity',
fieldName: 'OpportunityUrl',
type: 'url',
typeAttributes: {
label: { fieldName: 'Name' },
target: '_blank',
tooltip: 'Open the opportunity'
}
},
{
label: 'Amount',
fieldName: 'Amount',
type: 'currency',
typeAttributes: {
currencyCode: { fieldName: 'CurrencyIsoCode' },
currencyDisplayAs: 'symbol',
minimumFractionDigits: 0,
maximumFractionDigits: 2
}
},
{
label: 'Probability',
fieldName: 'ProbabilityFraction',
type: 'percent',
typeAttributes: { maximumFractionDigits: 0 }
},
{
label: 'Close Date',
fieldName: 'CloseDate',
type: 'date-local',
typeAttributes: { year: 'numeric', month: 'short', day: '2-digit' }
},
{
label: 'Last Activity',
fieldName: 'LastActivityAt',
type: 'date',
typeAttributes: {
year: 'numeric',
month: 'short',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false
}
},
{ label: 'Won', fieldName: 'IsWon', type: 'boolean' }
];
export default class OpportunityTable extends LightningElement {
columns = COLUMNS;
data = [
{
Id: '006000000000001',
Name: 'Renewal Q4',
OpportunityUrl: '/006000000000001',
Amount: 12500.5,
CurrencyIsoCode: 'EUR',
ProbabilityFraction: 0.9,
CloseDate: '2026-12-15',
LastActivityAt: '2026-11-28T14:30:00.000Z',
IsWon: false
},
{
Id: '006000000000002',
Name: 'Pilot Expansion',
OpportunityUrl: '/006000000000002',
Amount: 4800,
CurrencyIsoCode: 'USD',
ProbabilityFraction: 0.25,
CloseDate: '2027-01-20',
LastActivityAt: '2026-11-20T09:05:00.000Z',
IsWon: true
}
];
}<template>
<lightning-card title="Opportunities" icon-name="standard:opportunity">
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
hide-checkbox-column>
</lightning-datatable>
</lightning-card>
</template>Reading typeAttributes from the row
A typeAttributes value can be a literal, such as currencyCode: 'EUR'. It can also be an object with a fieldName property, such as { fieldName: 'CurrencyIsoCode' }. The table then reads that property from each row when it renders the cell. This is how the Amount column above shows euros on one row and dollars on the next.
The url type uses the same mechanism. fieldName holds the link target and typeAttributes.label holds the visible text. Without a label, the cell shows the URL itself.
Date, date-local, and time zones
Salesforce stores a DateTime as UTC, and the date type converts it to the user's time zone. That is correct for fields such as LastModifiedDate.
A Date field such as CloseDate has no time part. Under date it can display a day earlier for users west of UTC, because the value is read as midnight UTC and then shifted. Use date-local for Date fields. It displays the calendar date as stored.
If you leave typeAttributes off a date column, the table uses the user's locale default. Set timeZone only when you want to override the user's own time zone, for example to show every user a fixed office time.
Number, currency, and percent details
- Digit options follow the usual
Intl.NumberFormatrules. If you set bothminimumFractionDigitsandmaximumFractionDigits, the minimum must not exceed the maximum. - Significant digit options and fraction digit options are alternatives. Do not combine them on the same column.
currencyDisplayAsacceptssymbol,code, orname. The default issymbol.- If you omit
currencyCode, the table uses the org's default currency, not the record's. For multi-currency orgs, bind it to a field. - The
percenttype multiplies by 100 for display. Salesforce Percent fields return 25 for 25%, so the column would show 2500%. Divide by 100 when you prepare the data, as theProbabilityFractionfield does in the example.
Buttons and icons in cells
The button and button-icon types draw a clickable control in each row. Clicking one fires the table's rowaction event. The name in typeAttributes is how your handler knows which control was clicked.
const COLUMNS = [
{ label: 'Name', fieldName: 'Name' },
{
type: 'button',
typeAttributes: {
label: 'Approve',
name: 'approve',
variant: 'brand',
disabled: { fieldName: 'IsApproved' }
}
},
{
type: 'button-icon',
typeAttributes: {
name: 'refresh',
iconName: 'utility:refresh',
alternativeText: 'Refresh row',
variant: 'border-filled'
}
}
];Here disabled is read from each row, so approved rows get a greyed-out button. The handler works as in the row actions chapter: it reads event.detail.action.name and event.detail.row.
Common errors
- A column renders as plain text. The
typestring is misspelled or has the wrong case. Type names are lowercase and hyphenated, for exampledate-local. - A column is blank. The
fieldNamedoes not match a property on the row. Property names are case-sensitive. - Formatting has no effect. The
typeAttributesoption is not valid for that type, or it sits at the top level of the column instead of insidetypeAttributes. - A date is off by one day. You used
datefor a Date field. Switch todate-local. - A related-object column is empty. You used
Account.Nameas thefieldName. Flatten the value first. - Sorting a formatted column sorts oddly. Sorting works on the raw value in
fieldName, not on the displayed text. Aurlcolumn sorts by its URL. To sort by the label, pointsortableat a different field in your handler logic.
The cellAttributes option controls alignment and CSS classes, and it is separate from typeAttributes. It is covered in the next chapter. When none of the built-in types fits, such as a column that needs a picklist or a rich badge, you need a custom type, which a later chapter covers.
Exercise
A Salesforce Percent field Discount__c returns 15 for a 15% discount. A column with type: 'percent' and fieldName: 'Discount__c' shows 1500%. What change fixes it, and where do you make it?
Show answer
The percent type expects a fraction, so 0.15 shows as 15%. Divide the value by 100 when you prepare the data, for example by mapping each record to { ...row, DiscountFraction: row.Discount__c / 100 }. Then set the column's fieldName to DiscountFraction. The column definition itself stays type: 'percent'.
Exercise
You show Contract_Start__c (a Date field) and LastModifiedDate (a DateTime field) in one table. Which column type fits each one, and why?
Show answer
Use date-local for Contract_Start__c because it has no time part and should display exactly as stored, with no time zone shift. Use date for LastModifiedDate because it is a UTC timestamp that should be converted to the viewing user's time zone.
Exercise
Write a column definition that shows the account name as a link, opens in a new tab, and uses AccountLink for the URL and AccountName for the visible text.
Show answer
{ label: 'Account', fieldName: 'AccountLink', type: 'url', typeAttributes: { label: { fieldName: 'AccountName' }, target: '_blank' } }