2
votes

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
Is that combobox really so heavily used that getComponent is not efficient enough?Panu Haaramo
While you are probably correct. But I have a fairly complex XPage a considerable amount of processing going on. I have been going through my code and trying to get rid of as many refeences to getComponent as I can. The result has been pretty significant. I would recommend staying a long way away from getComponent. I access the dataSource directly wherever I can and likewise avoid using the backend document.Bill F
I agree it's best to use the most efficient way and looks like you got good responses!Panu Haaramo

2 Answers

7
votes

I would bind the combobox to to a viewScope variable. Easy to access and use in other places. But there is alot of ways to get the value but this is the way I prefer.

3
votes

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.