Friday, September 6, 2019

How To Use jQuery DataTable Plugin In Salesforce Lightning Component


How To Use jQuery DataTable Plugin In Salesforce Lightning Component 

jQuery DataTables is a very powerful plugin for creating table with advanced interaction controls. It provides searching, sorting, pagination  and many more cool features without any configuration. In this post we are going to learn how we can use this jQuery datatable in salesforce lightning components.
So in this sample post, we will fetch the opportunity object records from server and after that display those records using jQuery data table.
Step 1 : Download & Upload jQuery library and  jQuery data table plugin, in static resource.
In order to use data table jQuery plugin, you must include the jQuery, javaScript and CSS file on your Lightning Component via static resources.
Download Data Table jQuery Plugin
Download jQuery library [version 2.2.4 Recommended]
  • jQuery version 2.2.4 is NOT included within the dataTable plugin zip file – you must download it.
  • Go to below link for download JQuery 2.2.4 minified version. (2.2.4 Recommendation)
  • https://code.jquery.com/jquery-2.2.4.min.js
  • Save the JS file on your computer.
Step 2 : Upload Data Table zip file + jQuery js file as a static resources in your Salesforce org.
Static resource is a file or collection of files that is stored on Salesforce. Once your Data Table zip file and jQuery2.2.4 JS file  is added as a static resource, it can be referenced by any of your lightning component.
  • After successfully download both files (DataTables-1.10.16.zip + jQuery js file) now, we can upload these as a static resources.
  • For upload zip file, from Setup >> Develop >> Static Resources >> New.
  • In the Name field, enter the name which is use in lightning component.
  • Click Choose File or Browse, and select dataTable zip file which we downloaded before.
  • In the Cache Control drop-down list, select Public.
  • Click on the Save button.
Follow same steps to upload both files.
Data Table Zip file upload…
 
jQuery 2.2.4 JS file upload
Step 3 : Use jQuery Data Table on salesforce lightning component.
Step 4 : Create Apex Controller 
From developer console >> file >> new >> Apex Class 
jQueryDataTableCtrl.apxc
Step 5 : Create Lightning Component
From developer console >> file >> new >> Lightning Component 
dataTableDemo.cmp
                    Type
                    Stage
                    Amount
                    Close Date
                
            
            
                items="{!v.lstOpp}" var="acc">
                    
                        {!acc.Name}
                        {!acc.Type}
                        {!acc.StageName}
                        {!acc.Amount}
                        {!acc.CloseDate}
                    
                
  
            
        
    
 
 
  • By the ltng:require tag in lightning component we can load external CSS and JavaScript libraries after we upload them as static resources.
dataTableDemoController.js
From developer console >> file >> new >> Lightning Application
demo.app [Lightning Application]