Sub topic 14
Extending the table with custom column types
The built-in types in lightning-datatable cover text, numbers, dates, URLs, and more. This chapter assumes you have used columns, typeAttributes, and cellAttributes. It shows what to do when none of the built-in types can render a cell.
When a custom type is needed
First try to solve the problem with built-in features. Use cellAttributes for alignment and CSS classes, and a typeAttributes option on an existing type for formatting. Build a custom type only when the cell needs its own markup or behavior. Examples are a colored status badge, an image, or a picklist.
A custom type is a small component rendered inside each cell. You register it by extending the table itself.
Step 1: Build the cell component
The cell component is an ordinary LWC. It receives the cell value through an @api property.
// statusBadge/statusBadge.js
import { LightningElement, api } from 'lwc';
export default class StatusBadge extends LightningElement {
@api value;
@api tone;
get badgeClass() {
return this.tone === 'success'
? 'slds-badge slds-theme_success'
: 'slds-badge';
}
}<!-- statusBadge/statusBadge.html -->
<template>
<span class={badgeClass}>{value}</span>
</template>Step 2: Extend the datatable
Create a component that extends LightningDatatable from lightning/datatable. Give it a static customTypes map and no HTML template of its own. It inherits the standard one.
// contactDatatable/contactDatatable.js
import LightningDatatable from 'lightning/datatable';
import statusBadgeTemplate from './statusBadgeType.html';
export default class ContactDatatable extends LightningDatatable {
static customTypes = {
statusBadge: {
template: statusBadgeTemplate,
standardCellLayout: true,
typeAttributes: ['tone']
}
};
}The template file sits in the same folder. It hands the value and type attributes to your cell component.
<!-- contactDatatable/statusBadgeType.html -->
<template>
<c-status-badge value={value} tone={typeAttributes.tone}></c-status-badge>
</template>Each key in the map has three parts:
templateis the imported HTML file that renders the cell.standardCellLayoutset totruekeeps the normal cell padding and alignment. Set it tofalseif the cell should fill the whole area.typeAttributeslists the attribute names the type accepts. Any attribute you leave out of this list is not passed to the template.
Step 3: Use it in a parent component
Use your extended table instead of lightning-datatable. Everything else stays the same, including key-field, data, sorting, and selection.
<template>
<c-contact-datatable
key-field="Id"
data={data}
columns={columns}
hide-checkbox-column>
</c-contact-datatable>
</template>const COLUMNS = [
{ label: 'Name', fieldName: 'Name', type: 'text' },
{
label: 'Lead Source',
fieldName: 'LeadSource',
type: 'statusBadge',
typeAttributes: { tone: 'success' }
}
];For this column to show data, add LeadSource to the SELECT list in getContacts. To set the tone per row, use tone: { fieldName: 'tone' } and add a tone property to each row object.
Common errors
- The cell is empty. The type name in
columnsmust exactly match a key incustomTypes. The match is case-sensitive. - A type attribute is undefined in the template. The attribute is missing from the
typeAttributesarray incustomTypes. - The component will not compile. The extended component must not have its own
.htmlfile with the same name. Keep only the JavaScript file and the cell templates.
Exercise
A column uses type 'statusBadge' with typeAttributes: { tone: 'success' }, but the badge always renders in the default style. The template passes tone={typeAttributes.tone}. What is the most likely cause?
Show answer
The customTypes entry for statusBadge does not list 'tone' in its typeAttributes array, so the attribute is never passed to the template. Add 'tone' to that array.