In the Salesforce Lightning data table, we don’t have a defined data type to display picklist values in the LWC data table. To add picklist values to a Lightning data table, we need to define a custom data type to display the picklist field.
In this Salesforce tutorial, we will learn how to add a picklist in an LWC data table, and for this, we will also use the combo-box feature to display picklist values.
Picklist Field in Salesforce LWC Data Table
In Salesforce Lightning LWC data tables, the picklist values are displayed in a dropdown menu that allows users to select a single value from a predefined list of options. This feature of displaying a picklist is integrated into the LWC data tables to perform inline editing directly from the table rows.
Add a Picklist in Salesforce LWC Data Table
In the example below, we will create an LWC data table, fetching the account records along with the picklist field Industry.
- We will fetch the account records, so we need to create a controller class to fetch them. We also need to fetch the values of the picklist field, Industry.
For that, create an Apex class and enter the code below.
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
return [SELECT Id, Name, Industry FROM Account LIMIT 10];
}
@AuraEnabled(cacheable=true)
public static List<Map<String, String>> getIndustryOptions() {
List<Map<String, String>> options = new List<Map<String, String>>();
Schema.DescribeFieldResult fieldResult = Account.Industry.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for (Schema.PicklistEntry entry : ple) {
options.add(new Map<String, String>{'label' => entry.getLabel(), 'value' => entry.getValue()});
}
return options;
}
}
- After this, create an LWC component and enter the code below in the JS file. This will handle the logic of displaying the Account Name and Industry columns in the Data table, and also showing the values of the picklist in the dropdown.
This will be the main LWC component accountDataTable. To define a custom type for Industry, we have a type, customCombo, that is again an LWC that we will define in the next steps.
import { LightningElement, wire, track } from 'lwc';
import getAccounts from '@salesforce/apex/AccountController.getAccounts';
import getIndustryOptions from '@salesforce/apex/AccountController.getIndustryOptions';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
const columns = [
{ label: 'Account Name', fieldName: 'Name', type: 'text' },
{
label: 'Industry',
fieldName: 'Industry',
type: 'customCombo',
wrapText: true,
editable: true,
typeAttributes: {
options: { fieldName: 'industryOptions' },
value: { fieldName: 'Industry' }
}
}
];
export default class AccountDataTable extends LightningElement {
@track accounts = [];
@track draftValues = [];
columns = columns;
@wire(getAccounts)
wiredAccounts({ error, data }) {
if (data) {
this.processAccounts(data);
} else if (error) {
this.showToast('Error', 'Error loading accounts', 'error');
}
}
@wire(getIndustryOptions)
wiredIndustryOptions({ error, data }) {
if (data) {
this.industryOptions = data;
this.processAccounts(this.accounts);
} else if (error) {
this.showToast('Error', 'Error loading industry options', 'error');
}
}
processAccounts(data) {
if (this.industryOptions && data) {
this.accounts = data.map(account => ({
...account,
industryOptions: this.industryOptions
}));
}
}
handleRowAction(event) {
const { rowId, value } = event.detail;
this.draftValues = [{ Id: rowId, Industry: value }];
}
async handleSave(event) {
const updatedFields = event.detail.draftValues.slice()[0];
const recordInput = { fields: updatedFields };
try {
await updateRecord(recordInput);
this.draftValues = [];
this.showToast('Success', 'Account updated', 'success');
} catch (error) {
this.showToast('Error', error.body.message, 'error');
}
}
showToast(title, message, variant) {
this.dispatchEvent(new ShowToastEvent({ title, message, variant }));
}
}
- After this, enter the code below in the HTML file accountDataTable.html.
<template>
<lightning-card title="Accounts with Industry Combobox" icon-name="standard:account">
<div class="slds-m-around_medium">
<c-enhanced-data-table
key-field="Id"