Purpose:
The lightning-datatable component formats data based on the type you specify for the column.
Before you create your own data type, check the standard data types to see if one meets your requirements. You can use type attributes to customize the output for many types. The standard data types are:
- action
- boolean
- button
- button-icon
- currency
- date
- date-local
- location
- number
- percent
- phone
- text (default)
- url
Define Your Custom Type by Extending LightningDatatable:
myCustomTypeDatatable
├──customName.html
├──customNumber.html
├──myCustomTypeDatatable.js
└──myCustomTypeDatatable.js-meta.xml
//myCustomTypeDatatable.js
import LightningDatatable from 'lightning/datatable';
import customNameTemplate from './customName.html';
import customNumberTemplate from './customNumber.html';
export default class MyCustomTypeDatatable extends LightningDatatable {
static customTypes = {
customName: {
template: customNameTemplate,
standardCellLayout: true,
typeAttributes: ['accountName'],
},
customNumber: {
template: customNumberTemplate,
standardCellLayout: false,
typeAttributes: ['status'],
}
// Other types here
}
}
CUSTOM TYPE PROPERTY | TYPE | DESCRIPTION |
---|---|---|
| string | The name of your type's imported HTML template. |
| array | The comma-separated list of attributes to pass to the custom data
template. Access your data using the |
| boolean | Specifies whether the standard layout is used. The default is false. The standard layout is used by all standard data types. The default layout for custom data types is the bare layout. See Customize Data Type Layout and Styles. You can use the standardCellLayout to style cells for your custom data type to make them look similar to the standard data types. The standardCellLayout also supports accessibility and keyboard navigation. |
Create a Custom Data Template:
<!--customName.html-->
<template>
<lightning-badge
label={typeAttributes.accountName}
icon-name="standard:account">
</lightning-badge>
</template>
The customNumber.html template uses the default bare layout because myCustomTypeDatatable.js sets standardCellLayout: false for customNumber. You can add your own styling. This example adds a small padding around the cell and floats the number to the right. An icon is displayed if the number matches criteria specified in the data definition.
<!--customNumber.html-->
<template>
<div class="slds-p-around_x-small">
<lightning-formatted-number value={value} class="slds-float_right"></lightning-formatted-number>
<lightning-icon icon-name={typeAttributes.status} alternative-text="Employer Status"></lightning-icon>
</div>
</template>
Implement Your Data Table with the Custom Types:
Let’s implement a data table that uses the custom types. The first column displays the account name using the custom type we created in the previous section. The fieldName property matches the Name field on the account object.
Display 10 account records in the data table using an Apex controller.
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getAccountList() {
return [SELECT Id, Name, Industry, NumberOfEmployees FROM Account WITH SECURITY_ENFORCED LIMIT 10];
}
}
To implement the custom type datatable, create a wrapper component to contain your extended datatable component, define the datatable’s columns, and fetch data. Here we use myDatatable as the wrapper.
When defining the columns, you can pass in SLDS utility classes or your own classes using the cellAttributes property.
/* myDatatable.js */
import { LightningElement, wire, track } from 'lwc';
import getAccountList from '@salesforce/apex/AccountController.getAccountList';
const COLS = [
{ label: 'Account Name', type: 'customName',
typeAttributes: {
accountName: { fieldName: 'Name' }
},
},
{ label: 'Industry', fieldName: 'Industry',
cellAttributes: {
class: {fieldName: 'industryColor'},
}
},
{ label: 'Employees', type: 'customNumber', fieldName: 'NumberOfEmployees',
typeAttributes: {
status: {fieldName: 'status'}
},
cellAttributes: {
class: 'slds-theme_alert-texture'
}
}];
export default class MyDatatable extends LightningElement {
columns = COLS;
@track accounts = [];
@wire(getAccountList)
wiredAccounts({error, data}) {
if (error) {
// Handle error
} else if (data) {
// Process record data
this.accounts = data.map((record) => {
let industryColor = record.Industry === 'Energy' ? 'slds-text-color_success': '';
let status = record.NumberOfEmployees > 10000 ? 'utility:ribbon' : '';
return {...record,
'industryColor': industryColor,
'status': status
}
});
}
}
}
myDatatable.js shows several ways to apply styling on a custom cell. The Industry column uses the slds-text-color_success class only if the field value matches Energy. The NumberOfEmployees column uses the slds-theme_alert-texture class on all rows of the column. Additionally, the customNumber type that’s used for the NumberOfEmployees column includes layout and padding classes implemented directly in the customNumber.html markup.
Next, pass the data returned by the getAccountList Apex controller to the data attribute in your custom type datatable component.
<!--myDatatable.html-->
<template>
<c-my-custom-type-datatable
key-field="Id"
data={accounts}
columns={columns}
show-row-number-column>
</c-my-custom-type-datatable>
</template>
Happy coding... 😊