Display Icons in a Lightning Datatable in LWC

In Salesforce LWC data tables, it might be required to display icons for the field values in the respective rows or columns. The icons can be added for requirements, such as displaying different icons for different opportunity stages or showing active and inactive statuses.

In this Salesforce tutorial, we will learn how to display icons in a Lightning data table in LWC. In the examples, we will add the dynamic and static icons based on specific criteria in the data table.

Add Icons to the Salesforce Lightning Data Table in LWC

In Salesforce LWC components, we have a built-in icon display feature that can be accessed through the “lightning-icon” component, which allows us to use the standard Salesforce Lightning Design System (SLDS) icons in the LWC data table by specifying the icon-name attribute.

We can add icons to the LWC Lightning data table with static and dynamic icons. In the examples below, we will see both.

  • Display Static Icons in a Lightning Datatable in LWC
  • Display Dynamic Icons in a Lightning Datatable in LWC

Display Static Icons in a Lightning Datatable in LWC

To display static Salesforce icons, I will create an LWC table that fetches the opportunity records dynamically and displays the standard Opportunity icon in the table column.

  1. Enter the code below in the Apex controller class to fetch the opportunity records in the data table.
public with sharing class OpportunityController {
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> getOpportunities() {
        return [SELECT Id, Name, StageName, Amount 
                FROM Opportunity 
                LIMIT 20];
    }
}
  1. After this, create an LWC component and enter the code below in the HTML template. This will render the data in the LWC data table.
<template>
    <lightning-card title="Opportunities with Static Icons">   
        <lightning-datatable
            key-field="Id"
            data={opportunities}
            columns={columns}
            hide-checkbox-column
        ></lightning-datatable>
    </lightning-card>
</template>
  1. After this, enter the code below in the JS file, defining the logic of inserting the icon in the table column.
import { LightningElement, wire } from 'lwc';
import getOpportunities from '@salesforce/apex/OpportunityController.getOpportunities';

const columns = [
    {
        label: 'Opportunity Icon',
        type: 'icon',
        fieldName: '',
        cellAttributes: { iconName: 'standard:opportunity' }
    },
    { label: 'Opportunity Name', fieldName: 'Name' },
    { label: 'Stage', fieldName: 'StageName' },
    { label: 'Amount', fieldName: 'Amount', type: 'currency' }
    
];

export default class StaticIconDatatable extends LightningElement {
    opportunities = [];
    columns = columns;

    @wire(getOpportunities)
    wiredOpportunities({ error, data }) {
        if (data) {
            this.opportunities = data; 
        } else if (error) {
            console.error('Error fetching opportunities:', error);
        }
    }
}

To add the standard opportunity icon, we have added a column in the Columns array with the label “Opportunity Icon“, and the type should be ‘icon.’ To display only the icon in the column, we have left the field blank as (fieldName: ‘ ‘). If you want to display the icon with the field value, then mention it in the fieldName parameter.

To get the standard opportunity icon, we used cellAttributes: { iconName: ‘standard:opportunity’ }. If you want to use any custom icon of a custom object, then it can be referred to as “custom:<icon_label>.”

  1. Enter the code below in the meta.xml file to make the component available to the Lightning pages.
<isExposed>true</isExposed>
    <targets>
        <target>lightning__Ap