h:selectManyListBox valueChangeListener is not calling - jsf-2

I am using Netbeans 6.9.1. I am using code in which i am simply using selectManyListBox and i want that when user selects all the values then my valueChangeListener is call. Here what i am doing
<h:selectManyListbox id="countryListBox" size="5" value="#{news.saarcCountries}"
onselect="form.submit();" valueChangeListener="#{news.changeAppearOnCountryPage}">
<f:selectItems value="#{news.saarcCountriesMap}"/>
<f:ajax render="countryPageExpiryCalender" />
</h:selectManyListbox>
<p:calendar id="countryPageExpiryCalender" value="#{news.countryPageExpiryDate}"
navigator="true" style="z-index: 1;" locale="en" mode="popup"
pattern="dd/MM/yyyy" showOn="button" readOnlyInputText="true"
disabled="#{!news.appearOnCountryPage}" />
I also tried this, instead of onselect i used onchnage like
onchange="submit()"
Here is my valueChangeListener
#ManagedBean(name = "news")
#ViewScoped
public class News {
private Map<String, String> saarcCountriesMap = new LinkedHashMap<String, String>();
private Set<String> saarcCountries = new TreeSet<String>();
...
public void changeAppearOnCountryPage(ValueChangeEvent vcEvent){
Iterator iter = saarcCountries.iterator();
while(iter.hasNext()) {
String name = (String)iter.next();
System.out.println(name);
}
} //end of changeAppearOnCountryPage
} //end of class News
I want that when user select all the values from the selectmanyListBox, then my valueChangeListener is call, and i check if i have the values in saarcCountries variable, then i set appearOncountryPage = true, so my calender is render. But my valueChangeListener is not calling. What i am doing wrong?
If i choose onselect, then i want to ask one thing this event will trigger whenever i select the value, of after i have selected all the values. I want that when user select all the values then my ValueChangeListener is call and the values that user selected will be in my saarcCountries variable. So i can check the values that user selected.
I think onchange() is not appropriate in my case. Please help. I am stuck because of it:(
Thanks

try it like this:
<h:selectManyListbox id="countryListBox" size="5" value="#{news.saarcCountries}">
<f:selectItems value="#{news.saarcCountriesMap}"/>
<f:ajax event="change" listener="#{news.changeAppearOnCountryPage}" render="countryPageExpiryCalender"/>
</h:selectManyListbox>
You don't need the form.submit(); since you can use the ajax...

Related

Getting selected value of a SelectOneMenu

I'm testing the component "SelectOneMenu" on a jsf page. I'm populating this component dinamically though my ManageBean (that will get all Animals from database).
I would like to know if is possible to see the user selected item of that "SelectOneMenu" (combobox), I'm trying with value="#{animalsManage.animalSelect}" but it is only called on the beginning of the page. Also, I'm using an inputText to see the value of the selected intem of the "SelectOneMenu".
What I'm doing wrong?
JSF:
<body>
<ui:component>
<h:form>
<h:outputText value="Select one Mets File" />
<h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
<f:selectItem itemLabel="Select..." noSelectionOption="true"/>
<f:selectItems value="#{animalsManage.allAnimals}" />
</h:selectOneMenu>
<h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
</h:form>
</ui:component>
</body>
ManageBean:
#ManagedBean
#ViewScoped
public class AnimalsManage implements Serializable {
#EJB
private AnimalsFacadeREST animalsFacadeREST;
private String animalSelected;
private List< SelectItem> selectAnimals;
public List<SelectItem> getAllAnimals() {
List<Animals> al = animalsFacadeREST.findAll();
selectAnimals = new ArrayList< SelectItem>();
int i = 0;
for (Animals animal: al) {
selectAnimals.add(new SelectItem(i, animal.getName()));
i++;
}
return selectAnimals;
}
public String getAnimalSelected() {
return animalSelected;
}
public void setAnimalSelected(String animalSelected) {
this.animalSelected = animalSelected;
}
}
There are many solutions to the presented problem. I present here two basic ideas.
Server-side solution. Simply attach <f:ajax> tag inside your <h:selectOneMenu> to update selected values and rerender user's choice, like in
<h:selectOneMenu id="combo" value="#{animalsManage.animalSelected}">
<f:selectItem itemLabel="Select..." noSelectionOption="true"/>
<f:selectItems value="#{animalsManage.allAnimals}" />
<f:ajax execute="combo" render="textbox" />
</h:selectOneMenu>
<h:inputText id="textbox" value="#{animalsManage.animalSelected }" />
If you like, you may also do some custom logic with selected element in ajax listener by specifying listener="#{animalsManage.performCustomAjaxLogic}" of <f:ajax> tag.
Client-side solution. Simply update element with id="textbox" on basic change event. So, if you use jQuery the solution will be
$('#combo').change(function() {
$('#textbox').val($('#combo').val());
});
Thought the client-side solution will bind only text value of your input component.

JSF CommandButton Show/Hide Issue

Im using a primefaces FieldSet component and have a button field specified inside the field set. I have a requirement, Where, when I click on the command button, a h:selectonemenu & an inputtext should be displayed and On further click, the h:selectOnemenu &inputtext should be hidden. Below is the code that I have written to show & hide. The show part works fine. But when I try to click on the button to hide, it does not work.
xhtml code
<h:panelGrid id="grid1" columns="3">
<p:fieldset id="GlobalAdjustment" legend="GlobalAdjustment" style="font-size:12px !important;width:30%" >
<h:commandButton id="globalAdjustID" image="#{review.imageUrl}" actionListener="#{review.showUpdateList}" />
</p:fieldset>
<h:selectOneMenu id="SelectID" value="#{review.reviewList}" rendered="#{demandReview.selectoneRenderer}">
<f:selectItems id="FilterSelectListID" value="#{review.reviewListIDs}" style="font-size:12px !important"></f:selectItems>
</h:selectOneMenu>
<h:inputText id="fieldUpdateID" required="false" rendered="#{review.inputTextRenderer}"></h:inputText>
</h:panelGrid>
ManagedBean ActionListener method
public void showUpdateList(ActionEvent event)
{
System.out.println("entering the Action Method:");
Map<String, Object> idMap = new HashMap<String, Object>() ;
idMap = event.getComponent().getAttributes();
String url = (String) idMap.get("image");
System.out.println("The url is :"+url);
if(url.equals("/images/add_data_button.png")){
//upbean.setImageUrl("/images/remove_data_button.png") ;
imageUrl ="/images/remove_data_button.png" ;
selectoneRenderer = true;
inputTextRenderer = true;
}else
{
imageUrl ="/images/add_data_button.png" ;
selectoneRenderer = false;
inputTextRenderer = false;
}
}
When I click the command button inside the fieldset for the first time, it works fine and displays the selectoneMenu & the textbox. Once further clicks, the ActionListener is not getting invoked. Please help me resolve the issue.
Thanks
You can try another approach. Use p:commandButton with an update attribute like this:
<h:panelGrid id="grid1" columns="3">
<p:fieldset id="GlobalAdjustment" legend="GlobalAdjustment" style="font-size:12px !important;width:30%" >
<p:commandButton id="globalAdjustID" image="#{review.imageUrl}" actionListener="#{review.showUpdateList}" update="grid1"/>
</p:fieldset>
<h:selectOneMenu id="SelectID" value="#{review.reviewList}" rendered="#{demandReview.selectoneRenderer}">
<f:selectItems id="FilterSelectListID" value="#{review.reviewListIDs}" style="font-size:12px !important"></f:selectItems>
</h:selectOneMenu>
<h:inputText id="fieldUpdateID" required="false" rendered="#{review.inputTextRenderer}"></h:inputText>
</h:panelGrid>
This way you'll use partial processing and the conditional renderization of the panel's inner components will be done through AJAX.
Also, it could be a good idea to check your server's console to see if there are any messages in the queue that were not displayed.
Does the requirement specify using the server side? For something like this, I would have thought a simple Javascript script would have been better suited.
For example, use the jQuery library to do something like:
<h:commandButton id="xyz" onclick="$('#elementId').toggle();"/>

JSF 2 check / uncheck list of h:selectBooleanCheckbox

I have a loop with a bunch of h:selectBooleanCheckboxs inside a ui:repeat and I want to be able to check / uncheck all of them with one click. Following this this is how I try to do it:
<h:selectBooleanCheckbox value="#{bean.selectAll}" valueChangeListener="#{bean.selectAllCustomers}">
<f:ajax execute="#form" render="entireLoop"/>
</h:selectBooleanCheckbox>
The loop:
<h:panelGroup id="entireLoop">
<ui:repeat var="customer" varStatus="status" value="#{bean.customers}">
<tr>
<td>
<h:selectBooleanCheckbox value="#{bean.customersMap[customer]}"/>
</td>
<td><h:commandLink value="#{customer.userID}" action="#{navBean.gotoEditCustomer(customer)}"/></td>
<td>#{bean.getContact(user)}</td>
<td>#{customer.companyName}</td>
<td>#{customer.email}</td>
<td>#{customer.phone}</td>
</tr>
</ui:repeat>
</h:panelGroup>
The code in the bean:
public void selectAllCustomers() {
Iterator<User> keys = customersMap.keySet().iterator();
while(keys.hasNext()) {
User user = keys.next();
customersMap.put(user, selectAll);
}
}
When I click to check the "select all" checkbox, I have two problems:
Nothing happens. Only when I click on somewhere else in the page the method is called. How can I get it to go directly?
The entire loop disappears from the page. Why? (I tried to get the ajax to render only the selectBooleanCheckbox by giving it an id, or a name, but I'm getting an error saying this is not a naming component.)
Instead of using valueChangeListener, your method will work if you use the listener attribute of the <f:ajax> tag. It would be something like this:
<h:selectBooleanCheckbox value="#{bean.selectAll}">
<f:ajax listener="#{bean.selectAllCustomers}" render="entireLoop"/>
</h:selectBooleanCheckbox>
Besides, you don't need to submit the whole form with execute="#form". You only need to submit selectAll property and re-render the entireLoop.
If you really want to use valueChangeListener, you must note that your method will be triggered before the new value is applied in the selectAll property. As a result, your entireLoop will not be updated until the next request. To use valueChangeListener, your listener method should look like this:
public void selectAllCustomers(ValueChangeEvent e) {
boolean newSelectAll = (Boolean) e.getNewValue();
Iterator<Users> keys = customersMap.keySet().iterator();
while(keys.hasNext()) {
Users user = keys.next();
customersMap.put(user, newSelectAll);
}
}

Is there any JSF Select Time Zone component out there?

I have been looking for this for a while and haven't found it. It is surprisingly complicated as shown in this old IceFaces tutorial.
What is needed is a UIInput component that will set a java.util.TimeZone property, allowing the user to select it from a map or a list on the screen. Before I dive in to write one for myself -- does anyone know of an available component that does this?
Use <h:selectOneMenu> to represent a dropdown list. Use <f:selectItems> to feed it with a E[], List<E>, SelectItem[] or List<SelectItem> as value.
Here's how it can look like at its simplest:
#ManagedBean
#ViewScoped
public class Bean implements Serializable {
private String timeZoneID; // +getter +setter
private String[] timeZoneIDs; // +getter only
#PostConstruct
public void init() {
timeZoneIDs = TimeZone.getAvailableIDs();
// You may want to store it in an application scoped bean instead.
}
public void submit() {
System.out.println("Selected time zone: " + TimeZone.getTimeZone(timeZoneID));
}
// ...
}
with this view:
<h:form>
<h:selectOneMenu value="#{bean.timeZoneID}" required="true">
<f:selectItem itemValue="#{null}" itemLabel="Select timezone..." />
<f:selectItems value="#{bean.timeZoneIDs}" />
</h:selectOneMenu>
<h:commandButton value="submit" action="#{bean.submit}" />
<h:messages/>
</h:form>
If you want to make it a fullworthy TimeZone property, you'd need to bring in a #FacesConverter(forClass=TimeZone.class) which should be pretty straightforward enough.

How to show required message of h:selectOneMenu with a default select option?

I am facing one issue with selectOneMenu. I need to display list of items in a drop down and it is a required field.
In that drop down the first value is "Select". If the user doesn't select any question, then I need to display an error message like "Select any question".
Can anyone give me solution?
Just set the item value of the first item to null. You shouldn't set it with the value of the label.
E.g.
<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>
You can also create a validator.
1)
<h:selectOneMenu id="giftValue" value="#{yourController.giftDO.giftValue}">
<f:selectItems value="#{yourController.giftDO.giftValueMap}" />
<f:validator validatorId="selectOneMenuValidator"/>
</h:selectOneMenu>
<h:message for="giftValue" errorStyle="color:red"/>
//where giftValue enum willcontail the word "Select"
2) Create the SelectOneMenuValidator.java
public class SelectOneMenuValidator implements Validator {
/* (non-Javadoc)
* #see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
*/
public void validate(FacesContext context, UIComponent arg1, Object value)
throws ValidatorException {
String giftValue = (String)value;
if(giftValue != null && giftValue.toUpperCase().equals("SELECT")){
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Please Select a question!");
message.setDetail("Please Select a question!");
context.addMessage("Please Select a question!", message);
throw new ValidatorException(message);
}
}
}
3) Add the validator to the faces-config.xml
<validator>
<validator-id>selectOneMenuValidator</validator-id>
<validator-class>net.roseindia.validations.SelectOneMenuValidator</validator-class>
</validator>

Resources