I have a combo box with a list of values. The ComboBox is not bound to the datasource. I have an onClick event on the combo box and I need to know what value was selected. I can do a getComponent("comboBox1").getValue() but I know this is the least efficient way to access a control value. Is there a better way to access the value selected?
2 Answers
In the context of any server-side event handler, the JavaScript variable this
refers to the event handler itself. Because event handlers are actually components, and are considered "children" of whatever component they're associated with, the expression this.getParent()
will return a handle on the associated component.
So when you're just trying to get the value of the same component that fired the event (in your case, the onClick
event of a combo box):
this.getParent().getValue()
This doesn't suffer from the same inefficiency that getComponent()
does, because it doesn't have to search... it just navigates up one level in the component tree. If other components will benefit from knowing its value, however, you should also follow Fredrik's advice and bind the component to a viewScope
variable so you can just retrieve the value from memory instead of trying to locate a UI component from elsewhere in the page structure.
getComponent
is not efficient enough? – Panu Haaramo