Lightning component with drag and drop functionality to delete records.

In the following example, we will see how to implement 'drag and drop' feature in lightning component. We will see a small example to implement this, I am using standard contact object and I will try to fetch the contacts in my component and delete one contact by dragging and dropping the contact into a recycle bin.

Screenshots: -



Apex: -


  1. public class getContact {
  2. @AuraEnabled
  3. public static List<Contact> getContactList(){
  4. List<Contact> con = [SELECT Id, Name, Email FROM Contact];
  5. return con;
  6. }
  7. @AuraEnabled
  8. public static void deleteContact(String contactId){
  9. contact[] con = [SELECT Id,Name FROM Contact WHERE Id =: contactId];
  10. try{
  11. delete con;
  12. }
  13. catch (DmlException e){
  14. }
  15. }

  16. }

Component: -
ListOfContact.cmp
  1. <aura:component controller="getContact">
  2. <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  3. <aura:attribute name="contact" type="Object" />
  4. <lightning:layout>
  5. <lightning:layoutItem size="8">
  6. <lightning:card title="Contacts" iconName="standard:contact">
  7. <aura:iteration items="{!v.contact}" var="con" indexVar="index">
  8. <lightning:layoutItem size="8">
  9. <c:contactTile contact="{#con}" index="{#index}" />
  10. </lightning:layoutItem>
  11. </aura:iteration>
  12. </lightning:card>
  13. </lightning:layoutItem>
  14. <lightning:layoutItem size="4" padding="around-medium">
  15. <c:recycleBin />
  16. </lightning:layoutItem>
  17. </lightning:layout>
  18. </aura:component>
ListOfContact.js
  1. ({
  2. doInit : function(component, event, helper) {
  3. var action = component.get('c.getContactList');
  4. action.setCallback(this, function(response){
  5. var state = response.getState();
  6. if(state === 'SUCCESS'){
  7. component.set('v.contact', response.getReturnValue());
  8. }
  9. });
  10. $A.enqueueAction(action);
  11. }
  12. })
ListOfContact.css
  1. .THIS .bin{
  2. background: #ccc;
  3. }
contactTile.cmp
  1. <aura:component >
  2. <aura:attribute name="contact" type="Contact" />
  3. <aura:attribute name="index" type="Integer" />
  4. <div id="{#v.contact.Id}" draggable="true" ondragstart="{!c.handleDrag}">
  5. <lightning:layout verticalAlign="center" multipleRows="true">
  6. <lightning:layoutItem padding="around-small">
  7. <lightning:icon iconName="utility:rows" size="x-small" />
  8. </lightning:layoutItem>
  9. <lightning:layoutItem padding="around-small">
  10. <div class="slds-text-heading_small">{#v.contact.Name}</div>
  11. <div class="slds-text-heading_small">{#v.contact.Email}</div>
  12. </lightning:layoutItem>
  13. </lightning:layout>
  14. </div>
  15. </aura:component>
contactTile.js
  1. ({
  2. handleDrag : function(component, event, helper) {
  3. var contact = component.get('v.contact');
  4. var id = contact.Id;
  5. var dom = document.getElementById(id);
  6. dom.classList.add('ccc');
  7. event.dataTransfer.setData("contact", JSON.stringify(contact));
  8. }
  9. })
recycleBin.cmp
  1. <aura:component controller="getContact">
  2. <aura:attribute name="binVariant" type="String" default="warning" />
  3. <div aura:id="dropZone" class="drop-zone" ondragover="{!c.dragOverHandler}" ondragleave="{!c.dragLeaveHandler}" ondrop="{!c.handleDrop}">
  4. <lightning:icon iconName="utility:recycle_bin_empty" variant="{!v.binVariant}" size="large" />
  5. </div>
  6. </aura:component>
recycleBin.js
  1. ({
  2. dragOverHandler : function(component, event, helper) {
  3. event.preventDefault();
  4. component.set('v.binVariant', 'error');
  5. var cmpTarget = component.find("dropZone");
  6. $A.util.addClass(cmpTarget, 'active');
  7. },
  8. dragLeaveHandler : function(component, event, helper) {
  9. event.preventDefault();
  10. component.set('v.binVariant', 'warning');
  11. var cmpTarget = component.find("dropZone");
  12. $A.util.removeClass(cmpTarget, 'active');
  13. },
  14. handleDrop : function(component, event, helper) {
  15. event.preventDefault();
  16. var contact = event.dataTransfer.getData("contact");
  17. var action = component.get('c.deleteContact');
  18. action.setParams({'contactId': JSON.parse(contact).Id});
  19. action.setCallback(this, function(response){
  20. var state = response.getState();
  21. if(state === 'SUCCESS'){
  22. alert('The contact deleted successfully');
  23. }
  24. });
  25. $A.enqueueAction(action);
  26. },
  27. })
recycleBin.css
  1. .THIS {
  2. cursor: grab;
  3. width: 100%;
  4. transform: scaleX(1) translateZ(0);
  5. transform-origin: 50% 0;
  6. }
  7. .THIS:hover {
  8. background: aliceblue;
  9. }
  10. .THIS.ccc {
  11. transform: scaleX(0) translateZ(0);
  12. transition: transform 0.3s;
  13. }
App: -
  1. <aura:application extends="force:slds" access="GLOBAL" >
  2. <div class="slds-m-around--small">
  3. <c:ListOfContact />
  4. </div>
  5. </aura:application>

Comments

Popular posts from this blog

Thank you page with simple HTML and CSS

Lightning component to display chart using Chart.Js

Lightning component with drag and drop feature