Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call a Action on onchange event in a Dropdown ?  RSS feed

 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using jsf and i want to call a action method on onchange event.
Can anyone give me idea how to do that...
If answer includes java scipt please give a detailed solution
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Onchange isn't an action, so you can't use an Action method for a change in a dropdown. However, you can use a ValueChangeListener.

Aside from the difference in what method you define, there's also a difference in what it's safe to do in a ValueChangeListener, since the phase that actually sets the updated properties in the backing bean hasn't been done yet. And if you want to sleep well at night, you won't try to update the properties yourself in the ValueChangeListener method.

You don't need JavaScript unless you want AJAX operation based on the selection changing. If you're looking at something like that, you might consider using an AJAX-aware tagset such as RichFaces, which allows you to bind an action listener to any of the JavaScript options and additionally spares you the trouble of writing and debugging AJAX JavaScript code yourself.
 
Wei Geng
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a tricky approach to realize your request.

Two steps:
1. Set the 'onchange' attribute of <h:selectOneMenu> to 'submit()'.
2. In the corresponding valueChangeListener method, invoke the action method you want to use and invoke renderResponse method. Like following:


The principle as following:
Once the selection changed, a submission will be performed. The valueChangeListener method will be invoked first. Then you can freely invoke your action as any simple, normal methods. At last invoking renderResponse method will let system directly render the response to browser, jumping over the following normal JSF phases. In the other word, other actions or actionListeners will not process any more.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!