• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Render form components with Ajax

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
George Schrader
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic