I am currently developing a search bar for Salesforce Lightning and i am having a bit of trouble with editing the class of dynamically created components using aura:iteration.

At the moment i use this code to identify the components, this works but this is of course not the lightning way with its codelocker. Snippet from my Helper, it compared two objects and if the result is false it will add alds-hide to the class.

       for(var i; i < originalObj.length; i++){
        var comparison = _.isEqual(originalObj[i], results[i]),
            Id = results[i].componentId;
        if(comparison !== true){
            var arr = document.getElementById(Id);
            arr.className += " slds-hide";

And below the code of my component.

            <div class="slds-grid slds-wrap slds-grid--pull-padded">
                <aura:iteration indexVar="index" items="{!v.searchResult}" var="comp">
                    <div id="{!comp.componentId}" data-id="{!comp.componentId}" class="slds-size--1-of-1 slds-medium-size--1-of-2 slds-large-size--1-of-3 slds-p-around--small">
                        <c:pageComponentCard component="{!comp}"/> 

Code is a bit sloppy but you hopefully get the point. How do i properly approach this?

The only way i know is through aura:id but you cannot add expressions to it, only static values.

  • I have tried to add aura:id to the div inside the iteration, i see all of the objects when i try to use var objA = component.find('nameOfTheId') and after attempting to select the first object (objA[0]) and trying to use $A.util.addClass(objA[0], 'slds-hide'); it leaves no result – Olsthoorn Apr 6 '17 at 14:14
  • 1
    why dont you use $A.createComponents() , looks like you are getting the array of searchResults, instead of iterating the values in the DOM/component end. Do the iteration in the controller and push the array of components to the $A.createComponents(), this way you would be able to pass expressions to aura:id as it would be passed as json value to the component array. Take a look at the Section creating nested components developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – StormBr34ker Apr 6 '17 at 18:08

Your Answer

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

Browse other questions tagged or ask your own question.