lightning:datatable with header and footer in Salesforce Lightning Component

lightning:datatable with header and footer in Salesforce Lightning Component

Sample Code:


Apex Class:

  1. public class AccountListController {  
  2.       
  3.     @AuraEnabled  
  4.     public static List < Account > fetchAccts() {  
  5.       
  6.         return [ SELECT Id, Name, Industry, Type FROM Account LIMIT 100 ];  
  7.           
  8.     }  
  9.       
  10. }  



Lightning Component:

  1. <aura:component implements=“force:appHostable” controller=“AccountListController” >  
  2.       
  3.     <aura:attribute type=“Account[]” name=“acctList”/>  
  4.     <aura:attribute name=“mycolumns” type=“List”/>  
  5.       
  6.     <aura:handler name=“init” value=“{!this}” action=“{!c.fetchAccounts}”/>  
  7.                         
  8.             <lightning:card footer=“Only 100 accounts records are loaded” title=“Accounts”>  
  9.                 <p class=“slds-p-horizontal_small”>  
  10.                       
  11.                     <lightning:datatable data=“{! v.acctList }”   
  12.                                          columns=“{! v.mycolumns }”   
  13.                                          keyField=“id”  
  14.                                          hideCheckboxColumn=“true”/>  
  15.                               
  16.                 </p>  
  17.             </lightning:card>     
  18.       
  19. </aura:component>  



Lightning JavaScript Controller:

  1. ({  
  2.       
  3.     fetchAccounts : function(component, event, helper) {  
  4.           
  5.         component.set(‘v.mycolumns’, [  
  6.             {label: ‘Account Name’, fieldName: ‘Name’, type: ‘text’},  
  7.                 {label: ‘Industry’, fieldName: ‘Industry’, type: ‘text’},  
  8.                 {label: ‘Type’, fieldName: ‘Type’, type: ‘Text’}  
  9.             ]);  
  10.         var action = component.get(“c.fetchAccts”);  
  11.         action.setParams({  
  12.         });  
  13.         action.setCallback(this, function(response) {  
  14.             var state = response.getState();  
  15.             if (state === “SUCCESS”) {  
  16.                 component.set(“v.acctList”, response.getReturnValue());  
  17.             }  
  18.         });  
  19.         $A.enqueueAction(action);  
  20.           
  21.     }  
  22.       
  23. })  



Output:

Leave a Reply