This question already has an answer here:
How to clear out and reuse p:dialog when adding new items
(1 answer)
Closed 6 years ago.
folks.
I'm stuck on the following problem. I have a modal dialog with two selectOneMenu components in it. When I close the dialog and open it again the values in selectoneMenu's are still selected. This is my menus:
<p:selectOneMenu id="fromCurrency"
value="#{dialog.exchangeRateManageContainer.currencyIdFrom}"
styleClass="ui-input-required"
required="true"
requiredMessage="#{msgs['validation.maintenance.exchangeRate.fromCurrency']}">
<f:selectItem
itemLabel="#{msgs['label.maintenance.selectCurrency']}"/>
<f:selectItems value="#{dialog.currencies}" var="currency"
itemLabel="#{currency.code}"
itemValue="#{currency.currencyId}"/>
</p:selectOneMenu>
<p:outputLabel for="toCurrency" value="#{msgs['label.maintenance.toCurrency']}" />
<p:selectOneMenu id="toCurrency"
value="#{dialog.exchangeRateManageContainer.currencyIdTo}"
styleClass="ui-input-required"
required="true"
requiredMessage="#{msgs['validation.maintenance.exchangeRate.toCurrency']}">
<f:selectItem itemLabel="#{msgs['label.maintenance.selectCurrency']}"/>
<f:selectItems value="#{dialog.currencies}" var="currency"
itemLabel="#{currency.code}"
itemValue="#{currency.currencyId}"/>
This is the cancel button:
<p:commandButton id="cancelButton"
value="#{msgs['label.button.cancel']}"
icon="ui-icon-cancel"
action="#{dialog.cancel()}"
immediate="true"
process="#this"
oncomplete="addExchangeRateDialog.hide();"/>
And this is the cancel method:
public void cancel() {
manageCurrenciesDialog = null;
}
Any help will be appreciated.
The solution for me was to add update atribut on the command buton which opens the dialog with the dialog id. And it works.
In order to reset selections, you need to set the values of your components to null.
Change your cancel method to:
public void cancel() {
manageCurrenciesDialog = null;
exchangeRateManageContainer.currencyIdFrom = null;
exchangeRateManageContainer.currencyIdTo = null;
}
Reset values of all components
I reseted values of all components by calling action to my backing bean method.
Inside of this method I assigned all values of components such as p:selectOneMenu and p:selectBooleanCheckbox to null.
It works perfectly.
Related
I want to reset a selectOneMenu with another selectOneMenu to the default value and I can't use ajax.
First menu:
<h:selectOneMenu value="#{bean.rel}" id="relSelection"
valueChangeListener="#{bean.onRelChange}" onchange="submit();"
immediate="true">
<f:selectItems value="#{bean.rel}" />
</h:selectOneMenu>
Menu I want to reset:
<h:selectOneMenu value="#{bean.domSelected}" id="domSelection"
valueChangeListener="#{bean.onDomChange}" onchange="submit()"
hideNoSelectionOption="true" immediate="true" >
<f:selectItem itemValue="#{null}" itemLabel="Select..."
noSelectionOption="true" />
<f:selectItems value="#{bean.domNames}" var="item"
itemLabel="#{item}" itemValue="#{item}"/>
</h:selectOneMenu>
If the first menu is selected after the second already was selected by the user I want to reset the second menu.
I expected that the second menu would display the noSelectionOption after I set its value to null from the onReleaseChange listener, but it keeps the selected value.
Listener:
public void onRelChange(ValueChangeEvent event) {
domSelected = null; //type String
rel = (String) event.getNewValue();
//som other stuff
}
I already tried the remoteCommand approach described here
by adding this:
<p:remoteCommand name="resetInputs" process="#this relSelection"
actionListener="#{bean.clearMenu}" immediate="true">
<p:resetInput target="domSelection" />
</p:remoteCommand>
In the first menu I called the remoteCommand via onChange="submit(); resetInputs()"
Any other Ideas or Ideas why this is not working?
Thanks!
Primefaces 6.1
JSF 2.2
GlassFish 4.1.1
I'm using JSF 2.2.8 and primefaces 6.0, and i have a selectCheckBoxMenu i want to retrieve the selected values in my bean.
The selectCheckboxMenu is filled from the database but when i select the attributes and I save nothing happens it does not call the save function
Here is my selectCheckBoxMenu
<p:outputLabel for="ressource" value="Ressource"/>
<h:panelGroup >
<p:selectCheckboxMenu id="ressource" label="Ressource" value="#{affectationBean.selectedRessource}" multiple="true">
<f:selectItems value="#{affectationBean.ressources}" var="r" itemLabel="#{r.nom}" itemValue="r.idt_ressource" />
</p:selectCheckboxMenu>
</h:panelGroup>
<p:commandButton icon="ui-icon-save" actionListener="#{affectationBean.save}" value="Save" update="#affectation" ajax="false" style="display:inline-block;margin-top:5px"/>
Here is the the declaration of the selectedRessource and the actionListener save
private Long [] selectedRessource;
// Getters setters and Construct
public void save(){
for(int i=0 ;i<selectedRessource.length;i++){
system.out.println("id ===> " + selectedRessource[i]);
}
My suggestion would be:
First make sure everything is inside the h:form tag.
don't need to multiple = true as this tag does not take this attribute
i tested with below modification and got the selected multiple value in my bean. The only difference is i am using same value for itemLabel and itemValue but in your case it is object. i am using primefaces 6 also and dont even need to change actionListner to action. It is working as it is.sample xhtml
sample ResourceBean.java
<p:outputLabel for="ressource" value="Ressource"/>
<h:panelGroup >
<p:selectCheckboxMenu id="ressource" label="Ressource" value="#{resourceBean.selectedRessource}">
<f:selectItems value="#{resourceBean.ressources}" var="r" itemLabel="#{r}" itemValue="#{r}" />
</p:selectCheckboxMenu>
</h:panelGroup>
<p:commandButton icon="ui-icon-save" actionListener="#{resourceBean.save}" value="Save" ajax="false" style="display:inline-block;margin-top:5px"/>
The problem is in your p:commandButton, you have 3 options
change your method:
public void save(ActionEvent actionEvent){...}
change your action listener value:
actionListener="#{affectationBean.save()}"
or change your button to use action
action="#{affectationBean.save}"
DISCLAIMER: This is a workaround. It is not intended to be a permanent solution but will allow you to use selectCheckboxMenu and keep working.
There is an issue with this component that prevents it from passing values to the backing bean upon submit.
For some reason the array that should contain the selected values gets cleared out upon submit. Therefore I made an extra array that I did not declare in the tag, and updated in on every change event. Then on submit the values were still there. See below:
BackingBean.java
private String[] sCodes;
private String[] sCodes2; //extra array, not in form.xhtml
public void updateCodes()
{
sCodes2 = sCodes; //keeps the values in the other array
}
public void doSend() throws IOException
{
log.trace("selected codes: {} selected codes2 length: {}", sCodes.length, sCodes2.length);
}
form.xhtml
<p:selectCheckboxMenu id="codeCtl" value="#{bean.SCodes}" label="Codes" filter="true" filterMatchMode="startsWith" panelStyle="width:250px">
<f:selectItems value="#{bean.menuCodes}" />
<p:ajax event="change" listener="#{bean.updateCodes()}" />
</p:selectCheckboxMenu>
<p:commandButton value="submit" actionListener="#{bean.doSend}" id="ctlSubmit" update="appform"/>
How to toggle disable in primefaces components selectOneMenu and calendar ?
Question is when user inputs value in calendar then selectOneMenu should be disabled. But when he removes value from calendar selectOneMenu should be enabled again.
I have tried with this solution but since those components dont have action attribute I couldnt figure it out.
I dont have validation button I wolud like to use some event.
You could use the ajax event to find out if the value has been changed.
<p:calendar ... >
<p:ajax event="change" listener="#{bean.dateChange}" update="selectOneMenuId"/>
</p:calendar>
Then use something like this in your bean:
private boolean disableSelectOneMenu = true;
public void dateChange(DateSelectEvent event) {
Date date = event.getDate();
if (date == null) {
disableSelectOneMenu = true;
} else {
disableSelectOneMenu = false;
}
}
and use the disableSelectOneMenu property in you disable tag of you selectOneMenu.
Both <p:calendar/> and <p:selectOneMenu> has disabled properties. Both component has ajax events, <p:calendar/> has dateSelect and <p:selectOneMenu/> has change.
So, you need to make a bean method which will return true or false according to which selection has been made and bind it to disabled properties and update these components when selection has been made.
For example JSF part:
<p:calendar id="calendar" value="#{bean.calendar}" disabled="#{bean.calendarDisabled}">
<p:ajax event="change" update="selector calendar" process="#this"/>
</p:calendar>
<p:selectOneMenu id="selector" disabled="#{bean.calendarDisabled != true}">
<p:ajax event="change" update="selector calendar" process="#this"/>
</p:selectOneMenu>
And bean part:
public boolean calendarDisabled(){
if(calendar != null){
return false;
}else{
//...do whatever you needs basing on your requirements
}
}
Also please take a look at Primefaces manual
This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 3 years ago.
I need some help. I'm developing for jsf and primefaces web application and I'm facing a problem when I'm selecting from a drop down list to get the selected value but I'm getting an empty string in the action.
This is my xhtml code for selectOneMenu tag
<p:selectOneMenu value="#{tanAllot.batchName}" id="batchName">
<f:selectItem itemLabel="Select Batch" itemValue="" />
<f:selectItems value="#{tanAllot.batchList}" />
<p:ajax event="change" listener="#{tanAllot.test}" />
</p:selectOneMenu>
this is the method I'm using in the action class
private String batchName;
public String getBatchName() {
return batchName;
}
public void setBatchName(String batchName) {
this.batchName = batchName;
}
public void test() {
System.out.println(batchName);
}
My problem is when I select a value from p:selectOneMenu tag the default method should invoke in the action and retrieve the value but I'm getting an empty string.
Can anyone help me to solve this problem?
Consider the nature of batchList. batchList must be List of Strings. Using itemValue attribute (in f:selectItem) can be helpful.
Check my example. It uses a list of provinces (instances of "Province" class). However I only need the "id" value which is a "Long"; if I wanted the whole picked province as a "Province object" I would need a "Converter". (Example works perfectly):
<p:selectOneMenu id="provinceField"
value="#{addAddressesMB.formAddress.provinceId}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{addAddressesMB.provinceList}" var="i"
itemLabel="#{i.description}" itemValue="#{i.id}" />
<p:ajax update=":formId:cityField"
listener="#{addAddressesMB.provinceChangeHandler}" />
</p:selectOneMenu>
And here comes the listener method:
public void provinceChangeHandler() {
//do whatever you want with formAddress.provinceId
System.out.println(this.formAddress.provinceId);
//In my case I filter the cities according to the selected provinceId
// After that I update the cities dropdown(cityField) in the view.
}
Check your code and feel free to ask. Good luck.
Maybe someone could clear this up for me.
I have an h:selectOneMenu that should do something as soon as a user selects something from the list and then rerender a link based on the results.
<h:selectOneMenu value="#{backingBean.itemSelected}" valueChangeListener="#{backingBean.methodThatDoesStuff}" onchange="submit()" >
<s:selectItems var="_items" value="#{backingBean.itemList}"
label="#{_items.Name}" />
<s:convertEntity />
</h:selectOneMenu>
My assumption was that when the user selected an item it would populate a field in the backing bean and then run the methodThatDoesStuff.
But that isn't happening. backingBean.itemSelected isn't set until AFTER the method is called. I can get the event and use it directly in the method:
public void methodThatDoesStuff(ValueChangeEvent event){
Item item = (Item)event.getNewValue();
.... do stuff, set a flag to display link later
}
This works.
But this leaves my aComponent set to the previous value, so the 1st time it will be null, then will change the settings based on the last itemSelected(so I'm always one behind:
public void methodThatDoesStuff(){
Item item = this.itemSelected;
.... do stuff, set a flag to display link later
}
I've tried:
<h:selectOneMenu value="#{backingBean.itemSelected}" >
<s:selectItems var="_items" value="#{backingBean.itemList}" label="#{_items.Name}" />
<a:support event="onchange" ajaxSingle="true" reRender="aComponent" action="#{backingBean.methodThatDoesStuff}" />
<s:convertEntity />
</h:selectOneMenu>
But that doesn't work whether I use ValueChangeEvent or not.
Is using the event object the only way to get the currently selected item?
This is JSF 1.2 and Seam 2
Set attribute immediate="true" on your selectOneMenu.