0

I have an LWC that fetch account records. I want to apply some CSS to the table headers.

.html

<template>
    <lightning-card title="Datatable styling in lwc">

        <!-- Only load this if data is available -->
        <template if:true={tableData}>

            <lightning-datatable 
                key-field="Id" 
                data={tableData} 
                columns={columns}
                hide-checkbox-column
            >
            </lightning-datatable>

        </template>
    </lightning-card>
</template>

.js

import { LightningElement, wire } from 'lwc';

const COLUMNS = [
    {label:'Account Name', fieldName: 'Name', cellAttributes:{ class: 'test-css'}},

    {label:'Annual Revenue', fieldName: 'AnnualRevenue', type: 'currency', 
    cellAttributes:{ class: 'test-css'}
    },

.css

.test-css {
    color: blue;
    background-color: black;
}

the rest of the code works fine. It is just the css that is not working.

2
  • from what I remember it's className instead of class
    – ytiq
    Commented Apr 6, 2021 at 14:53
  • It doesn't work :( Commented Apr 6, 2021 at 14:55

1 Answer 1

1

You cannot apply custom classes to lightning-datatable:

Custom classes are currently not supported. To apply custom styling on your datatable cells, create a custom data type and then apply your custom CSS classes. See Using Different Layouts in Custom Cells.

You may only:

Pass Lightning Design System classes to cellAttributes when using standard data types.

2
  • can you please provide an example if how to do this. thanks Commented Apr 6, 2021 at 15:11
  • @AjmalRazeel Examples are in the documentation.
    – sfdcfox
    Commented Apr 6, 2021 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .