0

I'm sure I'm missing something obvious here but I'm trying to set all dynamically created iputselectoptions as selected when my lightning component is initialised but I'm finding that nothing is set when getting its "v.value"

Component snippet (opportunityStageNames is set in the initialisation)

<div aura:id="oppStagesDiv2">
    <ui:inputSelect multiple="true" class="multiple" aura:id="inputSelectStages">
        <aura:iteration items="{!v.opportunityStageNames}" var="stg">
            <ui:inputSelectOption text="{!stg}" value="true"/>
        </aura:iteration>
    </ui:inputSelect>
</div>

Javascript Component snippet (called from a button click)

var inputSelectStages = component.find("inputSelectStages");
console.log(inputSelectStages.get("v.value"));

The console shows the result as blank. However if I then alter my selections it returns the correct list so it's only not working on the initial setting.

I've tried various other coding options but cannot get it to work. What am I doing wrong?

Thanks in advance for any help offered.

Andee

  • Strictly speaking, your problem isn't in the code you've shown. How are you setting `opportunityStageNames? – sfdcfox Jun 21 '18 at 9:26
0

Worked it out but solution posted here in case it helps anyone coming here after me.

In the js controller initialistaion you need to dynamically set the select options rather than just the values. So the Component should look like :-

<aura:attribute access="private" name="opportunityStageNames" type="String[]"  description="List of possible stage names for an opportunity"/>
<div aura:id="oppStagesDiv2">
    <ui:inputSelect multiple="true" class="multiple" aura:id="inputSelectStages"/>
</div>

& in the init of the JS controller :-

// get the result from the apex controller call
picklistValues = response.getReturnValue();
component.set("v.opportunityStageNames", picklistValues);
var inputSelectStages = component.find("inputSelectStages");

// Loop through the results & create options for each.
var opts = [];
for (var i = 0; i < picklistValues.length; i++) {
    opts.push({class: "optionClass",label:picklistValues[i] ,value:picklistValues[i], selected:"true"});
}
inputSelectStages.set("v.options", opts);

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.