I am developing a jsf,richfaces application.where i want to populate the values of second select menu on the basis of selecting the choice from the first one .How i can achieve this.
<h:outputText id="section1" value="Section" />
<h:selectOneMenu id="section2" value="#{content.sectionName}" >
<f:selectItems value="#{content.sections}" />
</h:selectOneMenu>
what exactly i want is:
I have two tables one for category and one for section.
If a user choose a category from drop down menu then the other drop down menu for section should have the values only for selected category.
Please he
As per your question history you're using Ajax4jsf/RichFaces. Better use <a4j:support> instead of valueChangeListener.
<h:selectOneMenu id="section2" value="#{content.sectionName}" >
<f:selectItems value="#{content.sections}" />
<a4j:support event="onchange" action="#{content.changeSection}" reRender="otherMenuId" />
</h:selectOneMenu>
In the changeSection() method you need to populate the select items for the 2nd menu. The otherMenuId must refer to id of the other <h:selectOneMenu> in the same <a4j:region>.
You have to define a valueChangeListener for your first selectOneMenu:
<h:selectOneMenu id="select1" valueChangeListener="#{myBean.updateSections}">
<f:selectItems value="#{myBean.sections1}" />
</h:selectOneMenu>
Inside MyBean#updateSections you have to modify the list of SelectItems the second selectOneMenu has to show according to the selection you made.
Furthermore you have to submit your form or rerender section2 to display the updated values in section2.
Related
My requirement is like this. I have a selectMenu with some values (Examples: Engineering, Medicine, Law etc..,) . Suppose if I select Engineering in the drop down, I want another dropdown menu created dynamically which has values related to Engineering (Example: Electronics, Computers, Electricals etc..,). How do I achieve this in JSF 2.0 ?
You need to perform an ajax request when first h:selectOneMenu's selection change. This request will update the selectable items in the second h:selectOneMenu. After the ajax request, you must render the second h:selectOneMenu again, with the updated values.
Page:
<h:selectOneMenu value="#{bean.selectedSubject}">
<f:ajax listener="#{bean.changeSubject}" render="speciality_selection" />
<f:selectItems value="#{bean.subject}" />
</h:selectOneMenu>
<h:selectOneMenu id="speciality_selection" value="#{bean.selectedSpeciality}">
<f:selectItems value="#{bean.subjectSpecialities}" />
</h:selectOneMenu>
Managed bean:
public void changeSubject(){
//Loads the specialities depending on the selected subject
subjectSpecialities = loadSpecialitiesForSubject(selectedSubject);
}
I don't know how to implement onclick event on a combobox, my boss want me to do is once the user click a value in the combobox it automatically search and display all the value of the selected/click item. First question is it possible to have an onclick event on a JSF page without using any javascript/jquery? Right now I'm using ADF for designing the interface. Second question how can I implements this onclick event on my combobox?
There are a couple of ways to achieve this:
Use a valueChangeListener and execute your query when it fires.
Set autoSubmit="true" and when the bound value changes, execute your query.
Only selecting a value in a dropdown won't submit your form. This is not about JSF but HTML .. so without any JS i think it's not possible.
I do not know anything about ADF in special but in plain JSF you just have to add an ajax-event to your dropdown (e.g. in primefaces)
<h:form id="id1">
<p:selectOneMenu id="id2" value="#{myBean.value}"
immediate="true" editable="true" >
<f:ajax execute="#this" listener="#{myBean.doSomeAction}" />
<f:converter converterId="myConverter" />
<f:selectItems value="#{myBean.availableOptions}" />
</p:selectOneMenu>
</h:form>
I was wondering what would be the best or easiest way to allow a user to select nothing in a selectOneMenu.
My example: I have a list of registered users and the administrator should be able to filter the list of displayed users by some criterias. These criterias, like the usertype (employee, customer, ...) can be chosen by selectOneMenus, like this:
<h:selectOneMenu value="#{myBean.selectedUsertype}" converter="#{usertypeConverter}">
<f:selectItems value={myBean.usertypes}" />
</h:selectOneMenu>
When the corresponding selectOneMenu is being backed by a list of POJOs using a converter, how can I add an item to the list indicating that the user didn't choose any specific item? Currently I have a dummy usertype object displaying the label "---", but this is causing several problems in other areas of my application and I don't think that this is the best solution.
Just explicitly set the select item value to null.
<h:selectOneMenu value="#{bean.selectedItem}">
<f:selectItem itemValue="#{null}" itemLabel="--select--" />
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>
No, an empty string like itemValue="" is not sufficient. It really has to be null. Otherwise you run into trouble as described in this Q&A: Using a "Please select" f:selectItem with null/empty value inside a p:selectOneMenu.
If the item happen to be required="true" and you're using JSF 2.x, then you could add noSelectionOption="true" to the select item. This is only useful if you also set hideNoSelectionOption="true" on the selection component. It will then hide the empty option in the list once the enduser selects a different item, hereby making it impossible to re-select the empty option.
<h:selectOneMenu value="#{bean.selectedItem}" hideNoSelectionOption="true">
<f:selectItem itemValue="#{null}" itemLabel="--select--" noSelectionOption="true" />
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>
See also page 114 of The Definitive Guide to JSF under section "SelectItem tags":
Note that a select item with value of #{null} can be used to present the default selection in case the bean property associated with selection component's value attribute is null. If you have consulted the tag documentation of <f:selectItem>, then you'll perhaps have noticed the noSelectionOption attribute and have thought that it was intended to represent a "no selection option". Actually, this isn't true. Many starters indeed think so, as you can see in many forums, Q&A sites, and poor-quality tutorials on the Internet. In spite of the misleading attribute name, it does not represent a "no selection option".
A better attribute name would have been hideWhenOtherOptionIsSelected, and even then it works only when the parent selection component has explicitly a hideNoSelectionOption="true" attribute set. So, hideWhenOtherOptionIsSelectedAndHideNoSelectionOptionIsTrue would ultimately have been the most self-explanatory attribute name. Unfortunately, this wasn't very well thought out when the noSelectionOption was implemented in JSF 1.2. Requiring two attributes for this attribute to function shouldn't have been necessary. The primary purpose of this attribute pair is to prevent the web site user from being able to re-select the "no selection option" when the component has already a non-null value selected. For example, by having it prepared in a #PostConstruct method, or by re-rendering the component after a form submit with a non-null value.
Copyright disclaimer: book is written by me.
Add a single selectItem with null value;
<h:selectOneMenu value="#{bean.question}" required="true" requiredMessage="Please select a question">
<f:selectItem itemValue="#{null}" itemLabel="Select" />
<f:selectItems value="#{bean.questions}" />
</h:selectOneMenu>
We can in primefaces (when we have to use <p:selectOneMenu... from some reason like using <p:ajax..) add the following empty item:
<f:selectItem itemValue="#{null}" itemLabel="--select--" itemDisabled="#{Mybean.value ne null}" />
Note: In such case we don't need the following two tags:
hideNoSelectionOption="true"
and
noSelectionOption="true"
I'm attempting to put a few drop down menus inside of an a4j:repeat. The values for the second drop down are dependent on the value selected in the first. Below is the code I am attempting to use, but it passes a blank parameter:
<a4j:repeat id="localRepeat" var="local" value="#{InstanceController.instance.locations}" rowKeyVar="row">
<h:panelGrid columns="2">
<h:outputLabel value="Theater:" />
<h:selectOneMenu id="theater" converter="#{TheaterConverter}" value="#{local.theater}">
<f:selectItems id="theaters" value="#{InstanceController.allTheaters}" />
<a4j:support event="onchange" action="#{InstanceController.getAllCountriesInTheater}" reRender="country" >
<f:param name="theater" value="#{local.theater.id}"/>
</a4j:support>
</h:selectOneMenu>
<h:outputLabel value="Country:" />
<h:selectOneMenu immediate="true" id="country" converter="#{CountryConverter}" value="#{local.country}">
<f:selectItems value="#{InstanceController.allCountriesInTheater}" />
<a4j:support event="onchange" reRender="state" />
</h:selectOneMenu>
</h:panelGrid>
<rich:spacer height="10px" />
</a4j:repeat>
If I change the f:param to send "1" instead of "#{local.theater.id}" it works as expected.
Is there a way to get the selected value of the first drop down and send it as a parameter?
This reason this doesn't work is that when the f:param tag is rendered, the current value of local.theater.id is already assigned to the parameter. So the theater param will contain the id of the theater that was selected when the page was rendered - probably null because no theater has been selected yet.
What you are trying to do is far more easy:
Just remove the f:param and use the property directly. When the a4j:support tag triggers because the selectbox value was changed, jsf will validate your form tags and assign the appropriate model values. So, when the getAllCountriesInTheater action gets executed, the local.theater property will already contain the selected theater.
Depending on how your backing beans are designed, you will probably need a parameter to identify the location for which the selectbox was changed, so the getAllCountriesInTheater action will know in which of the locations to look for the selected theater.
I need to populate a drop down based on the selected value in another drop down. I can use AJAX requests to do that but I am searching for a solution using JSF only. The values to populate the drop down need to come from a table every time user changes his selection.
Thanks
You didn't specify the version, so I'll assume JSF 1.2. The <a4j:support> tag is part of RichFaces:
<h:selectOneMenu id="firstDropDown" value="#{bean.firstDropDownSelection}">
<f:selectItems value="#{bean.items}" />
<a4j:support event="onchange" reRender="secondDropDown"
immediate="true" action="#{bean.fetchItems2}" />
</h:selectOneMenu>
<h:selectOneMenu id="secondDropDown" value="#{bean.secondDropDownSelection}">
<f:selectItems value="#{bean.items2}" />
</h:selectOneMenu>
And in the method bean.fetchItems2 you load your collection items2 with the appropriate items.
What happens, is when the value of the first drop down changes, the second drop down is rerendered and its value is fetched from the server again.