How to Display Icon In Lightning Datatable

Hello everyone, today let’s create a component that displays an icon and text color for records based on some condition in aura using a lightning data table. If you have not followed our beginner lightning development series, please check here. Salesforce Lightning Tutorial For Beginners.

Conditionally Add Icon And Text Color For Records In Salesforce Lightning DataTable

Problem Statement: – Suppose the account record is Active then show that account row in green color and show the Utility check icon, suppose it is No, then show a row with red color and show the icon as utility wrong, and suppose if it is blank then show the warning icon and normal text color. Let’s see how we can develop this component using lighting datatable.

Note:- Here we have leveraged the existing picklist field available in the account object i.e Active__c. So now let’s go ahead and create our component.

No, let’s start with creating an apex class.


public class AccountHandler {
    public static List<Account> featchAccount() {
        return [SELECT Id, Name,Industry,Type,Active__c FROM Account];

Read the best salesforce Books: Click Here.

Here the apex class returns a list of accounts. Now let’s develop a component file.


<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="AccountHandler">
    <aura:handler name="init" value="{! this }" action="{! c.init }"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="data" type="Object"/>
    <lightning:card title="Account Details">
   //Data Table to display  as table
                             columns="{! v.columns }"
                             data="{! }"

In the above CMP field, we have created a columns attribute that holds a column’s value that will be declared in the Js file and one more attribute data that holds a collection type of object. So now let’s create the Js component.


    init : function(component, event, helper) {
        //Data table columns value.
        component.set('v.columns', [
            {label:'Important Account', fieldName: '',
                 class: { fieldName: 'Active__c' },
                 iconName: {
                     fieldName: 'displayIconName'
            {label: 'Account Name', fieldName: 'Name', type: 'text',
                 class: { fieldName: 'colortext' }}},
            {label: 'Industry', fieldName: 'Industry', type: 'Picklist',
                 class: { fieldName: 'colortext' }}},
            {label: 'Type', fieldName: 'Type', type: 'Picklist',
                 class: { fieldName: 'colortext' }}}            
        //Calling Server side apex method
        var action = component.get('c.featchAccount');
        action.setCallback(this, function(response) {
            //store state of response
            var state = response.getState();
            if (state === "SUCCESS") {
                //Storing response in records variable.
                var records = response.getReturnValue();
                //Iterating over a records.
                for ( var i = 0; i < records.length; i++ ) {  
                    //Checking record active field value.
                    //If found, setting icon to utility:check
                    //And setting color to sucess.
                    //Same if and else condition added for rest conditions.
                    if ( records[i].Active__c=="Yes" )  {
                    }	else if( records[i].Active__c=="No"){
                //Once Conditons staisfies add it to the attribute using component.set.
                component.set('', records);               

Read the comment to understand. Now we are done with Js, so let’s add some CSS to this component.


.THIS .Yes svg {
   fill: green;    

.THIS .No svg {
   fill: red;    

.THIS .null svg {
   fill: red;    

Now we are done with our component, let’s add this to the app page and see how it looks like.

Add the Component to the App Page


Congratulations, you have successfully created a lightning component. If you liked this post, please share it with your friends and colleagues. we will meet in, another awesome lightning component development. Also please read our below post on the lightning components.

Also Read

7 thoughts on “How to Display Icon In Lightning Datatable”

Leave a Comment