function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ForceRookieForceRookie 

How to create lightning component that will display all records in data table

Hi! Help me to create a lightning component that if I click the CustomObject tab, it will display all of its records in data table. Just similar layout if we click an Object tab.

I’m new to lightning. Any help will be appreciated.
Best Answer chosen by ForceRookie
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

You can place this component on the detail page or you can create a 'Lightning Tab'.

Note: I have used Opportunity object, you can replace Opportunity with your custom object.

Component:
<aura:component controller="DisplayRecordsC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <!-- attributes -->
    <aura:attribute name="data" 
                    type="Object"/>
    
    <aura:attribute name="columns" 
                    type="List[]"/>
    
    <!-- handlers -->
    <aura:handler name="init" 
                  value="{!this}" 
                  action="{!c.doInit}"/>
    
    <!-- datatable -->
    <lightning:datatable aura:id="opportunitydatatable"
                         
                         keyField="Id"
                         data="{!v.data}"
                         hideCheckboxColumn="true"
                         columns="{!v.columns}"/>
</aura:component>

Controller:
({
	doInit : function(component, event, helper) {
        
        helper.queryColumns(component,event,helper);        
        helper.queryOpp(component,event,helper);
    },
})

Helper:
({
	queryColumns : function(component,event,helper) {
        
        component.set('v.columns', [
            {label: 'Opp Name', fieldName: 'Name', editable: true, type: 'text'},
            {label: 'Acc Name', fieldName: 'AccountName', type: 'text'},
            {label: 'StageName', fieldName: 'StageName', editable: true, type: 'text'},
            {label: 'CloseDate', fieldName: 'CloseDate', type: 'date'},
            {label: 'Amount', fieldName: 'Amount', type: 'currency', cellAttributes: { alignment: 'left' }},
        ]);
    },
    
    queryOpp : function(component,event,helper) {
        
        var action=component.get('c.getOpportunities');
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var rows = response.getReturnValue();
                component.set("v.data", rows);
                
            }
        });
        $A.enqueueAction(action);
    },
})

Apex:
public class DisplayRecordsC {
    
    @AuraEnabled
    public static List<Opportunity> getOpportunities()
    {
        List<Opportunity> oppList=new List<Opportunity>();
        oppList=[SELECT Name,Account.Name,StageName,CloseDate,Amount FROM Opportunity];
        return oppList;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

You can place this component on the detail page or you can create a 'Lightning Tab'.

Note: I have used Opportunity object, you can replace Opportunity with your custom object.

Component:
<aura:component controller="DisplayRecordsC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <!-- attributes -->
    <aura:attribute name="data" 
                    type="Object"/>
    
    <aura:attribute name="columns" 
                    type="List[]"/>
    
    <!-- handlers -->
    <aura:handler name="init" 
                  value="{!this}" 
                  action="{!c.doInit}"/>
    
    <!-- datatable -->
    <lightning:datatable aura:id="opportunitydatatable"
                         
                         keyField="Id"
                         data="{!v.data}"
                         hideCheckboxColumn="true"
                         columns="{!v.columns}"/>
</aura:component>

Controller:
({
	doInit : function(component, event, helper) {
        
        helper.queryColumns(component,event,helper);        
        helper.queryOpp(component,event,helper);
    },
})

Helper:
({
	queryColumns : function(component,event,helper) {
        
        component.set('v.columns', [
            {label: 'Opp Name', fieldName: 'Name', editable: true, type: 'text'},
            {label: 'Acc Name', fieldName: 'AccountName', type: 'text'},
            {label: 'StageName', fieldName: 'StageName', editable: true, type: 'text'},
            {label: 'CloseDate', fieldName: 'CloseDate', type: 'date'},
            {label: 'Amount', fieldName: 'Amount', type: 'currency', cellAttributes: { alignment: 'left' }},
        ]);
    },
    
    queryOpp : function(component,event,helper) {
        
        var action=component.get('c.getOpportunities');
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var rows = response.getReturnValue();
                component.set("v.data", rows);
                
            }
        });
        $A.enqueueAction(action);
    },
})

Apex:
public class DisplayRecordsC {
    
    @AuraEnabled
    public static List<Opportunity> getOpportunities()
    {
        List<Opportunity> oppList=new List<Opportunity>();
        oppList=[SELECT Name,Account.Name,StageName,CloseDate,Amount FROM Opportunity];
        return oppList;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
ForceRookieForceRookie
Hello Khan, Thank you for your reply!
Do you have an idea also on how to make the Name clickable? And will direct to the onject’s record details?