Let's consider the below scenario:
I have a scenario to bind integer value which is calculated from apex function. I have to display integer value in lightning datatable column.
Please find the code below:
@AuraEnabled(cacheable=true)
public static Double getTestValue(String recordId){
if(recordId == null){
return null;
}
Decimal productAnnualVolume;
Integer accountCount;
Double quantityValue;
OpportunityLineItem oppLineItemRecord = [SELECT Id, Name, Quantity FROM OpportunityLineItem WHERE Id = :recordId];
productAnnualVolume = oppLineItemRecord.Quantity;
List<customObject__c> customObjectList = fetchCustomObjectProducts(recordId);
accountCount = customObjectList.size();
quantityValue = productAnnualVolume/accountCount;
if(quantityValue!=null){
return quantityValue;
}else{
return null;
}
}
JS Controller:
quantityValue;
accountsData = [];
@wire(getTestValue, {recordId: '$recordId'})
getTestValue({error, data}) {
if(data) {
this.quantityValue= data;
this.error = undefined;
console.log('quantityValue--------->' + this.quantityValue);
}
else if(error){
this.error = error;
this.quantityValue= undefined;
}
}
testColumns = [
{
label: 'Account Name',
fieldName: 'AccountName',
type: 'url',
typeAttributes: {label: { fieldName: 'Name' }, target: '_blank'}
},
{ label: 'Quantity', type: 'text', editable: true}
];
@wire(getAccounts, {recordId: '$recordId'})
accountList({error, data}) {
if(data) {
let tempAccList = [];
data.forEach((record) => {
let tempAccRec = Object.assign({}, record);
tempAccRec.AccountName = '/' + tempAccRec.Id;
tempAccList.push(tempAccRec);
});
this.accountsData = tempAccList;
this.error = undefined;
}
else if(error){
this.error = error;
this.accountsData = undefined;
}
}
HTML:
<template>
<div>
<lightning-card title="">
<div class="slds-p-around_xx-small">
<lightning-datatable
key-field="Id"
data={accountsData}
onrowselection={handleRowSelection}
onsave={handleSave}
draft-values={saveDraftValues}
columns={testColumns}>
</lightning-datatable>
</div>
</lightning-card>
</div>
</template>
Question: I would need to show 'this.quantityValue' value in the lightning datatable column - 'Quantity'. How could I add 'this.quantityValue' into the 'accountsData' array in JS so that I could be able to display quantity in the lightning datatable. Any kind of help is appreciated. Thank you