1

I'm trying to use Sorting Datatable //cdn.datatables.net/1.10.12/css/jquery.dataTables.css and //cdn.datatables.net/1.10.12/js/jquery.dataTables.js in Salesforce Lightning Component. I've included required library files. It is working in normal table. Not working in when i used in aura:iteration to display a table. See below image. enter image description here

Component:
    <aura:component controller="sortViewLead" implements="forceCommunity:availableForAllPageTypes" access="global">
    <ltng:require styles="/resource/customsortingDT/customsortingDT/jquery.dataTables.css"
                  scripts="/resource/jquery1,/resource/customsortingDT/customsortingDT/jquery.dataTables.js"
     afterScriptsLoaded="{!c.jsLoaded}" />
    <aura:attribute name="lstLeads" type="list"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <table id="extable" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>$433,060</td>
            </tr>

        </tbody>
    </table>
     <table id="exdumdp" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Company</th>
                    </tr>
            </thead>
            <tbody>
               <aura:iteration var="d" items="{!v.lstLeads}" >
                    <tr>
                        <td>{!d.Name}</td>
                        <td>{!d.Company}</td>
                         </tr>
                </aura:iteration>
            </tbody>
        </table>
</aura:component>

({
    doInit : function(component, event,helper) {
        console.log('Inside Doinit');
        var action = component.get("c.getLeads");
        console.log(action);
        action.setCallback(this,function(a){
            //get the response state
            var state = a.getState();
            //check if result is successfull
            console.log(state);
            if(state == "SUCCESS"){
                var result = a.getReturnValue();
                component.set("v.lstLeads",result); 
            }
            else if(state == "ERROR"){
                alert('Error in calling server side action');
            }
            //adds the server-side action to the queue        

        });
        $A.enqueueAction(action);
    },
    jsLoaded: function(component, event, helper) {

        console.log('Inside Jquery');
        var j$ = jQuery.noConflict();
        //Task Datatable
        j$('#extable,exdumdp').DataTable();

    }
})

/**
For Testing 
**/

public without sharing class  sortViewLead{

@AuraEnabled
    public List<Lead> getLeads1() {
        List<Lead> lstacc =  [SELECT id,Name,Company FROM Lead LIMIT 5];
        system.debug('!!!'+lstacc );
        return lstacc ;
    }
 @AuraEnabled
    public static List<Lead> getLeads() {

        sortViewLead d = new sortViewLead();

        return d.getLeads1();


    }
}
3
  • Could you please add relevant snippet of what you have done/tried? Jan 9, 2017 at 16:37
  • See Using External JavaScript Libraries. You can't import CDN files using Lightning, because the CSP (Content Security Policy) for Lightning doesn't allow external servers.
    – sfdcfox
    Jan 9, 2017 at 16:44
  • If you are just single sorting your table (one column at a time), then it's pretty easy to sort your Javascript array using a custom sort function, or even use the built in Array.sort() provided by javascript. Then you can avoid any third party libraries. Jan 9, 2017 at 17:27

0

Browse other questions tagged .