0

I have a form that has multiple fields in it. I want the values of those fields when I click on the "Apply" button. I am creating a filter, so I need the values and the field name in a string so I can change my SOQL Query accordingly. I don't know how to go about the controller code. Do I use component.find or something else? I'm not sure. All the fields are created dynamically. So I can't use aura:id for them because that causes issues apparently. Is there a way to go about it?

Component Code:

      <form aura:id ="filterForm">
            <div class="slds-grid slds-gutters">
                <div class="slds-col">
                    <aura:iteration items="{!v.filterList}" var="val">
                        <aura:if isTrue="{!val.type == 'PICKLIST'}">
                            {!val.pickListValue}
                            <lightning:select label="{!val.label}"><!--GET FROM CUSTOM LABEL-->
                                    <option text="None" value="None"/>
                                <aura:iteration items="{!val.pickListValue}" var="val1">
                                    <option text="{!val1.label}" value="{!val1.value}"/>
                                </aura:iteration>
                            </lightning:select>
                            <aura:set attribute="else">
                                <lightning:input label="{!val.label}" value = "{!v.collStrat}"/>
                            </aura:set>
                        </aura:if>
                    </aura:iteration>
                    <lightning:button class="slds-m-top_small" label="Cancel" />
                    <lightning:button onclick="{!c.handleSubmit}" class="slds-m-top_small" label="Apply" />
                                               </div>
                <div class="slds-col">
                </div>
            </div>
        </form>     

Controller Code:

handleSubmit: function(component, event, helper) {
        component.set('v.filterOpen',false);
        var mainForm = component.find("filterForm");
        console.log(mainForm);
       // var fields = event.getParam('fields');

    },

1 Answer 1

1

In theory, you should be able to use the values stored in the filterList attribute. You just need a few minor adjustments:

<lightning:select label="{!val.label}" value="{!val.selectedValue}"><!--GET FROM CUSTOM LABEL-->

And:

<lightning:input label="{!val.label}" value="{!val.selectedValue}"/>

By binding "value" to val, the iterator, those values will be stored in filterList, which you can then extract:

handleSubmit: function(component, event, helper) {
   var data = component.get("v.filterList").map(filter=>filter.selectedValue);

You can also use aura:id if you prefer:

<lightning:select label="{!val.label}" aura:id="inputs"><!--GET FROM CUSTOM LABEL-->

...

<lightning:input label="{!val.label}" aura:id="inputs" />

...

handleSubmit: function(component, event, helper) {
    var data = component.find("inputs").map(input=>input.get("v.value"));

These are the two most common techniques; you could also use event handlers to store the changes made somewhere, etc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .