Color Columns of Lightning Data Table – LWC

Color Columns of Lightning Data Table – LWC

Do you want to give your data table’s cells specific colors?
Do you want conditionally colorize your base data table component?
Are you bored of the same old plain data table?

The answer to all the above questions is this post. We will learn how you can colorize the cells of the lightning data table.

It's All Possible

Yes, it’s possible to assign a CSS class to each of the cells of the lightning data table. The secret here is to use “cellAttributes” property of the column.

The cellAttributes property provides additional customization to your cell, such as horizontal alignment, appending an icon to the output or assigning a CSS class to the cell. Check out the official documentation here.

Check Out Aura Version Of This Component

How To Do It?

It’s pretty simple to assign the cellAttributes property to a column definition. Check out a simple example below

{label: ‘Diet Type’, fieldName: ‘diet’, type: ‘text’, cellAttributes: { class: { fieldName: ‘dietCSSClass’ }}},

Things to observe from the above column definition:
– We are creating a column with a label/name as “Diet Type”. This will be visible as the header of the column of the data table.
– The data value each cell of this column will be in a field with “diet”.
– Each cell of this column can accept a CSS class name in field “dietCSSClass”.

A sample data for the above column look like below:
{diet : ‘Vegeterian’, dietCSSClass : ‘diet-veg’}

This will create a row in the data table with cell value as “Vegeterian”. Also you can have the definition of the class “diet-veg” in the CSS file of your component which will be applied to this cell.

A sample CSS class definition could be:
background : yellowgreen;

Now this will color your cell with a yellowgreen background. You can define other CSS properties as well.

Usage Example

Check out below sample component explaining everything we have learned so far.

HTML Markup Code:

JS Controller Code:


Below is the output of the above component, a nicely colored data table based on the user’s condition and CSS classes.

Check out more

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has 5 Comments

  1. Avatar

    Hello Manish,
    Thank you so much for sharing! I was able to follow through and it worked great. The only issue I am having is that when i try to apply my custom css class, it does not read the attributes. When I inspect in the browser, the class is attached to td tag, but the attributes did not apply. help?

    1. Avatar

      Yeah Lourdes, this seems to be an issue with LWC framework, I had faced the same. I am looking more into it and keep you posted.

  2. Avatar

    Hey Manish, any news on this? I was trying to add a CSS to make some urls not clickable while some are still clickable but it did the same as Lourdes, it added the tag, but the attributes did not apply.

    1. Avatar

      No Rafael, I am still checking on this. Somehow custom classes are not applying to data-table cells, and only slds classes are working.

Leave a Reply

Close Menu