Using jQuery in Lightning Components to Implement Dragging/Sorting
jQuery is a lightweight JavaScript library, which makes it easy to use JavaScript. Using jQuery we can build some rich UI in our Lightning Components.
In order to use jQuery in Lightning Components, we first need to upload jQuery related files to Static Resource. Below are the links for downloading jQuery/jQuery UI
Once we have the files downloaded, we upload them to Static Resource using Setup-->Custom Code-->Lightning Components-->Static Resource
Once we have the files uploaded to the Static Resource, we can use ltng:require tag to refer those jQuery files in our Lightning Components at the time of component loading. Below I have a sample component bundle (Component/Controller/Style) to display couple of "div" sections to display an Image that can be dragged to a droppable area and another section to display few rows that can be sorted using jQuery function.
jQueryComp.cmp:This is the component code to display sections for dragging and dropping Image and to display some sortable rows.
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global"> <!--Loading jQuery from static resource--> <ltng:require scripts="{!join(',', $Resource.jQuery, $Resource.jQuery_UI)}" afterScriptsLoaded="{!c.doInit}"/> <!--Set attribute to store values--> <aura:attribute name="valSet" type="Set"/> <!--Draggable image--> <lightning:tile label="Drag below image to the droppable area"/> <div id="draggable"> <p><img src="{!$Resource.Image1}" width="200" height="200"/></p> </div> <!--Area for dropping above image--> <lightning:tile label="Droppable area"/> <div id="droppable" class="styling1"/> <!--Iterator to display some rows for sorting--> <div class="styling1"> <lightning:tile label="Sort below rows"/> <ul id="sortable"> <aura:iteration var="s" items="{!v.valSet}"> <lightning:card><div id="{!s}">{!s}</div></lightning:card> </aura:iteration> </ul> </div> <br/> </aura:component>
jQueryComp.js: Here in this controller code, I have few jQuery functions that uses draggable/droppable and sortable methods which are done using components "div" id's.
({ doInit : function(cmp, event, helper) { // Variable to input Set values manually var tempSet = ['Drag SetValue1', 'Drag SetValue2', 'Drag SetValue3', 'Drag SetValue4', 'Drag SetValue5']; // Assign above variable to the Set attribute cmp.set("v.valSet", tempSet); // jQuery instantiation after document is loaded jQuery("document").ready(function(){ /* // jQuery function to get div id and implement dragging on that $(".styling").click(function(){ var divname= this.id; console.log(divname); //$("#"+divname).draggable(); //$("#mainId").draggable(); }); */ // jQuery function to enable dragging $(function(){ $("#draggable").draggable(); $("#droppable").droppable(); }); // jQuery function to enable sorting $(function(){ $( "#sortable" ).sortable(); $( "#sortable" ).disableSelection(); }); }); } })
jQueryComp.css: This is a simple css code to use in Lightning Component.
.THIS.styling1 { background-color: lightgrey; width: Auto; border: 25px solid grey; padding: 44px; margin: 20px; }
Finally its now time to demo our code sample. I have placed the above created component on a Home page and here is how it works.....
There are many blogs that explains on how to use jQuery in Lightning Components as jQuery is very light weight JavaScript framework. Below are few links that have inspired me for writing this blog post.
It is nice blog Thank you provide important information and I am searching for the same information to save my time
ReplyDeleteAngularJS 5 Online Course
This comment has been removed by the author.
ReplyDeleteThank you for excellent article.
ReplyDeleteUI Development Training
UI Development Training in Hyderabad
UI Development Online Training
I have got Error,
ReplyDeleteAction failed: c:JqueryUIUsed$controller$onclick [jQuery is not defined]
Any chance you could do this with a LWC instead of Aura? I'm not having any luck.
ReplyDeleteThanks for posting this blog.
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course| Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Developer Training in Chennai | Mean Stack Developer Training in Chennai | Full Stack Training | Certification | Full Stack Online Training Course
Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.
ReplyDeleteweb designing course in chennai
Buy high quality Grade A counterfeit money Real Passports, Visas, Driver's License ,ID CARDS, Marriage certificates, Diplomas, Birth Certificates, Credit cards, Utility bills, Social Security cards, Resident permits, Death certificates, Seaman cards, e.t.c Text/WhatsApp +17202488130 ( lifiben@gmail.com )
ReplyDeletehigh quality undetectable counterfeit banknotes for sale
buy high quality counterfeit money online
buy undetectable counterfeit money online cheap
buy 100% undetectable counterfeit money uk
high quality counterfeit money for sale
counterfeit money supplier
best place to buy counterfeit money
buy counterfeit money online
Text/WhatsApp +17202488130 ( lifiben@gmail.com )
Really awesome blog. Useful information and knowledge. Thanks for posting this blog. Keep sharing more blogs again soon.
ReplyDeleteOnline UI Development Training in Hyderabad
Online RPA Training in Hyderabad
Online Python Training in Hyderabad
Online Mean Development Training in Hyderabad