[SalesForce] Lightning Data Table and Aura Iteration

I am trying to render multiple data tables on a page (inside lightning accordians but thats irrelevent) using Aura iteration. i am specifically having trouble with the data attribute of the datatable. i set that equal to the iteration variable (which is an object so it shouldnt be an issue) but that doesn't seem to be accessing the data correctly. The Name of the accordian section is rendering correctly in the accordianSection and console.logs should conList is being set correctly, any ideas

Component

  <aura:attribute name="conList" type="List"/>
  <aura:attribute name="columns" type="List"/>
  <div class="slds-m-around_x-large">

    <lightning:accordion >
        <aura:iteration items="{!v.conList}" var="con">
            <lightning:accordionSection name="{!con.name}" label= 
            {!con.Name}">

                    <lightning:datatable
                        columns="{! v.columns }"
                        data="{!con}"
                        keyField="id"
                        hideCheckboxColumn="true"
                        onrowaction="{! c.handleRowAction }"
                        />

            </lightning:accordionSection>
        </aura:iteration>
    </lightning:accordion>

Controller

({    
   doInit: function(component,event,helper){

    component.set('v.columns', [

            {label: 'Premium', fieldName: 'Premium_c__c', type: 'number'}

        ]);

    var action = component.get('c.getContacts');
    action.setParams({ "rId": component.get("v.recordID") });
    action.setCallback(this, function(response){
        var state = response.getState();
        if(state === 'SUCCESS' && component.isValid()){
            //get contact list
           var intial= response.getReturnValue();
            component.set('v.conList', intial);
            console.log(component.get('v.conList'));
        }else{
            alert('ERROR...');
        }
    });
    $A.enqueueAction(action);
}
})

The con variable should just be an object with key:value pairs returned from my apex class and exactly what typically is used for the datatable so i am not positive why it doesnt seem to work

Best Answer

You can do this if the Array you assign to the table contains Arrays with the data you want per iteration. So what i mean is that each object in the array needs to be put in an array of its own.

[[{firstName : 'john',lastName : 'doe'}][{firstName : 'jane',lastName : 'doe'}]]
Related Topic