This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSF and the fly likes selectOneMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "selectOneMenu" Watch "selectOneMenu" New topic
Author

selectOneMenu

Jhonnathan Emilio Cardona Saineda
Ranch Hand

Joined: Jan 24, 2012
Posts: 38


what i need is that when a user choose a country, automaticaly can add a cities based in the country, but I don't find an attribute in the selectOneMenu as the action of the comandButton that triggers an event and so i can load the cities.

How can launch an event from the selectOneMenu when chosen country?

<h:selectOneMenu id="m">
<f:selectItems value="#{ejemploBB.myList1}" />
</h:selectOneMenu>


<h:selectOneMenu id="m1">
<f:selectItems value="#{ejemploBB.myList2}" />
</h:selectOneMenu>


thanks...


Sorry for my English, I'll improve
Bharat Kasodariya
Ranch Hand

Joined: Aug 19, 2011
Posts: 36
you can use 'valueChangeListener' attribute.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16098
    
  21

The reason you couldn't find anything is that until JSF 2, there wasn't any pretty way to do that.

JSF is built on HTTP and HTTP is not a continuous-conversation protocol. In other words, when you change control values, no actual network communications occur between client and server until an actual form submit is done. That is, the user clicks on a commandButton or commandLink.

So in order to make a menu selection submit a form, you'd need to add an "onchange" event attribute to the parent selection list. That onchange javascript would then "click the submit button".

But submitting an entire page is slow and ugly.

To make things more friendly, what's needed is AJAX. AJAX allows submitting just part of the page and in response updating only part(s) of the page. In particular, you'd want to update the child selection control based on the selection of the parent selection control.

There are 3 ways to do this. One is to use a set of custom extension tags with AJAX support in them such as RichFaces or IceFaces. That was the cleanest way to do things in JSF 1.x. In JSF2, the core tagset collection added an AJAX tag. The third way is the least-desirable way, since it involves writing your own JavaScript AJAX code, although use of a packaged solution such as jQuery can help.

I have found that the easiest way to get a dependent list control to follow changes to its parent selection is to write the "get" accessor for the child control's selectItems in a way where if there's no current list of selectitems, a list is created based on whatever the parent selection is. So what I do is make the value "set" method for the parent invalidate the child selectItem list, which causes a new, updated selectItem list to be built when the child control is re-rendered. It's also important when doing this to reset the current selection value for the child control, since the original selection value may not be referring to a valid value in the new selection list.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jhonnathan Emilio Cardona Saineda
Ranch Hand

Joined: Jan 24, 2012
Posts: 38

Tim¡¡¡¡¡¡¡ thank you very much, your explanation helped me
 
GeeCON Prague 2014
 
subject: selectOneMenu