3
<aura:iteration items="{!v.dataList}" var="item">
    <li class="slds-dropdown__item slds-is-selected"
        role="presentation">
        <a href="javascript:void(0);"
           role="menuitemcheckbox" 
           aria-checked="true" 
           tabindex="0"> 
            <span class="slds-truncate"> 
                <lightning:icon iconName="utility:check" 
                                size="x-small" 
                                alternativeText="Selected" /> 
                {! item[v.labelField]}
            </span>
        </a>
    </li>
</aura:iteration>
  • what does v.labelField contain ? – Mohith Shrivastava Dec 13 '16 at 4:47
  • @MohithShrivastava v.labelField contains name of label field. Value of label field will be used as display name. – Saurabh Mishra Dec 13 '16 at 5:17
  • You will need to use maps I guess as workaround for this or dynamic component generation as suggested in my answer – Mohith Shrivastava Dec 13 '16 at 5:19
  • 1
    @MohithShrivastava I was thinking to transform my data before using like dataList.map(function(item) { /* map data to new array with static field name */ }) – Saurabh Mishra Dec 13 '16 at 5:24
1

Dynamic reference syntax is not yet supported in lightning .This is one of the limitations of the framework currently .

Please see this answer for workaround .

1

This is how I solved my problem, In my controller -

doInit : function(component, event, helper) {
    var list = component.get('v.list');
    var labelField = component.get('v.labelField');
    var valueField = component.get('v.valueField');
    if(Array.isArray(list)) {
        list = helper.transformDataList(list, labelField, valueField);
    } else {
        /* TODO */
    }
    component.set('v.dataList', list);
}

In my helper

transformDataList : function(list, nameField, valueField) {
    var tranformedList = list.map(function(item) {
        item.label = item[nameField];
        item.value = item[valueField];
        delete item[nameField];
        delete item[valueField]; 
        return item;
    })

    return transformedList;
},

and then .cmp file I am using {! item.label} instead of {!item[v.labelField]}

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