0

I have aura:iteration which goes through my custom wrapper class (which contains List of sObjects), and show recordEditFrom for all of them.

My wrapper does not have an Id field.
However, it has a key field (Something like this Port 1, test product ) that is unique.

And I have a custom submit button.

Here is my code :

<aura:component>
<aura:iteration items="{!v.data}" var="item">
    <lightning:recordEditForm aura:id="editForm" recordId="{!item.Id}"
                              objectApiName="Custom_Object__c" 
                              onsubmit="{!c.onRecordSubmit}">
        <lightning:inputField fieldName="Custom_Field__c"/>
        <lightning:button variant="brand" label="Save" aura:Id="!{item.key}" type="submit" />
    </lightning:recordEditForm>
</aura:iteration>
</aura:component>

enter image description here

So my questions are:

Do we have a possibility in the onRecordSubmit check on which button from the aura:iteration list we clicked?

I tried aura:Id, however, it can not be generated here

Any help will be appreciated.

Thank you.

2
  • 1
    It is recommended to copy-paste code as text, instead of the image for the question. If someone will try to replicate the behavior of your code, would be much easier to copy that from a question, instead of writing from an image. Nov 1, 2021 at 11:05
  • 1
    @OleksandrBerehovskyi , Sorry, I agree with you, I have changed it. Nov 1, 2021 at 11:22

1 Answer 1

3

aura:id can't be set dynamically via an expression. It is mentioned in the Component IDs documentation.

aura:id doesn't support expressions. You can only assign literal string values to aura:id.

at the same time, it is not required to be unique across the component. You can set aura:id as a string along with setting a custom data-. Custom data- dataset attribute supports expressions. So it will be storing a unique key for submit button.

Howewer, data- dataset is not supported in lightning:recordEditForm. In this case a workaround whould be to use class attribute, as when you click submit button, source of the event is a form:

<aura:component>
    <aura:iteration items="{!v.data}" var="item">
        <lightning:recordEditForm
                aura:id="editForm"
                recordId="{!item.Id}"
                objectApiName="Custom_Object__c" 
                onsubmit="{!c.onRecordSubmit}"
                class="{!item.key}">
            <lightning:inputField fieldName="Custom_Field__c"/>
            <lightning:button variant="brand" label="Save" type="submit" />
        </lightning:recordEditForm>
    </aura:iteration>
</aura:component>

it is possible to get a source of the event in handler js controller:

onRecordSubmit: function(cmp, event, helper){
    const form = event.getSource();
    console.log('clicked form key: ', form.get('v.class'));
}
3
  • I have following error "The attribute "data-key" was not found on the COMPONENT markup://lightning:button" Nov 1, 2021 at 11:53
  • @VitaliiDehnerys please check my updated answer. Nov 1, 2021 at 12:48
  • thanks you . it works now. Nov 1, 2021 at 13:51

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.