+ Start a Discussion
Tatiana CarpiucTatiana Carpiuc 

Nested iterations

Hi,

I have a nested aura:iteration like this:

<aura:iteration items="{!v.results}" var="res">                  
        <tr class="slds-hint-parent">
             <aura:iteration items="{!v.ColumnsNameArr}" var="colName">
                  <td>
                          <div class="slds-truncate" ><a href="javascript:void(0);">{!res[colName]}}</a></div>
                  </td>
               </aura:iteration>                    
            </tr>
   </aura:iteration>

How can I write correct {!res[colName]}? It is possible to do something like this in lightning?

Thank you.
Jarosław KołodziejczykJarosław Kołodziejczyk
Do you mean something like this: {!res +'['+colName+']'}?
Tatiana CarpiucTatiana Carpiuc
It doesn't work:(
Jarosław KołodziejczykJarosław Kołodziejczyk
Can you tell me what type of items are you iterating over? List<String> or something like this?
Tatiana CarpiucTatiana Carpiuc
For a specific column name it looks like this : {!res.Type} but for me Type is dynamic ..in my case colName
Tatiana CarpiucTatiana Carpiuc
<aura:attribute name="results" type="List" />
<aura:attribute name="ColumnsNameArr" type="String[]" />
Jarosław KołodziejczykJarosław Kołodziejczyk
I'm pretty sure there is no such thing as <aura:attribute  type="List" />, here are the bacis types you can get: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_attr_types_basic.htm , you can also make custom class or object type List like type="Account[]".
 
Jarosław KołodziejczykJarosław Kołodziejczyk
If you have a dynamic type, you would need to create custom class and make List of it's instances.
Tatiana CarpiucTatiana Carpiuc
<aura:attribute name="results" type="sObject[]" /> because the tabe depends on the object I send from design file, attribute type for results will be sObject[]
Tatiana CarpiucTatiana Carpiuc
Sorry, is Object, not sObject
Jarosław KołodziejczykJarosław Kołodziejczyk
Could you provide me with code on how you are creating these lists? both objects and ColumnsNameArr
Tatiana CarpiucTatiana Carpiuc
results attribute is created after a do a call to a method from apex controller to get all records for my object: 
action.setCallback(this, function(response) {
                var state =  response.getState();
    
                if (state === "SUCCESS") {
                    var resp = response.getReturnValue();
                    component.set('v.results', resp);
                }
            });

and I have data in resp.

ColumnsNameArr is different defined...I have in design file a text input....and I insert there the object fields I want to be displayed separated by comma. In js Controller I get the fields using split(',') and trim() and insert them in  ColumnsNameArr .

var columnsName = component.get('v.tableColumnsName'); //tableColumnsName is the attribut in both files (design and component)
columnsNameArr = columnsName.split(','); 
columnsNameArr = columnsNameArr.map(function(str) { return str.trim(); });
component.set('v.ColumnsNameArr', columnsNameArr);
Tatiana CarpiucTatiana Carpiuc
I saw that I get error when I try to save the file if I use {!res['Type']} so..I think there is only one way to write : {!res.Type}(which works)...writting {!res.colName} is ok when I save the file...but when I inspect the page there is no value..is blank. If I use {!colName} it displays all I need to iterate..so colName and res are not empty, they have what I expected to have...but {!res.colName} doesn't work...
Jarosław KołodziejczykJarosław Kołodziejczyk
If you want to use it like {!res.colName}, a Map<Object,List<String>> might be better, since you will be using object/res as a key, and list<string> as colNames.
Team Codekiat.comTeam Codekiat.com
this might be useful 
https://www.codengine.in/2019/07/nested-iteration-salesforce-lightning-aura-components.html