0

I am trying to apply dynamic icons to LWC data table using "cellAttributes". In my @wire method, I am running "ForEach" on fetched records, checking condition and assigning icon accordingly. So far it does not work and datatable does not display any icon. Please advice what I am doing wrong? Ideally into Status field, I should see Icons dynamically based on "Cathegory__c" field value. Is this possible to achieve somehow?

Here is my code:

const COLUMNS = [  
{ label: 'status', cellAttributes:
{ iconName: { fieldName: 'priorityicon' }}},
{ label: 'Scenario', fieldName: 'Cathegory__c', },

];

export default class NbeFullListOnObject extends LightningElement {

@wire(FetchRecos)
wiredRecos({error,data}){
    if(data){
        this.record = data;

        //loop through the list of contacts and assign an icon based on the rating
        this.record.forEach(item => {

            let iconObj = {...item};
            if(item.Cathegory__c === 'value1'){
                iconObj.priorityicon = "action:priority";
            }else if(item.Cathegory__c === 'value2'){
                iconObj.priorityicon = "action:bug";
            }else{
                iconObj.priorityicon = "action:call";
            }
        });
    }
} 

1 Answer 1

0

Using spread syntax you cloned those record and modified just the clone, so data held by this.record never changed.
You should use map() which returns a new array:

if(data){
    this.record = data.map((item) => {
        const iconObj = {...item};
        if(item.Cathegory__c === 'value1'){
            iconObj.priorityicon = "action:priority";
        }else if(item.Cathegory__c === 'value2'){
            iconObj.priorityicon = "action:bug";
        }else{
            iconObj.priorityicon = "action:call";
        }
        return iconObj;
    });
}

You must log in to answer this question.

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