-
1. Re: How to dynamically build content in drop down boxes
niox.nikospara.yahoo.com Oct 18, 2010 4:38 AM (in response to richfaces_ahop)Hi,
A very simple example would be as follows.
XHTML:<h:selectOneMenu id="combo1" value="#{bean.propOne}"> <h:selectItems ... /> <a:support event="onchange" ajaxSingle="true" action="#{bean.combo1Changed}" reRender="combo2" /> </h:selectOneMenu> <h:selectOneMenu id="combo2" value="#{bean.propTwo}"> <h:selectItems value="#{bean.dynamicItems}" /> </h:selectOneMenu>
Java:
@Name("bean") public class MyBean { // properties propOne and propTwo with getters, setters ... private List<SelectItem> dynamicItems; public void combo1Changed() { dynamicItems = null; } public List<SelectItem> getDynamicItems() { if( dynamicItems == null ) { // create the list depending on the value of propOne ... } return dynamicItems; } }
Explanation: The items of combo #2 are held in the dynamicItems property of the bean. The getter of this property rebuilds the list if it is null, or returns the cached value. The <a:support> will trigger a server action when combo #1 changes. This action (combo1Changed) simply nullifies the dynamicItems to trigger the recreation of the list. The same could be achieved using <f:setPropertyActionListener> for this simple case.
The <a:support> is ajaxSingle="true" so that the server will only process the value of combo1. This keeps validation errors from other, unrelated fields to block the execution of this action. It is also better for the server, as it does not process the entire JSF component tree.
Finally the reRender="combo2" specifies that combo2 needs to be redrawn. The getDynamicItems() will be called and recreate the list.
Only the bare essentials are shown in this code, but you should get the idea.
Happy coding!
-
2. Re: How to dynamically build content in drop down boxes
richfaces_ahop Oct 18, 2010 9:06 PM (in response to richfaces_ahop)Nikos,
This is pretty clever and I am coding up the details as I am writing this email!! Thanks for the quick response! I have been doing a lot of nice things with Seam and this will be added to the bag of tricks...again thanks!
Antonio
-
3. Re: How to dynamically build content in drop down boxes
richfaces_ahop Oct 19, 2010 9:30 PM (in response to richfaces_ahop)I would like to continue this thread. I was able to very quickly get this solution coded up today while at work. Now, I have a scenario where I select a particular item in a selectItem drop down box. If I select Item1, I need for a message to be printed in a particular panel and certain dataTables to be reRendered...I was able to get this figured out today. Next, if I select Item2, then I need certain inputText and PickList boxes to be disabled. How would I go about doing this? Thanks in advance for your assistance.
Antonio