+ Start a Discussion
akash_dev__cakash_dev__c 

I have used lightning data table for my custom object and I am not able to see any output?

Hey,

I have used lighning datatable to show my custom object fields on thr browser but I am not getting any data on my screen.

Below is the code I have written:

This is my main component:

<aura:component implements="force:appHostable" controller="EntitlementListControllerx">
    
    <!-- attributes -->
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>
    
    <!-- handlers-->
    <aura:handler name="init" value="{!c.doInit }" action="{!this }"/>


    <!-- the container element determine the height of the datatable -->
    <div style="height: 300px">
        <lightning:layout horizontalAlign="space">
            <lightning:layoutItem size="12">
                <lightning:datatable
                                    keyField="id"
                                    data="{! v.data }"
                                    columns="{! v.columns }"
                                    hideCheckboxColumn="true"/>
                </lightning:layoutItem>
        </lightning:layout>
    </div>
</aura:component>

Controller:

({
    doInit: function (cmp, event, helper) {
        cmp.set('v.columns', [
            {label: 'Entitlement Name', fieldName: 'Name', type: 'text'},
            {label: 'Entitlement Number', fieldName: 'Entitlement_Name__c', type: 'text'},
            {label: 'Entitlement Amount', fieldName: 'Entitlement_Amount__c', typeAttributes: { currencyCode: 'USD'}},
            {label: 'Entitlement Type', fieldName: 'Entitlement_Type__c', type: 'text'},
        ]);

        helper.getEntitlement(cmp,helper);
    }
})

Helper:\

({
    getEntitlement : function(cmp) {
        helper.callServer(cmp,"c.getEntitlementList",
                                   function(response){
                                       for(var i=0; i>response.length; i++){
                                           var row = response[i];
                                           row.Owner = row.Contact__r.Name;
                                       }
                                        cmp.set("v.data", response);//set data in the page variable
        });
    }
})

Controller to fetch the data:

public with sharing class EntitlementListControllerx     
{
     @AuraEnabled//Annotation to use method in lightning component
    
    public static List<Entitlement__c> getEntitlementList() 
    {//Fetch data
        return [SELECT Id,Name,Entitlement_Name__c, Entitlement_Amount__c, Entitlement_Type__c, CreatedDate FROM Entitlement__c];
    }    
}

Please help!
Raj VakatiRaj Vakati
your code looks good but      where is    helper.callServer(cmp,"c.getEntitlementList") method ?
akash_dev__cakash_dev__c
Hi Raj V, 

getEntitlementList this is a function in the above class EntitlementListControllerx and I am calling getEntitlementList from EntitlementListControllerx controller. I am not able to dentify what's missing. Could you please tell me a bit more. It would be a great help.

Thanks
Balagopal GBalagopal G

Hi

are you trying to call to the backend.

If yes , please consider the following link.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

you need to define the following

var action=component.get("c.methodName");

// set params if needed.
 define a callback that is executed after
action.setCallback(this,function(result){
   if(result.getState()=="SUCCESS"){
       // do the needful
   }
)};

//add to the server side action to the queue

$A.enqueueAction(action);