Lightning datatable displaying rows but values are not visible

auradatadynamic-soqllightning-datatable

I am trying to display Account records through dynamic SOQL, and I am getting the queried data in the console. Also, the result screen is displaying the rows with respect to that data, but the values in it aren't visible.

I am attaching the screenshot of the result screen, and I am adding my code below for reference. Can anyone please let me know what's wrong here?

App

<aura:application extends='force:slds'>
    <c:RakitaCompNineteen/>
</aura:application>

Component

<aura:component controller='RakitaCompNineteenMain'>
    <aura:attribute name='searchText' type='String' />
    <aura:attribute name='accounts' type='Account[]'/>
    <aura:attribute name='fields' type='List' default='["Name", "Industry", "Phone"]'/>
    <aura:attribute name='flag' type='Boolean'/>
    <lightning:card>
        <aura:set attribute='title'>
            <ui:inputText value='{!v.searchText}'/>
            <ui:button label='Search' press='{!c.invoke}'/>
        </aura:set>
        <aura:if isTrue='{!v.flag}'>
            <lightning:datatable keyField="id" data="{! v.accounts }" columns="{! v.fields }" />    
        </aura:if>
    </lightning:card>
</aura:component>

Controller

({
    invoke : function(component, event, helper) {
        var searchText = component.get('v.searchText');
        var abc = component.get('c.returnAccounts');
        abc.setParams({'searchStr':searchText});
        abc.setCallback(this, function(response){
            var state = response.getState();
            if(state === 'SUCCESS') {
                var returnVal = response.getReturnValue();
                component.set('v.accounts', returnVal);
            }
            component.set('v.flag', true);
            console.log(returnVal);
        });
        $A.enqueueAction(abc);
    }
})

Apex Class

public class RakitaCompNineteenMain {
    @AuraEnabled
    public static List<Account> returnAccounts(String searchStr) {
        String query = 'SELECT Id, Name, Industry, Phone From Account WHERE Name LIKE \'%' + searchStr + '%\'';
        List<Account> accts = Database.query(query);
        return accts;
    }
}

Screenshot of the result screen

enter image description here

Best Answer

Fields value which is passed columns attribute of data table could be the problem. Try creating your fields like below (label, fieldname and type). fieldname in lowercase.

enter image description here

Related Topic