You need to sign in to do that
Don't have an account?
Bobby Knezevic
<ui:inputSelectOption> label is "undefined" when retrieving using selectCmp.get("v.label")
I have a very simple <ui:inputSelectOption> element and a button that calls a controller action that examines the element's label and value. However, the label is "undefined" on the select element.
Controller code:
I've examined the selectCmp object in the debugger and the label property is, in fact, "undefined". What could be the problem here?
<ui:inputSelect aura:id="pricebooks"> <ui:inputSelectOption text="" label="--None--" /> <aura:iteration items="{!v.pricebooks}" var="pricebook"> <ui:inputSelectOption text="{!pricebook.Id}" label="{!pricebook.Name}" /> </aura:iteration> </ui:inputSelect> <button onclick="{!c.selectPricebook}">Select</button>
Controller code:
({ selectPricebook: function(component, event, helper) { var selectCmp = component.find("pricebooks"); var value = selectCmp.get("v.value"); console.log(value); // The expected result var label = selectCmp.get("v.label"); console.log(label); // undefined })
I've examined the selectCmp object in the debugger and the label property is, in fact, "undefined". What could be the problem here?
In my experience, I've used a select option as a means to store key-value pairs without having to populate a controller-side data structure. However, in this case I was forced to. My solution is a quick and dirty one.
Solution:
All Answers
In my experience, I've used a select option as a means to store key-value pairs without having to populate a controller-side data structure. However, in this case I was forced to. My solution is a quick and dirty one.
Solution:
I just ran into this same issue today, and I just wanted to share my solution in case someone else gets stuck with this. I will be using Bobby's code to reproduce my answer.
I found it a little less code expensive to create a new component to hold the inputSelect, and on my new component have an event fire when the inputSelecOption is clicked. That way you are guaranteed access to the label and text values from the inputSelectOption.
On the new component's controller I just grab the values I need, wrap them on an event, and fire it.
Leaving the initial code looking something like:
This will avoid another apex call, as well as avoiding the for loop on the Javascript controller.
This also removes the submit button, which might be an issue depending on what you want to do with the inputSelectOption information. We wouldn't want to create an object every time, someone clicks an option. But then it can just be a matter of creating an object in memory, firing it as an event, and then let the submit button deal with the event on time.
In the end the rule of thumb for this scenarios (a rule taught to me by my lead dev), is that: "If there's an issue with information sharing, it maybe that the component you are trying to use should not have that knowledge/responsibility. Delegate to another component and let it handle that responsibility/knowledge."