1

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

4
  • what attribute type is conList ? iirc, aura:iteration will only work for arrays (in this case, it should be an array or list of object(s)) – glls Mar 22 '19 at 18:28
  • Sorry didn't indent correctly but it is a list, the iteration works correctly as i am able to set the name correctly of the lightning accordian. I believe that lightning data table takes a list / array as well and that is the issue – Bobbygllh Mar 22 '19 at 18:39
  • 1
    what does your response.getReturnValue() look like? – glls Mar 22 '19 at 18:41
  • Have you tried doing this without a server round trip. Just create the data in the component directly and see if that works? – pkamathk Jul 23 '19 at 15:02
-1

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'}]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.