2

I have a problem to retrieve some values within some picklists. Here is my code of the lightning component:

<div class="c-container">
        <lightning:layout horizontalAlign="space">
            <lightning:layoutItem flexibility="auto" padding="around-small" size="6">
                <div id="pklProperty1" text="primo gruppo: " name="selectProperty" hidden="true">
                    <aura:iteration items="{!v.Property1}" var="Prodotto">

                        <aura:if isTrue="{!Prodotto.NE__Type__c == 'Enumerated'}">
                            <lightning:select aura:id="pklProductFamilies" label="{!Prodotto.Name}">
                                <aura:iteration items="{!Prodotto.NE__PropertyDomains__r}" var="pd">
                                    <option aura:id="propertiesDomain" value="{!pd.Id}">{!pd.Name}</option>
                                </aura:iteration>
                            </lightning:select>
                            <aura:set attribute="else">
                                <lightning:input label="{!Prodotto.Name}" name="{!Prodotto.Id}" />
                            </aura:set>
                        </aura:if>

                        <br/>
                    </aura:iteration>
                </div>
            </lightning:layoutItem>
        </lightning:layout>
    </div>

And here is the code in the controller where I try to retrieve the selected values:

verify : function(component, event, helper) {
        var items = component.find("propertiesDomain");
        console.log("items[0]: "+items[0].value + " items[1]: " + items[1].value);      
    }

But the returned values are undefined. I also try to change this line:

var items = component.find("propertiesDomain");

to this:

var items = component.find("propertiesDomain").get("v.value");

But I get this error:

This page has an error. You might just need to refresh it. Action failed: c:AnagraficaMateriali$controller$verify [component.find(...).get is not a function] Failing descriptor: {c:AnagraficaMateriali$controller$verify}

Could anybody know how to resolve? Thanks in advance.

2 Answers 2

6

To get the value selected you need to target the lightning:select and get his value, because if you target the options they will all have the same aura:id as it does not support expressions (so there's no way to create dynamic ones).

2
  • I suspected, so if another person confirm your claim, I accept your answer.
    – DarkSkull
    Jan 19, 2017 at 10:53
  • 1
    @darkskull Nordine is right. If you want the selected value of lightning:select, target it by cmp.find() or binding the select's value to an aura: attribute.
    – Praveen
    Jan 19, 2017 at 17:42
2

As @Nordine Hammache said you need to to target the lightning:select. As per your code for getting value ,You need to create sub component. I have worked out similar example

Component parent:

 <lightning:layout horizontalAlign="space">
            <lightning:layoutItem flexibility="auto" padding="around-small" size="6">
                <div id="pklProperty1" text="primo gruppo: " name="selectProperty">
                    <aura:iteration items="{!v.accounts}" var="Prodotto">
                               <c:testfor  value1="{!v.accounts}" />
                        <br/>
                    </aura:iteration>
                </div>
            </lightning:layoutItem>
        </lightning:layout>

Component child:

<aura:attribute name="value1" type="Account[]" />
<lightning:select aura:id="pklProductFamilies" label="ff" onchange="{!c.onSelectchange}">
 <aura:iteration items="{!v.value1}" var="pd">
       <option  value="{!pd.Id}">{!pd.Name}</option>                         
  </aura:iteration>               
  </lightning:select>

Component Controller

onSelectchange: function(component, event, helper) {
        console.log(component.find("pklProductFamilies").get("v.value"));
    }

You must log in to answer this question.

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