0

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

1 Answer 1

0

You cannot just link any data you like into a datatable. You need to put the data inside the data attribute. That might look like something like this:

this.accountsData = [...this.accountData, { quantity: this.quantityValue }]
5
  • Thank you for the answer. I tried but getting error as [this.accountData is not iterable] Commented Aug 17, 2022 at 13:21
  • @Rose It was kind of a guess, you didn't share all your code, so I had to go off what I had. The point is, you will need to add the data to the property bound to the data attribute if you want to show the data inside the table. Otherwise, consider adding it outside somewhere. Commented Aug 17, 2022 at 13:22
  • Thank you. I need to show both accounts details and quantity value in the table. Accounts details are getting displayed properly. Do i need to pass a wrapper from apex to achieve this. Commented Aug 17, 2022 at 13:46
  • @Rose no, you could just use an aggregate result query. Commented Aug 17, 2022 at 14:17
  • Is there any other way to add quantityValue in accountsData in JS. I think we won't be able to use aggregate query here because quantity is calculated separately. accountsData is account details. Commented Aug 17, 2022 at 17:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.