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
Aakanksha SinghAakanksha Singh 

Mass delete operation through checkbox in lightning component

Hello everyone,
I need to know how can I perform mass delete operation with the use of checkboxes.
Here is my component.
<pre>
<aura:component implements="force:appHostable" controller="AccountListController">
<aura:attribute name="accounts" type="Account[]"/>
<aura:handler name="init" value="{!this}" action="{!c.getAccount}" />
<ui:button class="slds-button slds-button--destructive" press="{!c.delete}">Delete</ui:button><!--Mass Delete operation-->
<table class="slds-table slds-table--bordered slds-max-medium-table--stacked-horizontal">
            <thead>
                <tr class="slds-text-heading--label ">
                    <th class="slds-is-sortable" scope="col">
                        <ui:inputCheckbox label="" class="check" aura:id="master" click="{!c.checkAll}"/>
                        Action                        
                    </th>
                    <th class="" scope="col">Account Name</th>
                </tr>  
            </thead>
            <tbody>
                <aura:iteration items="{!v.accounts}" var="account">
                    <tr class="slds-hint-parent">
                        <td class="" data-label="Action" style="padding-left:0;">
                            <ui:inputCheckbox label="" class="check" text="{!account.Id}" aura:id="dependent"/>
                        </td>
                        <td class="" data-label="Account Name" style="padding-left:0;">
                            <a href="{! '#/sObject/' + account.Id + '/view'}">{!account.Name}</a>
                        </td>                                 
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
<aura:component>
</pre>
This is the client side controller:
<pre>
getAccount : function(component,event,helper) {
        var action=component.get("c.getAccountList");
        action.setCallback(this, function(a) {
            component.set("v.accounts", a.getReturnValue());
        });
        $A.enqueueAction(action);
    },
checkAll:function(component,event,helper){
        var master= component.find("master");
        var dep= component.find("dependent");
        var val= master.get("v.value");
        if(val==true){
            for(var i=0;i<dep.length;i++){
                dep[i].set("v.value",true);
            }
        }else{
           for(var i=0;i<dep.length;i++){
                dep[i].set("v.value",false);
            }
        }
    },
    delete:function(cmp,evt,helper){}
</pre>
This is the sever side controller:
<pre>
public with sharing class AccountListController {
    @AuraEnabled
    public static List<Account> getAccountList() {
        return [SELECT Id,Name,Accountnumber FROM Account order by CreatedDate desc];
    }       
}
</pre>
Kindly help.
Thanks
Regards
 
Best Answer chosen by Aakanksha Singh
sfdcMonkey.comsfdcMonkey.com
Hi Aakanksha Singh
update in your code with below code for delete by selected checkbox 

in your component add this line for store id of selected checkbox
<aura:attribute name="massDeleteList" type="String[]"/>
in your js controler 
delete:function(component,event,helper){
      var dep1 = component.find("dependent");
      var listOfId = [];
    for(var i=0;i<dep1.length;i++){
        var cond = dep1[i].get("v.value");
        if( cond == true){
          listOfId.push(component.find("dependent")[i].get("v.text"));
          }  
       console.log('selectd id' + listOfId);
       } // for loop close 
         component.set("v.massDeleteList" , listOfId);       
         var delIdsPassInClass = component.get("v.massDeleteList");
         var action = component.get("c.DeleteMass");
         action.setParams({ 
             "delIDs" :  delIdsPassInClass
             });
         action.setCallback(this, function(response) {
            var state = response.getState();
            alert(state);
            if (state === "SUCCESS") {    
              
            }
        });
         $A.enqueueAction(action);
   
    },
  in apex class 
@AuraEnabled 
    public static void DeleteMass(List<String> delIDs){
       
     List<account> lstOfDeleteAccount =  new List<account>();
         for(String st: delIDs){
             account acc = new account();
             acc.Id = st ;
               lstOfDeleteAccount.add(acc);  
          }
          Delete lstOfDeleteAccount;
 
    }

Read code line by line and Understand this functionality :)
Thanks 
let me inform if any problem with it or if it helps you mark it best answer :)
 

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi Aakanksha Singh
update in your code with below code for delete by selected checkbox 

in your component add this line for store id of selected checkbox
<aura:attribute name="massDeleteList" type="String[]"/>
in your js controler 
delete:function(component,event,helper){
      var dep1 = component.find("dependent");
      var listOfId = [];
    for(var i=0;i<dep1.length;i++){
        var cond = dep1[i].get("v.value");
        if( cond == true){
          listOfId.push(component.find("dependent")[i].get("v.text"));
          }  
       console.log('selectd id' + listOfId);
       } // for loop close 
         component.set("v.massDeleteList" , listOfId);       
         var delIdsPassInClass = component.get("v.massDeleteList");
         var action = component.get("c.DeleteMass");
         action.setParams({ 
             "delIDs" :  delIdsPassInClass
             });
         action.setCallback(this, function(response) {
            var state = response.getState();
            alert(state);
            if (state === "SUCCESS") {    
              
            }
        });
         $A.enqueueAction(action);
   
    },
  in apex class 
@AuraEnabled 
    public static void DeleteMass(List<String> delIDs){
       
     List<account> lstOfDeleteAccount =  new List<account>();
         for(String st: delIDs){
             account acc = new account();
             acc.Id = st ;
               lstOfDeleteAccount.add(acc);  
          }
          Delete lstOfDeleteAccount;
 
    }

Read code line by line and Understand this functionality :)
Thanks 
let me inform if any problem with it or if it helps you mark it best answer :)
 
This was selected as the best answer
Aakanksha SinghAakanksha Singh
Thanx for the help.
@soni piyush
sfdcMonkey.comsfdcMonkey.com
pleasure :)
and what is the staus of your below quetion. 
you can ask me any question if any doubt on this question.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000g2FvIAI
best regards 
Aakanksha SinghAakanksha Singh
It is not solved yet, still working..
Umang SinghalUmang Singhal
check the below link which has complete working code.
https://umangsinghal.wordpress.com/2017/06/11/move-selected-table-values-from-one-component-to-another