Salesforce : Lightning DataTable
Lightning DataTable
LightningDataTableController(Apex Controller )
public class LightningDataTableController{
@AuraEnabled
public static List<Contact> fetchAccountContacts(String contactId) {
List<Contact> lstContact=new List<Contact>();
lstContact=[select id ,Name,Email,Phone from contact where accountid=:contactId];
return lstContact;
}
}
LightningDataTable.cmp (Component)
<aura:component controller="LightningDataTableController"
implements="force:hasRecordId,flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader" access="global">
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:attribute name="data" type="Object"/>
<aura:attribute name="columns" type="List" default="[{label: 'Name', fieldName: 'LinkName', type: 'url', typeAttributes: {label: { fieldName: 'Name' }, target: '_blank'}},
{label: 'Email', fieldName: 'Email', type: 'text'},
{label: 'Phone', fieldName: 'Phone', type: 'text'}]"/>
<aura:attribute name="recordId" type="String" default="" />
<!-- the container element determine the height of the datatable -->
<lightning:card aura:id="lightCard" class="slds-card_boundary" title="{!'Contacts ('+v.data.length+')'}" iconName="standard:contact">
<div style="overflow-x: auto;">
<lightning:datatable style="height: 90% !important;display: block;"
aura:id="contacts"
columns="{!v.columns}"
data="{!v.data}"
keyField="id"
/>
</div>
</lightning:card>
</aura:component>
LightningDataTableController.js (JS Controller )
({
doInit : function(component, event){
var contactAction = component.get("c.fetchAccountContacts");
contactAction.setParams({
contactId: component.get("v.recordId")
});
contactAction.setCallback(this,function(response){
var state = response.getState();
if (state === "SUCCESS" && response.getReturnValue() != '') {
var records =response.getReturnValue();
records.forEach(function(record){
record.LinkName = '/'+record.Id;
});
component.set('v.data',response.getReturnValue());
}else if(state === "ERROR"){
console.log('A problem occurred: ' + JSON.stringify(response.error));
}
});
$A.enqueueAction(contactAction);
}
})
Note: After save whole code you will drag and drop code into Lightning Record Detail Page of Account

This comment has been removed by a blog administrator.
ReplyDeleteThis article does not include the other configuration. I created all bundle but do not see Account and Contact tabs, neither New Contact button. Could you please post this article in its entirety? I am new to lightning.
ReplyDeleteThis is only a simple example of DataTable if you wants more option you have read Lightning Documents.
DeleteThanks but please don't add any Ad with Comments...
ReplyDeleteThanks a lot for sharing a valuable blog on Salesforce lightning. I was browsing through the internet looking for salesforce lightning and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject, you can find more information on Salesforce lightning here:
ReplyDeleteSalesforce lightning interview questions and answers
Salesforce lightning
Thank you for appreciating my blog please share with your contact.
DeleteThanks for sharing this! The content was engaging and easy to follow. Great job!
ReplyDeletelightning training salesforce
I truly enjoyed reading this post. It’s packed with useful insights and practical information.
ReplyDeletelightning web components course