function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Janno RipJanno Rip 

Delete a row in Lighting Component Table

Hello everyone,

I am getting started with Lighting Components and build my own first tabe. I am displaying all quotedocuments from all quotes on the opportunity level so that our sales sees all documents in one table and does not have to open every single quote. Here is what I got so far:

User-added image

Now with Sales being Sales they make mistakes and create quotedocuments (pdfs) they dont actually need. So they need to be able to delete these quotedocuments in this table. That is the point where I am stuck. I found some documentation but I always end up with deleting the entire opportunity. My biggest challenge is to access every single quotedocument based on the row the button is hit.

Here is my apex class:

public class JTo_ListOfPDFs {
    @AuraEnabled
public static List <Quote> getPDF(Id opportunityId) 
    {
        return [SELECT Id,Name,CreatedDate,QuoteNumber,TotalPrice,(SELECT Id,CreatedDate,Name,ContentVersionDocumentId,QuoteId FROM QuoteDocuments )FROM Quote WHERE OpportunityId =  :opportunityId ORDER BY Id DESC];
         
    }    
}
The component
<aura:component controller="JTo_ListOfPDFs" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    
	<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:dependency resource="markup://force:editRecord" type="EVENT" />
    <aura:handler event="force:refreshView" action="{!c.doInit}" />

    <!--aura attributes--> 
    <aura:attribute name="quoteDocumentRows" type="Object[]"/>
    <aura:attribute name="recordId" type="Id" />

    <!--Page header-->
    <div class="slds-page-header" role="banner">
        <span class="slds-text-heading_medium">List of PDFs</span>
        <ui:button class="btn" press="{!c.setOutput}"/>
    </div>

     <!--Lightning data table markup-->
    <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="Quote">Quote Number</div>
		      </th>
                
                              
		      <th scope="col">
                    <div class="slds-truncate" title="Name">PDF</div>
		      </th>
		      <th scope="col">
		        <div class="slds-truncate" title="Date">Date</div>
		      </th>
                <th scope="col">
		        <div class="slds-truncate" title="Date">Delete</div>
		      </th>
            </tr>
		   </thead>
  	<tbody>
   <aura:iteration items="{!v.quoteDocumentRows}" var="cell" >
     <aura:iteration items="{!cell.QuoteDocuments}" var="pdf"  >	

 	 <tr>
         
         <td data-label="Quote Number">
        <div class="slds-truncate" title="Quote Number">
            <a href="{! '/'+cell.Id}" target="_blank">{!cell.QuoteNumber} </a>
      </div>
          </td>

      <td data-label="PDF">
        <div class="slds-truncate" title="PDF">
            <a href="{!'https://meinestadt--partial.lightning.force.com/servlet/servlet.FileDownload?file='+ pdf.Id}" target="_blank">{!pdf.Name} </a>
      </div>
          </td>
        
      <td data-label="Date"> <lightning:formattedDateTime year="numeric" month="numeric" day="numeric"  hour="2-digit" minute="2-digit" value="{!pdf.CreatedDate}" /> 
       
      </td>
       <td>
         <lightning:buttonIcon aura:id="deleteButton" iconName="utility:close" size="small" variant="border-filled" alternativeText="Close"/>
            </td>
    </tr>
	 </aura:iteration>
	</aura:iteration>
    
  </tbody>	
	
  </table>
    
 
</aura:component>
The controller
({
    doInit : function(component, event, helper){
        helper.getAllPDFs(component);
        
    }    
    
})
The Helper
({
	getAllPDFs : function(component, event, helper) {
        var action = component.get("c.getPDF");
        action.setCallback(this, function(a){
            component.set("v.quoteDocumentRows", a.getReturnValue());
        });
        action.setParams({
            opportunityId: component.get("v.recordId")
        });
       //https://developer.salesforce.com/blogs/developer-relations/2015/11/building-context-aware-lightning-components.html
        $A.enqueueAction(action);
	},
    setOutput : function(component, event, helper) {
        var editRecordEvent = $A.get("e.force:editRecord");
        editRecordEvent.setParams({
             "recordId": component.get("v.recordId")
        });
        editRecordEvent.fire();
	}
})

Can anyone point me in the right direction?  That would be great.

Thanks in Advance!

 
AbhishekAbhishek (Salesforce Developers) 
https://www.biswajeetsamal.com/blog/dynamic-add-delete-row-in-lightning-component/

Try the code snippet as mentioned in the above blog.

For further reference check this too (https://www.forcetalks.com/blog/how-to-add-or-delete-dynamic-rows-in-salesforce-using-lightning-web-component/).

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Janno RipJanno Rip
Hi @Abhishek,

thanks for the quick reply. However this link seems to be dead to me.

User-added image
AbhishekAbhishek (Salesforce Developers) 
I can able to access this URL,

https://www.forcetalks.com/blog/how-to-add-or-delete-dynamic-rows-in-salesforce-using-lightning-web-component/

Try clear cache and cookies for the browser or Try Incongitino window, You can try in another browser.
 
sachinarorasfsachinarorasf
Hi Janno Rip,

Please go through the below link to get delete functionality for row in the lightning data table component

https://blog.sujeshram.com/2017/06/update-delete-record-from-datatable-listview-lightning.html

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com