+ Start a Discussion
bharath kumar 52bharath kumar 52 

How to add a new column to editable lightning table

Hi All,

I am working on a requirement where i am supposed to make the table editable and capture values. The challenge here is that i have to capture values from this table and store it in another object so i think i am supposed to add a new column to lightning datatable using wrapper.
User-added image

The user should enter values in columns amount and reason which i am planning to implement through wrapper but how to set values is something which is confusing me and i am not sure if this approach will work. Please check my code and let me know if you have any suggestions.
Component Code:

<aura:component controller="OpportunityAdjustmentController">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="OppId" type="Id" />
    <aura:attribute name="listOfAdjustments" type="OpportunityLineItem[]" />
	<aura:attribute name="wrapperList" type="object"/>
    
  <div class="slds-p-around--large">
      
     
      <p style="color:red">{!v.wrapperList}</p>
      
  <!--https://www.lightningdesignsystem.com/components/data-tables/-->
    <table class="slds-table slds-table--bordered slds-table--cell-buffer">
      <thead>
        <tr class="slds-text-title--caps">
          <th scope="col">
            <div class="slds-truncate" title="Product Name">Product Name</div>
          </th>
          <th scope="col">
            <div class="slds-truncate" title="Quantity">Quantity</div>
          </th>
          <th scope="col">
            <div class="slds-truncate" title="Adjusted Amount">Adjusted Amount</div>
          </th>
          <th scope="col">
            <div class="slds-truncate" title="Adjusted Quantity">Adjusted Quantity</div>
          </th>
        </tr>
      </thead>
      <!--table body start, 
        Iterate contact list as a <tr>
        -->
      <tbody>
          <aura:iteration items="{!v.wrapperList}" var="wrapObj">
            <!--<aura:iteration items="{!v.wrapperList.lstContact}" var="con"> -->
              <tr>
                <th scope="row">
                  <div class="slds-truncate" title="{!wrapObj.oli.name}">{!wrapObj.oli.name}</div>
                </th>
                <th scope="row">
                  <div class="slds-truncate" title="{!wrapObj.adjustmentReason}">{!wrapObj.adjustmentReason}</div>
                    <!--Planning to use input tag here-->
                </th>
                <th scope="row">
                  <div class="slds-truncate" title="{!wrapObj.adjustmentAmount}">{!wrapObj.adjustmentAmount}</div>
                     <!--Planning to use input tag here-->
                </th>
                <th scope="row">
                   <div class="slds-truncate" title="{!con.LeadSource}">{!con.LeadSource}</div>
                </th>
              </tr>
            <!-- </aura:iteration> -->
         </aura:iteration>
      </tbody>
    </table>
  </div>
</aura:component>
 
Apex controller :
public class OpportunityAdjustmentController {
public static list<opportunityAdjustment> adjustmentList = new list<opportunityAdjustment>();
    @AuraEnabled
    public static list<opportunityAdjustment> getOpportunityAdjustment(Id OpportunityId){
        list<opportunitylineitem> oliList = new list<opportunitylineitem>([select id,name,opportunityid,unitprice,quantity from opportunitylineitem where
                                                                           opportunityid=:OpportunityId]);
        for(opportunitylineitem oli : oliList){
            string reason;
            Decimal adjAmount=0;
            adjustmentList.add(new opportunityAdjustment(oli,'',adjAmount,oli.opportunityid,oli.Id));
        }
        return adjustmentList;
    }
    
    @AuraEnabled
    public static list<opportunityAdjustment> createOpportunityAdjustment(list<opportunityAdjustment> oppAdjlist){
        //list<opportunitylineitem> oliList = new list<opportunitylineitem>([select id,name,opportunityid,unitprice,quantity from opportunitylineitem where opportunityid=:OpportunityId]);
        list<Opportunity_Adjustment__c> adjustmentListToInsert = new list<Opportunity_Adjustment__c>();
        Opportunity_Adjustment__c oppAdj = new Opportunity_Adjustment__c();
        for(opportunityAdjustment oa : oppAdjlist){
            oppAdj.Adjustment_amount__c = oa.adjustmentAmount;
            oppAdj.Reason_for_adjustment__c = oa.adjustmentReason;
            oppAdj.Opportunity__c = oa.oppId;
            adjustmentListToInsert.add(oppAdj);
            //adjustmentListToInsert.add(new opportunityAdjustment(oli,'',adjAmount,oli.opportunityid,oli.Id));
        }
        if(adjustmentListToInsert!=null){
            
           insert adjustmentListToInsert;
        }
        return adjustmentList;
    }


    class opportunityAdjustment{
        @AuraEnabled
        public opportunitylineitem oli{get;set;}
        @AuraEnabled
        public string adjustmentReason{get;set;} // set data on attribute level
        @AuraEnabled
        public Decimal adjustmentAmount{get;set;} // set data on attribute level
        @AuraEnabled
        public string oppId{get;set;}
        @AuraEnabled
        public string oliId{get;set;} 
        
        opportunityAdjustment(OpportunityLineItem o, String s, Decimal d, Id oppId, Id oliId){}
        
        
    
    }


}

Thanks,
Bharath​​​​​​​