You need to sign in to do that
Don't have an account?
Dynamic assignment on aura:id
I'm creating a Lightning component that uses aura:iterate to dynamically build a form, with a field for each item in a list object. The markup works great, but I don't know how to go back and get the values from the fields later -- to validate the fields and then to save the values.
I would normally use aura:id to component.find() the field, and then get("v.value"). BUT aura:id is undefined because it can't be dynamically assigned with a merge field. Here's how I was trying to assign aura:id in the iteration markup, but that aura:id assignment doesn't work.
So, any ideas on another attribute I can use to identify and call each field?
Is there a way to do a component.find() on a different attribute, like label, that can be set dynamically?
I'm using ui:inputNumber based on my use of it in the Lightning Component Trail on Trailhead (the one with Expenses and the Camping List), but I'm seeing an altenative -- lightning:input (Beta). I'm reading the docs on it, thinking maybe I can use the name attribute, but not sure if I can then component.find() it by name.
Any insight or ideas out there? Thanks.
I would normally use aura:id to component.find() the field, and then get("v.value"). BUT aura:id is undefined because it can't be dynamically assigned with a merge field. Here's how I was trying to assign aura:id in the iteration markup, but that aura:id assignment doesn't work.
<aura:component > <aura:attribute name="topic" type="Topic__c"/> <div class="slds-tile slds-hint-parent"> <div class="slds-form-element slds-is-required"> <div class="slds-form-element__control"> <ui:inputNumber aura:id="{!v.topic.Id}" label="{!v.topic.Name}" class="slds-input" labelClass="slds-form-element__label" value = "{!v.topicResult.score__c}" /> </div> </div> </div> </aura:component>I've double checked that {v.topic.Id} is valid (I can assign it to the label with no problem). I've tried using a simple string in a variable, also with no success. It appears aura:id has to be hard-coded.
So, any ideas on another attribute I can use to identify and call each field?
Is there a way to do a component.find() on a different attribute, like label, that can be set dynamically?
I'm using ui:inputNumber based on my use of it in the Lightning Component Trail on Trailhead (the one with Expenses and the Camping List), but I'm seeing an altenative -- lightning:input (Beta). I'm reading the docs on it, thinking maybe I can use the name attribute, but not sure if I can then component.find() it by name.
Any insight or ideas out there? Thanks.
Yes you are right that we cannot hard code aura:id. Instead of aura:iteration, an approach that you could consider is to dynamically create a number of components equal to the size of your object list upon rendering of your page, please see Dynamically Creating Components (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cb_dynamic_cmp_async.htm?search_text=createcomponent). This way, you can set make a loop on JS and assign the aura:id dynamically by appending a string plus the index value based on the size of the object list which you can later retrieve when you validate.