1

I have a component where I am building a lightning:recordEditForm using fields based on a different field sets that looks a little something like this:

<aura:component>

    <aura:attribute name="fieldApiNames" type="List" />
    <aura:attribute name="showSpinner" type="Boolean" default="true" />

    <aura:if isTrue="{!v.showSpinner}">
        <lightning:spinner alternativeText="Loading..." />
    </aura:if>

    <lightning:recordEditForm onload="{!c.handleLoad}" objectApiName="Account">
        <lightning:messages />

        <lightning:layout multipleRows="true">
            <aura:iteration items="{!v.fieldApiNames}" var="api">
                <lightning:layoutItem aura:id="formItems" size="6">
                    <div aura:id="top-div" />
                    <lightning:inputField aura:id="field" fieldName="{!api}" />
                    <div aura:id="bottom-div" />
                </lightning:layoutItem>
            </aura:iteration>
        </lightning:layout>
    </lightning:recordEditForm>

    <lightning:button title="Show Field Values" onclick="{!c.handleButton}" />

</aura:component>

When you call find() on an aura:id that occurs multiple times, you get back an array of everything with that aura:id. If I wanted to access an element within the body of one of the elements returned with my find() call, could I do it with the aura:id of said sub-element like below, in the handleButton function?

({
    handleLoad : function(cmp, evt, helper) {
        cmp.set( "v.showSpinner", false );
    },

    handleButton : function(cmp, evt, helper) {
        var first = cmp.get("v.fieldApiNames")[0];
        cmp.find("formItems").forEach( function(item) {
            alert( item.find("field").get("v.value") );       <== ?
        } );
    }
})
  • Something like cmp.find("formItems").find("field").get("v.value"); ? – Pranay Jaiswal Jun 19 '19 at 16:06
  • 1
    That particular line throws an error, because (.. I think ..) I need to use the loop on cmp.find("formItems") because there will be multiple elements with that aura:id. – griffinmelnick Jun 19 '19 at 16:12
1

It appears that this "nested find" (per se) is not possible. I opted to dynamically create each lightning:layoutItem and each, respectively, nested lightning:inputField in an init handler for my component. This allowed me to assign an aura:id as I created each individual component.

| improve this answer | |
0

you should use foreach for lightning:inputField aura id, then you get array of inputfields cmps then do foreach and get values like what i tried below

controller.js

 handleButton : function(cmp, evt, helper) {
        var first = cmp.get("v.fieldApiNames")[0];
        cmp.find("field").forEach( function(itemcmp) {
            alert( itemcmp.get("v.value") );       <== ?
        } );
    }  
| improve this answer | |
  • This would work in scenarios where the use case is exactly the same as my example. Unfortunately (and I apologize if this was poor practice), my example is not my exact use case. I was trying to simplify it to find a answer without too much of the nitty gritty. – griffinmelnick Jul 2 '19 at 3:29

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.