I want to reset <p:selectOneMenu> in primeface. I used type="reset" this can reset the text fields only not a selectonemenu. My code
<p:panel id="Applyleave_panel" >
<p:selectOneMenu id="leavetype" value="#{requestbean.leavetype}" required="true" style="width:50%;">
<f:selectItem itemLabel="Select type" itemValue="" />
<f:selectItems value="#{requestbean.leave_type}" />
</p:selectOneMenu>
</panel>
<p:commandButton value="Reset" type="reset"/>
You can use p:resetInput given that your component is inside a form.
<p:commandButton value="Reset" update=":form" immediate="true">
<p:resetInput target=":form" />
</p:commandButton>
EDIT: You can also target the p:panel component as well.
<p:commandButton value="Reset" update=":Applyleave_panel" immediate="true">
<p:resetInput target=":Applyleave_panel" />
</p:commandButton>
I use the below
<p:commandButton id="resetSearchCir" type="reset"
value="#{button.reset}" immediate="true">
<f:ajax event="click"
listener="#{searchBean.resetActionListener}" render="#form" />
</p:commandButton>
public void resetActionListener(AjaxBehaviorEvent event) {
LOG.info("Reset button clicked...");
setResetClicked(true);
// re-initialise your form field objects which you want to reset
logWarn("All values have been reset. Please enter the new values to search again.");
}
Related
I'm working on a project with JSF + Primefaces 6.0.
On this particular page there is something wrong which I don't know how to fix.
When #selected-operator or #selected-site values are changed the form#operatorsForm is updated correctly via AJAX and the results are displayed in the datatable.
The problem is when I click on the commandButton that is loaded within the datatable the modal dialog "confirmWV" is not showing.
On my other pages I have the same code for modals but buttons there are not loaded via AJAX so this makes me think it is AJAX related issue.
When debugging, clicking on the button is not calling the method confirmOperatorsRightsAction().
Thank you for your help!
page.xhtml
<h:form id="operatorForm">
<p:selectOneMenu id="selected-operator"
value="#{operatorController.selectedOperator}">
<f:selectItem itemValue="" itemLabel="Choose operator" />
<f:selectItems value="#{operatorBean.getOperatorsList()}"
var="operator" itemLabel="#{operator.username}"
itemValue="#{operator.idPk}" />
<f:ajax execute="selected-operator" />
</p:selectOneMenu>
<p:selectOneMenu id="selected-site"
value="#{operatorBean.selectedSite}">
<f:selectItem itemValue="" itemLabel="Choose site" />
<f:selectItems value="#{operatorBean.getSites()}"
var="site" itemLabel="#{site.strname}" itemValue="#{site.idPk}" />
<f:ajax execute="selected-site" />
</p:selectOneMenu>
</h:form>
<h:form id="operatorsForm">
<p:dataTable var="operatorSite"
value="#{operatorBean.getOperatorSites()}"
id="operators-list">
<p:column headerText="#{msg['item.Username']}">
#{operatorSite.operator.username}
</p:column>
<p:column headerText="#{msg['item_SiteName']}">
#{operatorSite.site.strname}
</p:column>
<p:column headerText="">
<p:commandButton
actionListener="#{operatorBean.confirmOperatorsRightsAction(operatorSite)}"
value="#{msg['item_Delete']}"
icon="fa fa-trash">
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<p:dialog widgetVar="confirmWV" modal="true"
header="#{msg['modal.confirm.title']}" height="150" width="450">
<h:form id="confirmOperatorForm">
<my:confirm />
</h:form>
</p:dialog>
OperatorBean.java
public void confirmOperatorsRightsAction(OperatorSite os) {
//some code here
RequestContext.getCurrentInstance().update("confirmOperatorForm");
RequestContext.getCurrentInstance().execute("PF('confirmWV').show();");
}
In my example, the bean paisMB opens a dialog with a datatable for the user search a object. In the bean portoMB I have the controller of a crud that uses the object searched in the paisMB.
The code below works, but it does not update the inputText, the update of the <p:ajax> execute before the onsuccess. How can I update the inputText?
<p:inputText id="pais" label="País" value="#{paisMB.paisSelecionado.nome}" required="true" readonly="true" />
<p:commandButton icon="fa fa-search" actionListener="#{paisMB.openPaisDialog}" >
<p:ajax event="dialogReturn" update="panel"
listener="#{paisMB.onPaisChosenFromDialog}"
onerror="#{portoMB.portoSelecionado.setPais(null)}"
onsuccess="#{portoMB.portoSelecionado.setPais(paisMB.paisSelecionado)}" />
</p:commandButton>
BalusC showed me an answer that fixes my problem.
The following is the modified code:
<p:commandButton icon="fa fa-search" actionListener="#{paisMB.openPaisDialog}" >
<p:ajax event="dialogReturn" listener="#{paisMB.onPaisChosenFromDialog}"
onerror="setPaisNull()" onsuccess="setPaisObj()" />
<p:remoteCommand name="setPaisObj" update="panel" action="#{portoMB.portoSelecionado.setPais(paisMB.paisSelecionado)}" />
<p:remoteCommand name="setPaisNull" update="panel" action="#{portoMB.portoSelecionado.setPais(null)}" />
</p:commandButton>
I'm having a trouble with getting rid of required message.
I have a form in which I have a few fields and a button.
When I press a button there is validation that checks if required fields where filled with values if not then required message is displayed for invalid value/component.
Now I want to select a value from selectOneMenu or type something into inputText and when I do that I want the required message to dissapear without need to press the button again.
How would you do that?
I've tried to remove message with sth like this, but it doesn't seems to work:
Iterator<FacesMessage> msgIterator = FacesContext.getCurrentInstance().getMessages();
while (msgIterator.hasNext())
{
FacesMessage facesMessage = msgIterator.next();
msgIterator.remove();
}
Could you help me with that?
Here is example code:
<h:form id="mainForm">
<h:selectOneMenu required="true" id="dictionaryValueId" value="#{SomeBean.dictionarySelectedValue}">
<f:selectItem itemValue="#{null}" itemLabel="#{i18n['view.choose']}" />
<f:selectItems value="#{SomeBeanBean.dictionaryValuesMap}" var="element"
itemLabel="#{element.descripption}" itemValue="#{element.key}" />
<f:ajax event="change" execute="#this msgId" render="msgId dictionaryValueId"/>
</h:selectOneMenu>
<h:message id="msgId" style="display:none;" for="dictionaryValueId" />
...
<h:commandButton value="#{i18n['button.forward.name']}"
actionListener="#{SomeBean.forward}" >
<p:ajax process="#form" update="mainForm"/>
</h:commandButton>
I am not sure, but is not there a problem with
style="display:none;"
for
<h:message id="msgId"/>
You can wrap your message with <h:panelGroup/> and render by this panelGroup Id, this Id will be always present on your form.
<h:form id="mainForm">
<h:selectOneMenu required="true" id="dictionaryValueId" value="#{SomeBean.dictionarySelectedValue}">
<f:selectItem itemValue="#{null}" itemLabel="#{i18n['view.choose']}" />
<f:selectItems value="#{SomeBeanBean.dictionaryValuesMap}" var="element" itemLabel="#{element.descripption}" itemValue="#{element.key}" />
<f:ajax event="change" execute="#this" render="messageBundle1 dictionaryValueId"/>
</h:selectOneMenu>
<h:panelGroup id="messageBundle1">
<h:message id="msgId" style="display:none;" for="dictionaryValueId" />
</h:panelGroup>
<h:commandButton value="#{i18n['button.forward.name']}"
actionListener="#{SomeBean.forward}" >
<p:ajax process="#form" update="mainForm"/>
</h:commandButton>
</h:form>
I am using this dialog to display a Create form
<p:dialog id="dialogoGestion" header="Asignar nueva gestión" widgetVar="dlg1" >
<p:ajax event="close" update="dialogoGestion"/>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Cliente: "/>
<h:outputText value="#{miscMB.factura.cliente.nombre}"/>
<h:outputText value="Factura: "/>
<h:outputText value="#{miscMB.factura}"/>
<h:outputLabel value="#{bundle.CreateGestionLabel_descripcion}" for="descripcion" />
<p:inputText id="descripcion" value="#{miscMB.nuevaGestion.descripcion}" title="#{bundle.CreateGestionTitle_descripcion}" />
<h:outputLabel value="#{bundle.CreateGestionLabel_fechaLimiteGestion}" for="fechaLimiteGestion" />
<p:calendar locale="es" pattern="dd/MM/yyyy" id="fechaLimiteGestion" value="#{miscMB.nuevaGestion.fechaLimiteGestion}" title="#{bundle.CreateGestionTitle_fechaLimiteGestion}" required="true" requiredMessage="#{bundle.CreateGestionRequiredMessage_fechaLimiteGestion}">
</p:calendar>
<h:outputLabel value="#{bundle.CreateGestionLabel_usuario}" for="usuario" />
<p:selectOneMenu id="usuario" value="#{miscMB.nuevaGestion.usuario}" required="true" requiredMessage="#{bundle.CreateGestionRequiredMessage_usuario}">
<f:selectItems value="#{usuarioController.itemsAvailableSelectOne}"/>
</p:selectOneMenu>
</h:panelGrid>
<br />
<p:commandButton onsuccess="micalendario.update();PF('dlg1').hide();" action="#{gestionController.crearGestion()}" value="#{bundle.CreateGestionSaveLink}" >
<f:setPropertyActionListener target="#{gestionController.selected}" value="#{miscMB.nuevaGestion}"/>
<f:setPropertyActionListener target="#{gestionController.factura}" value="#{miscMB.factura}"/>
<f:setPropertyActionListener value="#{null}" target="#{miscMB.nuevaGestion}"/>
<f:actionListener binding="#{miscMB.anularNuevaGestion()}"/>
<f:actionListener binding="#{miscMB.agregarGestion(gestionController.selected)}"/>
</p:commandButton>
</h:form>
</p:dialog>
This is where I call the dialog to display
<p:commandLink value="Asignar gestión" onclick="PF('dlg1').show();return false;" action="#{gestionController.preparar()}" rendered="#{miscMB.factura!=null}">
<f:setPropertyActionListener target="#{gestionController.factura}" value="#{miscMB.factura}"/>
</p:commandLink>
What happens is that both <h:outputText value="#{miscMB.factura.cliente.nombre}"/> and <h:outputText value="#{miscMB.factura}"/> values display the properties' last state.
For example, the first time an item selected and you call the dialog, the output is none, because the state is not set. Then, when you select another item and call the dialog, the output is the state last set, which is the previous item value.
Apparently, the setPropertyActionListener in the commandLink is called after the dialog is closed or it executes an action, but not when the commandLink is clicked.
So, how can I set the properties before the dialog frame is displayed?
I am trying to have a button that sets edit mode and refreshes the form, but by clicking the button, nothing happens.
This is my code:
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<p:commandButton type="submit" value="Edit Your Records" icon="ui-icon-edit"
update="#form" rendered="#{!bean.editMode}">
<f:setPropertyActionListener value="true" target="#{bean.editMode}"/>
</p:commandButton>
<p:commandButton type="submit" value="Exit Edit Mode" icon="ui-icon-back"
update="#form" rendered="#{bean.editMode}">
<f:setPropertyActionListener value="false" target="#{bean.editMode}"/>
</p:commandButton>
</h:panelGrid>
<p:dataTable id="table" value="#{bean.table}" var="apartment">
...
</p:dataTable>
</h:form>
Thanks!
I would suggest using
<h:form>
<p:commandButton value="Edit Your Records" update="#form" rendered="#{!bean.editMode}" action="#{bean.toggleEditMode()}" />
<p:commandButton value="Exit Edit Mode" update="#form" rendered="#{bean.editMode}" action="#{bean.toggleEditMode()}" />
<h:outputText value="#{bean.editMode}" />
and
public void toggleEditMode() {
this.editMode = !this.editMode;
}
ok, it seams that the function called in the 'action' attribute needs to return a String.
The following worked:
public String toggleEditMode() {
this.editMode = !this.editMode;
return "#";
}
Thanks!