File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Multiple selectOneMenu with same value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Multiple selectOneMenu with same value" Watch "Multiple selectOneMenu with same value" New topic
Author

Multiple selectOneMenu with same value

Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

HI all,
I want to display two selectOneMenu components on my screen at two different locations. I have the same value(same variable in the bean) for both and the same listener method in the bean too. When i do this, i am getting following issues:

1. When i change the value in either of the selectOneMenu Component, the event Listener is triggered and the code in the bean is executed.
2. If it was serially first selectOneMenu that was changed, then both the selectOneMenu still carry the old value.
3. If it was the second selectOneMenu that was changed, then both the components carry the new value.

Any suggestions/comments?

Thanks.


When the compiler's not happy, ain't nobody happy.
Ad Rajesh
Greenhorn

Joined: Apr 25, 2006
Posts: 21
You can try giving different names in your ba cking bean. Any specific reason to have the same name for 2 variables.


SCJP 1.4
SCWCD 5.0
Ad Rajesh
Greenhorn

Joined: Apr 25, 2006
Posts: 21
Also you can't have the same 2 variable in the backing bean.
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Actually i am displaying a datatable wth pagination. I want to display a choice to the user to display as many records per page as he/she wants. So the drop down will contain values for recordsPerPage. This drop down i want to display at the top and the bottom of the table and user should be able to change the value in any one.
So basically there are no 2 variables in use, but just one, which is being acted upon by two selectOneMenus

Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16055
    
  21

I think I'd avoid duplicating the control, actually. But the alternative is to add JavaScript so that when you select on one menu, the value is propagated to the other menu via client-side code so there's no disagreement. Either that or use 2 different properties on the backing bean and try and determine which one is authoritative.

The JavaScript solution has the disadvantage that if the user disables JavaScript, you're right back where you started.


Customer surveys are for companies who didn't pay proper attention to begin with.
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Thanks Tim,
Yes, the idea of having two properties in the backing bean was what i thought of first too....so basically this means that we cannot have the same property for two different components on the page, right? even though it wont throw any exception or error, it will give abnormalities in behaviour....ill try the javascript solution once and then decide from the two!!

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16055
    
  21

Janardan Kelkar wrote:Thanks Tim,
Yes, the idea of having two properties in the backing bean was what i thought of first too....so basically this means that we cannot have the same property for two different components on the page, right? even though it wont throw any exception or error, it will give abnormalities in behaviour....ill try the javascript solution once and then decide from the two!!



Well, if by "abnormalities in behaviour", you mean "It doesn't do what I want it to do", probably so. I think the behaviour itself is within the JSF specs - and thus predictable, for what consolation that's worth.

I do display-only references to the same property multiple places on a page all the time. Classic example is a table where both the ID and caption columns have hyperlinks to detail editing of the row in question.
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

Hi Tim,

I Tried using two properties in the backing bean and bind them to the the two selectOneMenus, but it still did not work. Here's what i did in detail:
1. Declare two variables in the backing bean.
2.Have a method in the bean that is common to both the selectOneMenus and is invoked onchange.
3.Ensured that the two variables are set to the new value selected by the user.

Inspite of this i am facing the same issue!! So Finally did it the javascript way...As you mentioned, this is not foolproof, so would prefer a method that does this thing in the backend, any workarounds?


Thanks.
Mike Maurer
Greenhorn

Joined: Jul 13, 2010
Posts: 2
I have this same issue with a selectOneMenu component in a datatable column, bound to a single property in the backing bean, and using the same items list. The number of rows in the table is dynamic, but let's say there are 25, thus 25 instances of the selectOneMenu component. No matter which record I change the selectOneMenu value for, the value of the backing bean property will always end up being whatever the current value of LAST instance of the component in the table is set to. For example, if I select "value 2" on the the 4th row, and the 25th (last) row is still set to the default value of "value 1", then calling the getter for that property will always return "value 1". If I change that last instance to something else, like "value 3", then every other selectOneMenu instance will always return "value 3". I put a valueChangeListener on the component with the backing bean method doing a print of the old & new value, and print statements on the 'setter' for that property as well so I could see what was going on. The method called by the valueChangeListener does indeed show correct old and new values. And the setter method is called properly with the new value. But the same setter method is then called again, once for every row in the table, so the property value immediately gets set back to the value of the next call. That's why it always ends up with the value of the component in the LAST row of the table. I can't have unique bean properties and setter/getter methods for each row in the datatable. But surely there is a way to make this work. HELP!
Mike Maurer
Greenhorn

Joined: Jul 13, 2010
Posts: 2
Found the solution. Just a dumb oversight on my part. My selectOneMenu component inside the datatable was bound to a single property in the backing bean. I moved that property out of the backing bean and into the object ("Order" object) that my datatable was bound to instead. My datatable is bound to an array of these "Order" objects, and each row is its own unique instance of the Order object, and each contains its own unique instance of the property to hold the value of its selectOneMenu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple selectOneMenu with same value