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
Tom O WardTom O Ward 

lightning data table - data not displayed

I've had some difficulty displaying data from a SOQL query in a lightning data table. Currently I get the column headings but no data.

My intention is to display a series of SOQL queries with 4 columns (row name, SOQL query for this month, SOQL Query for last month, expected value). At this point I only have the SOQL query for one row until I can get the data displaying.

I'm much more experienced with the backend part of this than the front end so I'm sure I'm missing something obvious as to why the data is not displaying

Component:
<aura:component implements="flexipage:availableForAllPageTypes" access="global"  controller="FieldRepQuery">
    <aura:attribute name="mydata" type="Object"/>
    <aura:attribute name="mycolumns" type="List"/>
    	<aura:handler name="init" value="{! this }" action="{! c.init }"/>
    		<div style="height: 300px">
    		<lightning:datatable data="{! v.mydata }"
        		columns="{! v.mycolumns }"
        		keyField="opportunityName"
        		hideCheckboxColumn="true"/>
             </div>    
</aura:component>
Controller
({
    init: function (cmp, event, helper) {
                
		helper.queryColumns(cmp, event, helper);
        
        helper.queryData(cmp, event, helper);
            
        }          
    
})
Helper
({
	queryColumns : function(cmp, event, helper) {
        
		  cmp.set('v.mycolumns', [
            { label: 'Field', fieldName: 'RowName', type: 'text'},
            { label: 'This Month', fieldName: 'ThisMonth', type: 'text'},
            { label: 'Last Month', fieldName: 'LastMonth', type: 'text'},
            { label: 'Expected', fieldName: 'Expected', type: 'text'}
              ])
	},

 	queryData : function(cmp,event,helper) {
        
        var action=cmp.get('c.getData');
        
        action.setParams({
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.data", response.getReturnValue());
                
            }
        });
        $A.enqueueAction(action);
    }
       
})
Apex query
public class FieldRepQuery {      
@AuraEnabled
    public static List<TableRow> getData(){
        List<TableRow> Data = new List<TableRow>();
        list<AggregateResult> SamplesThs = [SELECT COUNT (Id)cnt FROM Stock_Usage__c WHERE (Product_Transfer_Type__c = 'Sample' AND Transaction_Date__c = THIS_MONTH AND Owner.Id =:UserInfo.getUserId())];
        string SamplesThis = string.valueOf(SamplesThs[0].get('cnt'));
        list<AggregateResult> SamplesLst = [SELECT COUNT (Id)cnt FROM Stock_Usage__c WHERE (Product_Transfer_Type__c = 'Sample' AND Transaction_Date__c = LAST_MONTH AND Owner.Id =:UserInfo.getUserId())];
        string SamplesLast = string.valueOf(SamplesThs[0].get('cnt'));
        TableRow Samples = new TableRow('Samples', SamplesThis, SamplesLast,'5'); 
        system.debug(Samples);
        return Data;
    }
    
    public class TableRow {		
        public string RowName   {get; set;}
        public string ThisMonth   {get; set;}
        public string LastMonth   {get; set;}
        public string Expected       {get; set;}
        
        public TableRow(String RN, string TM, string LM, string E){
            this.RowName = RN;
            this.ThisMonth = TM;
            this.LastMonth = LM;
            this.Expected = E;
        }
    } 
   
}
ShivankurShivankur (Salesforce Developers) 
Hi Tom,

It seems you are using 2 different variables at 2 different places, it means while retrieving you are returning List of table rows with data as variable. But you using mydata in the lightning component. 

You should use set or assign the data retrieved into mydata variable and then use it, as per current it seems the attribute is not getting set with any values and not displaying any.

You can follow documentation to understand about this:
https://www.sfdcpoint.com/salesforce/lightning-datatable-example-salesforce/

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
Tom O WardTom O Ward
Thanks for taking the time to have a look Shivankur and spotting that, I've updated the helper to refer to mydata (updated below).

Unfortunately I'm still getting a blank table. I have confirmed the apex returning the correct results
I've done this through a dashboard in the mean time but was trying to setup something a bit more customisable and without the refresh button requirement
 
({
	queryColumns : function(cmp, event, helper) {
        
		  cmp.set('v.mycolumns', [
            { label: 'Field', fieldName: 'RowName', type: 'text'},
            { label: 'This Month', fieldName: 'ThisMonth', type: 'text'},
            { label: 'Last Month', fieldName: 'LastMonth', type: 'text'},
            { label: 'Expected', fieldName: 'Expected', type: 'text'}
              ])
	},

 	queryData : function(cmp,event,helper) {
        
        var action=cmp.get('c.getData');
        
        action.setParams({
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.data", response.getReturnValue());
                
            }
        });
        $A.enqueueAction(action);
    }
       
})