I am trying to specify a background color and a font color depending on a numeric value of a field. I have that working no problem. This is how it looks.
HTML:
<lightning-layout-item size="4" class={home_0_15}>
{predictionRecord.Percentage__c}%
</lightning-layout-item>
JS:
@track predictionRecord;
@wire(getStatistics, { recordId: '$recordId' }) wiredStatistics ({error, data}){
if (error) {
this.error = error;
} else if (data) {
this.predictionRecord = data;
this.error = undefined;
}
}
get home_0_15() {
if (this.predictionRecord.Percentage__c >= 0 && this.predictionRecord.Percentage__c < 40) {
return 'low-percentage goals-minute-contents slds-text-align_center';
} else if (this.predictionRecord.Percentage__c >= 40 && this.predictionRecord.Percentage__c < 50) {
return 'medium-low-percentage goals-minute-contents slds-text-align_center';
} else if (this.predictionRecord.Percentage__c >= 50 && this.predictionRecord.Percentage__c < 70) {
return 'medium-percentage goals-minute-contents slds-text-align_center';
} else if (this.predictionRecord.Percentage__c >= 71) {
return 'high-percentage goals-minute-contents slds-text-align_center';
}
}
CSS:
.low-percentage {background-color: #ffe9e6 !important; color:#cc2d13;}
.medium-low-percentage {background-color: #f6f7cd !important; color:#696b15;}
.medium-percentage {background-color: #72da233d !important; color:#147221;}
.high-percentage {background-color: #72da2373 !important; color:#147221;}
This works. The problem is that there are around 50 fields which I need to do the same for. This would mean 50 getters. This is not very efficient. I already went through the internet and you can't create dynamic getters as far as I understood. Is there another way to make some kind of dynamic CSS approach to solve this issue?