File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Render form components with Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Render form components with Ajax" Watch "Render form components with Ajax" New topic

Render form components with Ajax

George Schrader

Joined: Oct 29, 2007
Posts: 20
I am creating a form dynamically based on questions from a database. Some of the questions need only be displayed based on a yes of one of the previous radio button questions (yes or no). In other words, if one of the answers are selected with yes, then how do I dynamically render other questions or not render them if it is selected as no. Since the ids are created dynmically, how can this be done?

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

That's some really nasty EL in there. Wow.

Your example didn't attempt any AJAX. I never tried out the new AJAX tag for JSF2, but I can tell you how the RichFaces AJAX would handle that.

First of all, however, I'd recommend using a dataTable and not a ui:repeat. You're basically attempting to program on what is properly supposed to be a page template. Or in other words, you're breaking the MVC paradigm. The code should be in the Model, not in the View (most Controllers in JSF are vendor-supplied, not user-written).

Secondly, along the same lines, you're writing code in EL. EL should be kept simple, not only so that people don't have to treasure-hunt between the Model and View for functionalities, but because EL can be a real bugger to debug.

Having said that, here's a simple RichFaces sample of how to make something like what you described, adhering to the about guidelines. It looks like you're using IceFaces, and it has similar capabilities.

I've made a few implied changes to the table dataModel to keep the EL simple and to keep the model a unit instead of a collection of loosely-associated data from different places. That is, after all, what models are for.

More importantly for your purposes, note that I've directed the UI to re-render the 2 response controls when the radio buttons are changed. I've also removed the "required" stuff, since the model is expected to have initialized the value, and thereafter the UI should ensure that one of the buttons is selected. Properly speaking, for a yes/no input a checkbox should be employed, but that's another matter.

An IDE is no substitute for an Intelligent Developer.
George Schrader

Joined: Oct 29, 2007
Posts: 20
Thank you very much for the response. Guess I should have stated that I am using Primefaces. I will see if there is a comparable tag and try this out...
I agree. Here's the link:
subject: Render form components with Ajax
jQuery in Action, 3rd edition