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
Related
I have a selectOneMenu in a form with an ajax to update a separate component when it is triggered. This menu also uses validation. This first item in the menu is null and the validation makes sure that you can't submit the form if this item is null. However, I do want the user to be able to select the null item and that it set the value of that item in my backing bean to null. However the set is not happening when the user selects the null item because the validation failure occurs first and is throwing an exception preventing the value from being set to null in my backing bean.
Here is my code
<h:form>
...
<p:selectOneMenu value="#{myBean.container}">
<f:selectItem itemLabel="Select One" itemValue=#{null} />
<f:selectItems value="#{myBean.getContainerItems()}" />
<p:ajax event="change" update="containerNumSection" />
<f:validateRequired />
</p:selectOneMenu>
...
</h:form>
Is there any way that I can have the value in my backing bean set to null when the user selects the null item while keeping the validation?
I have found a somewhat dirty workaround using a custom validation method that is able to distinct cases where either the selectOneMany is changed or the submit button is pressed:
<h:form>
<p:selectOneMenu id="select" value="#{myBean.stringVal}"
validator="#{myBean.validateSelection}">
<f:selectItem itemLabel="nothing" itemValue="#{null}" />
<f:selectItem itemLabel="a" itemValue="a" />
<f:selectItem itemLabel="b" itemValue="b" />
<p:ajax update="#form" process="#this"/>
</p:selectOneMenu>
<p:commandButton value="submit" update="#form" process="#form">
<f:param name="selectionRequired" value="true"/>
</p:commandButton>
<h:message for="select" />
<h:outputText value="#{myBean.stringVal}" />
</h:form>
If the user presses the submit button, a request parameter named "selectionRequired" is set to "true". If this is the case, the validation method delegates to the javax.faces.validator.RequiredValidator.validate() method, else it simply does nothing.
MyBean.validateSelection:
public void validateSelection(FacesContext context, UIComponent component, Object value) {
String selectionRequired = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap()
.get("selectionRequired");
if (Boolean.valueOf(selectionRequired)) {
new RequiredValidator().validate(context, component, value);
}
}
I had to enable javax.faces.VALIDATE_EMPTY_FIELDS in order for this to work, else the validateSelection method was not invoked at all when #{null} is selected.
web.xml:
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
This is my current structure for my p:selectOneMenu:
<h:form id="groupSelectionForm">
<p:outputLabel value="Momentane Gruppe:" for="groupSelection" />
<p:selectOneMenu id="groupSelection" value="#{foodPlanManagementBean.selectedGroup}" style="width:150px">
<f:selectItem itemLabel="-" itemValue="#{null}"/>
<f:selectItems value="#{foodPlanManagementBean.getGroups()}" var="group" itemLabel="#{group.name}" itemValue="#{group}"/>
<p:ajax event="change"/>
</p:selectOneMenu>
</h:form>
This results in a checkbox containing a default value given by the single selectItem as well as a few generated options from the selectItems.
However, the setter for the given field "selectedGroup" is only triggering for the selectItem.
The selectItems do not seem to do anything when they are being clicked.
Any ideas?
try to define a listener in ajax component, ex:
<p:ajax id="seasonAjax" event="change" process="#this" listener="#{yourBean.yourMethod}" update="elementThatYouWantToUpdate" />
process = this to process selected element.
In selectItems don't use get method use directly list elements(put get/set in your bean) ex:
<f:selectItems value="#{yourBean.yourList}" var="" itemLabel="" itemValue="" />
If this doesn't work test if you need to use a converter, if selectedGroup is a complex object or pass directly identification of selectGroup( selectedGroup.id)
I hope it helps.
I have a problem when I use a p:tabView with dynamic="true", and there is a h:selectOneMenu on one tab, and on the other is a commandLink which is ajax="false". After clicking to the commandLink twice the value of the selectOneMenu is lost.
This problem does not occur when the tabView is dynamic="false".
The value of the h:inputText is not lost, but I see the following warning in the logfile:
org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils decodeUIInput WARNING: There should always be a submitted value for an input if it is rendered, its form issubmitted, and it was not originally rendered disabled or read-only. You cannot submit a form after disabling an input element via javascript. Consider setting read-only to true instead or resetting the disabled value back to false prior to form submission. Component : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /form/regional/region.xhtml][Class: javax.faces.component.html.HtmlBody,Id: j_id_5][Class: javax.faces.component.html.HtmlForm,Id: TestForm][Class: org.primefaces.component.tabview.TabView,Id: tabviewTest][Class: org.primefaces.component.tabview.Tab,Id: j_id_8][Class: javax.faces.component.html.HtmlInputText,Id: j_id_f]}
Here is the form:
<p:tabView dynamic="true" cache="true" id="tabviewTest">
<p:tab title="Tab 1">
<h:selectOneMenu value="#{Region.dropDownValue}" id="dropDown">
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
<f:selectItem itemLabel="4" itemValue="4"/>
</h:selectOneMenu>
<h:inputText value="#{Region.inputValue}" />
</p:tab>
<p:tab title="Tab 2">
<p:commandLink ajax="false"
id="link"
value="Test"
actionListener="#{Region.someActionMethod}" />
</p:tab>
</p:tabView>
And here the Bean:
public class Region {
private Integer dropDownValue = 3;
private String inputValue = "Test";
public void someActionMethod(ActionEvent ev) {
System.out.println("someActionMethod called");
}
public Integer getDropDownValue() {
return dropDownValue;
}
public void setDropDownValue(Integer dropDownValue) {
this.dropDownValue = dropDownValue;
}
public String getInputValue() {
return inputValue;
}
public void setInputValue(String inputValue) {
this.inputValue = inputValue;
}
}
My Environment: Primefaces 5.0/5.1.RC1, Myfaces 2.1/2.2, Tomact 7
Any ideas what could be wrong?
What scope does your ManagedBean have?
When you use a RequestScope you are not able to submit your selectOneMenu with an UICommand component like p:commandLink when you set the ajax attribute to false. The changes are lost in this case.
Here are two possibilities to fix your problem:
Attempt 1: Set your Bean ViewScoped:
In most cases this will work. If you must use special annotations to annotate your beans (like Apache DeltaSpike #ViewAccessScoped for example), try to separate your bean into View and Controller beans, annotating the View with just simple #ViewScope and keeping all the values in it.
Attempt 2: Remove ajax="false" from p:commandLink:
This will work if your use-case allows it. For example, downloading a file with PrimeFaces will require explicit declaration that the ajax is not to be used, so this solution will not be applicable.
add ajax listener
<h:selectOneMenu value="#{Region.dropDownValue}" id="dropDown">
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItem itemLabel="1" itemValue="1"/>
<f:selectItem itemLabel="2" itemValue="2"/>
<f:selectItem itemLabel="3" itemValue="3"/>
<f:selectItem itemLabel="4" itemValue="4"/>
<p:ajax event="change" update="#this"/>
</h:selectOneMenu>
My selectOneMenu isn't fireing an onchange event. Here is the code:
<p:selectOneMenu id="select_preset"
value="#{JobMgmtBean.presetGroupName}"
valueChangeListener="#{PFJobMgmtBean.PresetGroupChangeEvent}"
onchange="submit();">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
</p:selectOneMenu>
The selectOneMenu is nicely populated, and I can select different values. But I would expect that after I change a selection, page would be refreshed, i.e. backing bean (RequestScoped) would be recreated (onchange="submit();")? But nothing happens when selection in the selectOneMenu is changed.
Also, value change listener PresetGroupChangeEvent() is not called.
Did I go wrong somewhere?
I'm using Tomcat 7.0.25 + MyFaces 2.1.6 + PrimeFaces 3.2.
try to remove
onchange="submit();"
and to add
<p:ajax update="#this"/>
from your p:selectOneMenu when you using primefaces
<p:selectOneMenu id="select_preset"
value="#{JobMgmtBean.presetGroupName}"
valueChangeListener="#{PFJobMgmtBean.PresetGroupChangeEvent}">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
<p:ajax update="#this"/>
</p:selectOneMenu>
check your listener signature (starts from big "P" ?)
import javax.faces.event.ValueChangeEvent;
public void PresetGroupChangeEvent(ValueChangeEvent event) { }
why you are not adding update attribute using p:ajax? I think rerendering components more better solution than refreshing, also you can just rerender needed components. But if you still want to refresh page you can also use javascript:
<p:selectOneMenu id="select_preset" value="#{JobMgmtBean.presetGroupName}" onchange="window.location.reload();">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
</p:selectOneMenu>
and just for updating necessary components:
<p:selectOneMenu id="select_preset" value="#{JobMgmtBean.presetGroupName}">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
<p:ajax event="change" update="#form" />
</p:selectOneMenu>
this works for me
<p:selectOneMenu id="select_preset"
value="#{JobMgmtBean.presetGroupName}"
valueChangeListener="#{PFJobMgmtBean.PresetGroupChangeEvent}">
<f:selectItems value="#{JobMgmtBean.presetGroupList}"/>
<p:ajax process="select_preset" partialSubmit="true" event="valueChange" update="yourComponentName"/>
</p:selectOneMenu>
try to use ajax from primefaces and refresh only what you want to refresh
i'm making a JSF2.0 project using mojarra primefaces tomcat6.x.
I made a select list and when i select item of the list i want to redirect to the selected url. It can be an internal URL.
It's work but i want to know if it's possible to redirect in new window.
I have the following code JSF:
<h:form>
<h:selectOneMenu onchange="this.form.submit();" valueChangeListener="#{wagent.selectBusinessTravelLink}">
<f:selectItem itemLabel="#{msg['form.select.defaultValue']}" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{wagent.businessTravelLinks}" var="bLinkItem" itemLabel="#{bLinkItem.label}" itemValue="#{bLinkItem.id}" />
</h:selectOneMenu>
</h:form>
Java:
public void selectBusinessTravelLink(ValueChangeEvent event) {
// some stuff
FacesContext.getCurrentInstance().getExternalContext().redirect(targetUrl);
}
Use JavaScript's window.open() function rather than form.submit() during the change event.
Assuming that the select item values are fullworthy URL's, here's an example:
<h:selectOneMenu onchange="window.open(this.options[this.selectedIndex].value)">
Use onclick="this.form.target='_blank'" (or in your case in onchange), i.e.,
<h:form id="form">
<h:selectOneMenu onchange="this.form.target='_blank'; this.form.submit();" valueChangeListener="#{wagent.selectBusinessTravelLink}">
<f:selectItem itemLabel="#{msg['form.select.defaultValue']}" itemValue="" noSelectionOption="true"/>
<f:selectItems value="#{wagent.businessTravelLinks}" var="bLinkItem" itemLabel="#{bLinkItem.label}" itemValue="#{bLinkItem.id}" />
</h:selectOneMenu>
</h:form>
And of course, don't forget to fix the id attribute in <h:form id="form">