Hello I'm trying to solve this problem. Hope you can help me.
I have a question form with optionals question (textfields, checkbox, radiobuttons). I'm trying to open a confirm dialog ("There are some question not answered, are you sure you want to continue? YES/NO") just if there are question unfilled. I can OPEN de dialog to ask for confirmation. But I cant validate if the dialog has to be displayed or it have to be skipped.
<h:form>
<div class="col-xs-12 ctg-home-button center">
<p:commandButton
id="cancel-button"
actionListener="#{answerSurveyView.restart}"
immediate="true"
styleClass="btn btn-default"
title="#{propertiesBean.getProperty('answer.cancel')}"
value="#{propertiesBean.getProperty('answer.cancel')}"
/>
<h:outputText value=" " />
<p:commandButton
id="answer-button"
action="#{answerSurveyView.saveAnswers}"
styleClass="btn btn-default"
title="#{propertiesBean.getProperty('answer.send')}"
update="answer-form"
value="#{propertiesBean.getProperty('answer.send')}"
>
<p:confirm header="Confirmation" message="#{propertiesBean.getProperty('answer.survey-confirmation')}" icon="ui-icon-alert" />
</p:commandButton>
</div>
<p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
<p:commandButton value="#{propertiesBean.getProperty('answer.send')}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="#{propertiesBean.getProperty('answer.close')}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</h:form>
Use the rendered attribute of your confirmDialog to indicate whether you want render it or not. Set to the rendered attribute a value returned from your view, indicating whether there are unasnwered questions or not.
Reload your confirmDialog each time the user asnwers a question by using an ajax on event, calling a method on your view to recalculate the unansweredQuestions attribute; and the update attribute with your confirmDialog id as value, so you can set rendered to false once the last question is ansewered.
For instance, if you were using a dropdown to give options for the answer, the code of the page might look like this:
<div class="col-xs-12 ctg-home-button center">
<p:commandButton ....
.....
</div>
<p:selectOneMenu value="#{answerSurveyView.firstQuestionAnswer}">
<p:ajax listener="#{dropdownView.onAnswerSelect}" update="myConfirm" />
<f:selectItem itemLabel="Select option" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{answerSurveyView.firstQuestionAnswerOptions}" />
</p:selectOneMenu>
<p:confirmDialog id='myConfirm' rendered="#{answerSurveyView.unansweredQuestions}" global="true" showEffect="fade" hideEffect="fade">
<p:commandButton value="#{propertiesBean.getProperty('answer.send')}" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="#{propertiesBean.getProperty('answer.close')}" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
And the code in your view:
// declare unansweredQuestions attribute and set it to false
private boolean unansweredQuestions = false;
// unansweredQuestions getter
public boolean getUnansweredQuestions() {
return unansweredQuestions;
}
// call this method every time a question is answered
// to update the unansweredQuestions value
public void onAnswerSelect() {
unansweredQuestions = foo(); //method returns true if there are questions left and false if not.
}
Related
I have an inputTextArea in a dialog and wanted that the bean property shouldn't be sent/changed when the user clicks on the cancel button, but it does.
<p:dialog header="Notizen" id="paketNotizenDialog" modal="true"
widgetVar="paketNotizenDialogWV">
<h:form>
<p:panelGrid columns="1">
<p:inputTextarea scrollHeight="200" rows="6" cols="33" autoResize="false"
value="#{paketErstellenDialogController.selectedPaket.notiz}" />
</p:panelGrid>
<p:commandButton value="Save" process="#form" oncomplete="PF('paketNotizenDialogWV').hide();"/>
<p:commandButton value="Cancel" oncomplete="PF('paketNotizenDialogWV').hide();" process="#none" update="#none" />
</h:form>
</p:dialog>
The button which opens the dialog:
<p:commandButton id="notizEintragButton" value="T" process="#this"
onclick="PF('paketNotizenDialogWV').show();" />
Any hints? Thanks in advance.
As you are using the commandButton, the default behaviour would be to submit the enclosing form with ajax request.
I suspect what you want to do here is to reset the form input and close the dialog. In that case you should go for the type="reset" which according to the primefaces doc:
Reset buttons do not submit the form, just resets the form contents.
And once that is done, trigger your closing javascript code:
<p:commandButton value="Cancel" type="reset"
onclick="PF('paketNotizenDialogWV').hide();"/>
If you do not want to reset the form, just close the dialog then use:
<p:commandButton value="Cancel" type="button"
onclick="PF('paketNotizenDialogWV').hide();"/>
Which according to the primefaces doc would:
Push buttons are used to execute custom javascript without causing an
ajax/non-ajax request. To create a push button set type as "button"
Update
If you want to reset the values from the backing bean then use reset input fields functionality of primefaces.
In your case it would be something like:
<p:panelGrid columns="1">
<p:inputTextarea id="input" scrollHeight="200" rows="6" cols="33" autoResize="false"
value="#{paketErstellenDialogController.selectedPaket.notiz}" />
</p:panelGrid>
<p:commandButton value="Cancel" oncomplete="PF('paketNotizenDialogWV').hide();"
process="#this" update="input" >
<p:resetInput target="input" />
</p:commandButton>
just add type="button" and remove process="#none" update="#none"
from <p:commandButton value="Cancel" oncomplete="PF('paketNotizenDialogWV').hide();" process="#none" update="#none" />
I am designing a page technology using jsf 2.0 and primefaces. In my page I insert a prime faces calendar. When the user click on calendar to change the default date, a warning message shown which is a dialog box containing two command button. Now what I am trying to do is when the user click on cancel command button, the date should be changed back to the default date which was populated from the first time page when the page loaded. I wrote a java script but its not working.
<label>Date<label>
<p:calendar readonlyInput="true" widgetVar="calDate" yearRange="c-20:c+20" navigator="true" id="b1incomeeffectfrom#{incmStatus.index}" value="#{userDate.effectiveFrom}" pattern="dd/mm/yyyy">
<p:ajax event="dateSelect" oncomplete="checkForDefault(this);" global="false"/>
</p:calendar>
<p:dialog rendered="#{user.Lock}" widgetVar="dlg1" header="Warning" modal="true">
<h:panelGroup layout="block" styleClass="modal-dialog">
<h:panelGroup layout="block" styleClass="modal-content">
<h:panelGroup layout="block" styleClass="modal-body">
<div class="form-horizontal">
<h:panelGroup layout="block" styleClass="form-group">
<h:outputLabel styleClass="col-xs-8 control-label">Are you sure want to change?</h:outputLabel>
</h:panelGroup>
</div>
</h:panelGroup>
<h:panelGroup layout="block" styleClass="modal-footer">
<p:commandButton value="Ok" type="button" styleClass="btn btn-primary" onclick="PF('dlg1').hide();PF('dlg2').show();" />
<p:commandButton value="Cancel" type="button" styleClass="btn btn-primary" onclick="resetDate();" />
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
</p:dialog>
javascript
function resetDate(){
PF('dlg1').hide();
calDate.setDate(null);
}
Your calendar value is coming from your backing bean (userDate.effectiveFrom). You will have to change the value there to take effect
<p:commandButton value="Cancel" type="button" styleClass="btn btn-primary"
onsuccess="PF('dlg1').hide()" action="#{userDate.resetDate()}" update="calDate" />
class UserDate{
[..]
public void resetDate(){
this.effectiveFrom = initialDate;
}
[..]
}
I'm trying to show an updated value in confirm dialog message but I keep getting the old value as in this scenario
<h:form>
<p:inputText value="#{bean.object.amount}"/>
<p:commandButton value="CALCULATE" update="cal" actionListener="#{bean.calculate()}"/>
<h:panelGroup id="cal">
<h:outputText value="#{bean.object.amount}"/>
<p:commandButton value="SUBMIT" actionListener="#{bean.submit()}">
<p:confirm header="Confirmation" message="Amount is : #{bean.object.amount} ?"/>
</p:commandButton>
<p:confirmDialog global="true">
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</h:panelgGroup/>
</h:form>
Bean code:
#ManagedBean(name="bean")
#ViewScoped
public class Bean implements Serializable {
private SomeClass object;
#PostConstruct
public void init(){
this.object = new SomeClass();
}
public void calculate(){
//do some colculation (not related to amount field in object)
}
public void submit(){
//submit to database
}
//getter setter
}
When I enter a value in amount, lets say 50. and update the cal component I get the updated amount in the outputtext "50". However, in the confirm button message I get amount as 0 instead 50. How can I show the updated value in the confirm message?
PS:
Primefaces-4.0
Take a look at the user guide of primefaces in the confirm dialog section, in the Non-Global mode the document mentioned:
Message facet is
useful if you need to place custom content instead of simple text.
While in the Global mode, I can't find similar sentences like that, and I've tried using the facet in Global mode and it doesn't work.So,
Do you really use this confirm dialog multiple times?
If not:
I suggest you take away the global parameter and change your code like this:
<h:form>
<p:inputText value="#{bean.object.amount}"/>
<p:commandButton value="CALCULATE" update="cal" actionListener="#{bean.calculate()}"/>
<h:panelGroup id="cal">
<h:outputText value="#{bean.object.amount}"/>
<p:commandButton value="SUBMIT" actionListener="#{bean.submit()}" oncomplete="PF('confirmDlg').show()"/>
<p:confirmDialog header="Confirmation" widgetVar="confirmDlg">
<f:facet name="message">
<h:outputText value='Amount is : #{bean.object.amount} ?'/>
</f:facet>
<p:commandButton value="Yes" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</h:panelGroup>
</h:form>
.
If so:
(You do use the confirm dialog multiple times and tired of writing several dialogs with same form but different message.)
I suggest you write a dialog on your own,and you can also change the message in the dialog from backing bean like what you did:
<h:form id="myForm">
<p:inputText value="#{bean.object.amount}"/>
<p:commandButton value="CALCULATE" update="cal" actionListener="#{bean.calculate()}"/>
<h:panelGroup id="cal">
<h:outputText value="#{bean.object.amount}"/>
<ui:param name="message" value="Amount is :#{bean.object.amount}?" />
<p:commandButton value="SUBMIT" actionListener="#{bean.setMessage(message)}" action="#{bean.submit()}" update="myForm:myDialog" oncomplete="PF('myDlg').show()"/>
</h:panelGroup>
<p:dialog id='myDialog' widgetVar="myDlg" header="Confirmation" modal="true" resizable="false">
<h:panelGrid columns="3">
<h:panelGroup styleClass="ui-icon ui-icon-alert" style="float:right"/>
<h:outputText value="#{bean.message}"/>
<h:outputText/>
<h:outputText/>
<p:commandButton value="Yes" type="button" icon="ui-icon-check" oncomplete="PF('myDlg').hide()"/>
<p:commandButton value="No" type="button" icon="ui-icon-close" onclick="PF('myDlg').hide()"/>
</h:panelGrid>
</p:dialog>
</h:form>
p:confirm does not implement state saving, thus it loses its attributes' values after the first JSF lifecycle. It also evaluates EL only once at view build time.
I posted the solution in this answer.
Inside a <a4j:outputPanel> I have a disabled inputtext that displays Authors from a List<Author> that belongs to the class DocumentBean. I have also a <h:selectOneMenu> that displays names of Authors. When an Author from the menu is selected the Author is added to the List<Author> I mentioned above, and a new disabled inputtext appears.
Here's the code described above:
Autor:
<a4j:outputPanel id="teste">
<a4j:repeat value="#{insertDocController.docController.documentBean.authorList}" var="author">
<br />
<h:inputText value="#{author.name}" disabled="true" />
<br />
<a4j:commandButton type="submit" action="#{insertDocController.removeAutor(author.uri)}"
value="Remover" render="teste" />
</a4j:repeat>
<br />
<h:selectOneMenu id="chooseAuthor" value="#{insertDocController.selectedAuthorUri}">
<f:selectItem noSelectionOption="true" itemLabel="selecione" itemValue=""/>
<f:selectItems value="#{insertDocController.authorValues}" var="author"
itemValue="#{author.uri}" itemLabel="#{author.name}"/>
</h:selectOneMenu>
<a4j:commandButton type="submit" action="#{insertDocController.addAuthor()}"
value="Inserir" render="teste" />
<br />
</a4j:outputPanel>
Here's the addAuthor() from insertDocController:
public void addAuthor() {
if(this.selectedAuthorUri != null) {
AuthorBean a = new AuthorBean();
a.setUri(this.selectedAuthorUri);
a.setName(this.authMapper.searchNameByAuthorUri(this.selectedAuthorUri));
this.docController.getDocumentBean().addAuthor(a);
}
}
On that same xhtml page there's an inputtext. Everything was working properly, when the inputtext was like this:
Title: <h:inputText id="input"
value="#{insertDocController.docController.documentBean.title}">
</h:inputText>
But stopped working properly since I set the inputtext to be required and added a message to it, like this:
Title: <h:inputText id="input"
value="#{insertDocController.docController.documentBean.title}"
required="true" requiredMessage="Campo Obrigatorio">
<f:ajax execute="#this input" render="message" event="blur"/>
</h:inputText>
<h:message for="input" id="message" style="color:red"/>
After I changed the title inputtext, when I choose an Author on the <h:selectedOneMenu> it simply doesn't call the action method addAuthor() from the <a4j:commandbutton> inside the <h:selectedOneMenu>.
(That's the line with the action method that isn't called - It was extracted from the above code I gave you at first):
<a4j:commandButton type="submit" action="#{insertDocController.removeAutor(author.uri)}"
value="Remover" render="teste" />
If I turn the inputtext as it was before it starts to work again.
What may be the problem? Thank you!
You need to specify the execute attribute.
Delete Author
<a4j:commandButton type="submit" action="#{insertDocController.removeAutor(author.uri)}"
value="Remover" render="teste" execute="#this">
</a4j:commandButton>
For the add author button
<a4j:commandButton type="submit" action="#{insertDocController.addAuthor()}"
value="Inserir" render="teste" execute="#this, chooseAuthor">
</a4j:commandButton>
Hope it helps.
I would like to display in a dialog the number inserted in the inputText.
<p:inputText id="nbr"
type="number"
value="#{MB.number}"
required="true"
label="nbr" />
<p:confirmDialog id="confirmPurchase"
message="Your Database was successfully created. And contains "
appendToBody="true"
header="Buy Emails List"
severity="info"
widgetVar="purchase">
<a class="boldtext">
#{MB.number}
<h5> datas</h5>
</a>
<p:commandButton id="confirm" value="Buy" actionListener="#{MB.buy())}" />
<p:commandButton id="decline"
value="Decline"
onclick="purchase.hide();"
type="button" />
</p:confirmDialog>
The code below returns always 0 as a number in the confirm dialog.
Update1
the dialog is showing once the action in the commandButton is completed
<p:commandButton value="Extract" update="table nbr" id="ajax" ajax="true" widgetVar="extractButton action="#{MB.search()}" oncomplete="purchase.show();"/>
First of all, you need a <h:form/> around your <p:inputText> and your <p:commandButton>.
Your <p:commandButton> should have the attribute update=":outputUserText".
Inside your Dialog, you need a <p:outputLabel id="outputUserText" value="#{MB.number}"