Primefaces call ConfirmDialog from Backing - java

I would like to call confirmDialog via backing. This code works perfectly, but how can I set the message and set the actionlistener of the confirmDialog via backing? There is two condition, while:
The user check the option A on the checkbox (I omitted the code), then it should be directly print a text to console. --> This one is done by the code below
The user check the option B on the checkbox, then it should be show the confirmDialog and while the user press YES button, it should be call another function on the backing.
How to do that? Thanks.
<p:commandButton value="Execute" icon="ui-icon-circle-check" update="frmContent" actionListener="#{backing.validate}" />
<p:confirmDialog id="cfmDlg" widgetVar="wvCfmDlg" 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>
In Backing:
public void validate() {
if(mode.equals("1")) {
System.out.println("OK");
} else {
//call confirmDialog and set message + action listener
RequestContext context = RequestContext.getCurrentInstance();
context.execute("wvCfmDlg.show();");
}
}

If I understood your question correctly.. I would do it in this way.
xhtml
<p:commandButton style="display: none"
widgetVar="confirmButton"
actionListener="#{backing.yesFunction}" >
<p:confirm header="Confirmation" message="Are you sure?" />
</p:commandButton>
<p:commandButton value="Execute"
actionListener="#{backing.validate}" />
<p:confirmDialog id="cfmDlg" global="true" >
<p:commandButton value="Yes" />
<p:commandButton value="No" />
</p:confirmDialog>
bean
public void validate() {
if(mode.equals("1")) {
System.out.println("OK");
} else {
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('confirmButton').jq.click();");
}
}
Basically you add a hidden button (with p:confirm) in the usual way and you click it through jQuery.

Related

Dialog over other dialog in primefaces

Initially, sorry for my English.
Well, I have dialogs in my application that work individually very well each of them, but when I need one dialog to call another, I have problems closing this second dialog opened on top of the first one. I am creating them by the java API from the primefaces. What would solve this problem?
EDIT:
Right, I call my first dialog here:
<h:outputLabel value="#{bundle.CreateProfissionalLabel_idRegraComissao}" />
<h:panelGroup>
<h:inputText readonly="true" value="#{profissionalController.selected.idRegraComissao.descricao}" id="nomeRegraComissao"/>
<p:commandButton value="#{bundle.Pesquisar}" action="#{searchRegraComissaoController.abrirDialogo()}" process="#this" update="#none">
<p:ajax event="dialogReturn" listener="#{profissionalController.regraComissaoSelecionada}" process="#this" update="nomeRegraComissao"/>
</p:commandButton>
</h:panelGroup>
My openDialog method (searchRegraComissaoController.abrirDialogo()) for the first dialog is:
public void abrirDialogo() {
Map<String, Object> opcoes = new HashMap<>();
opcoes.put("modal", true);
opcoes.put("resizable", false);
opcoes.put("contentHeight", 470);
RequestContext.getCurrentInstance().openDialog("/webapp/lovs/SearchRegraComissao", opcoes, null);
}
Now I have an xhtml file (/webapp/lovs/SearchRegraComissao.xhtml) that is rendered in this first dialog and is responsible for opening the second. Here's how it does it:
<p:outputLabel value="#{bundle.CreateRegraComissaoLabel_idClinica}" />
<p:panel>
<h:inputText readonly="true" value="#{searchRegraComissaoController.filtro.clinica.nome}" id="nomeClinica"/>
<p:commandButton value="#{bundle.Pesquisar}" actionListener="#{searchClinicaController.abrirDialogo()}" process="#this" update="#none">
<p:ajax event="dialogReturn" listener="#{searchRegraComissaoController.clinicaSelecionada}" process="#this" update="nomeClinica"/>
</p:commandButton>
</p:panel>
Right Here, it also has the method that opens this second dialog:
public void abrirDialogo() {
Map<String, Object> opcoes = new HashMap<>();
opcoes.put("modal", true);
opcoes.put("resizable", false);
opcoes.put("contentHeight", 450);
RequestContext.getCurrentInstance().openDialog("/webapp/lovs/SearchClinica", opcoes, null);
}
Well, So far, everything works fine, the problem is in the next step. Now I have the method that should close the second dialog. This method is called after the user select one row into dataTable in the second dialog:
<p:column headerText="#{bundle.Acao}">
<p:commandButton value="#{bundle.Escolher}" process="#this" action="#{searchClinicaController.selecionar(clinica)}"/>
</p:column>
And the final method is:
public void selecionar(Clinica clinica) {
clinicas = new ArrayList<>();
//todasClinicas = false;
// isCPF = "CPF";
//nome = "";
RequestContext.getCurrentInstance().closeDialog(clinica);
}
The end of this method is reached without errors but the second dialog isn't closed. Anyone can help?
SOLUTION: I was reading the guide of primefaces 6 and using primefaces 5. In primefaces 5 nested dialogs aren't supported. I just updated my version to 6 and now is working fine.
I had the same problem today with primefaces 5.2.
I solved by adding a form and config modal="true" and appendTo="#(body)"
in the second dialog.
Follows example:
main.xhtml
<h:body>
<h:form id="frmMain">
<p:commandButton value="Open"
process="#this" update="frmMain:dialog1"
oncomplete="PF('wdgDialog1').show()">
</p:commandButton>
<p:dialog id="dialog1" header="Dialog 1"
dynamic="true" modal="true"
widgetVar="wdgDialog1" >
<ui:include src="/dialog/dialog2.xhtml" />
</p:dialog>
</h:form>
</h:body>
dialog2.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<p:dialog id="dialog2" header="Dialog 2"
modal="true" appendTo="#(body)"
widgetVar="wdgDialog" >
<h:form id="frmDialog">
<p:panel>
<p:commandButton value="Test"/>
</p:panel>
</h:form>
</p:dialog>
</ui:composition>

h:selectOneMenu change event with p:ajax

I am trying to get the value from on change in h:selectOneMenu using p:ajax.
But I am getting null value always, dont know whats wrong below is the code.
<h:form>
<h:selectOneMenu id="selectMenu" value="#{userHomeController.gymsSelectType}">
<f:selectItem itemLabel="Close to me" itemValue="closest" />
<f:selectItem itemLabel="Visited by me" itemValue="visited" />
<p:ajax process="selectMenu" listener="#{userHomeController.selectMenuListener}" event="change" update=":home-form:panel" />
</h:selectOneMenu>
</h:form>
and the bean class is
public void selectMenuListener() {
System.out.println("-------- >> " + gymsSelectType); // here null coming
if (gymsSelectType.equals("a")) {
//
} else {
//
}
}
this is a viewscoped class.
and below setter for the variable gymsSelectType is also prints null
public void setGymsSelectType(String gymsSelectType) {
System.out.println("------------ >> "+gymsSelectType);
this.gymsSelectType = gymsSelectType;
}
Have your tries this
<p:ajax process="#this" listener="#{userHomeController.selectMenuListener}" event="change" update=":home-form:panel" />
Try this
<p:ajax partialSubmit="true" listener="#{userHomeController.selectMenuListener}" event="change" update=":home-form:panel" />

multiple f:ajax calls in multiple h:selectOneMenu

Consider the following form:
1. User selects a value from the selectOneMenu "selectorDB"
2. Ajax call returns items for the second selectOneMenu and makes the whole "second" panelGroup rendered
3. User selects a value from the second selectOneMenu
4. Ajax call returns String which is placed inside of the inputText and panelGroup which contains that inputText rendered.
Everything works fine till im not selecting a value from the second selectOneMenu. Ajax of the fourth action is not even called. I've placed a couple of console System.outs to check if the listener is called and got nothing.
Is that possible to make something like the thing I'm attempting to do?
Thank you for any replies.
*.jsf:
<h:form>
<h:panelGroup id="first">
<h:outputLabel value="Select a database to make queries against" for="selectorDB" />
<h:selectOneMenu id="selectorDB" value="#{sparqlQuerier.chosenRing}">
<f:selectItem itemLabel="Select..." itemValue="0" />
<f:selectItems value="#{sparqlQuerier.ringTitles}" />
<f:ajax render="second third" listener="#{sparqlQuerier.firstListener}"/>
</h:selectOneMenu>
</h:panelGroup>
<br />
<h:panelGroup id="second">
<h:outputLabel rendered="#{sparqlQuerier.doRenderSecond}" value="Select a query to execute" for="selectorQuery" />
<h:selectOneMenu rendered="#{sparqlQuerier.doRenderSecond}" id="selectorQuery" value="#{sparqlQuerier.chosenQuery}">
<f:selectItem itemLabel="Select..." itemValue="0" />
<f:selectItems value="#{sparqlQuerier.queries}" />
<f:ajax render="third" listener="#{sparqlQuerier.secondListener}"/>
</h:selectOneMenu>
</h:panelGroup>
<br />
<h:panelGroup id="third">
<h:inputText rendered="#{sparqlQuerier.doRenderThird}" value="#{sparqlQuerier.queryBody}" onfocus="if (this.value == '#{sparqlQuerier.queryBody}') {this.value = '';}" onblur="if (this.value == '') {this.value ='#{sparqlQuerier.queryBody}';}" />
</h:panelGroup>
</h:form>
bean:
public void firstListener(AjaxBehaviorEvent event)
{
doRenderThird = false;
doRenderSecond = false;
if(chosenRing!=0)
{
doRenderSecond = true;
try
{
// returning rows from the database...
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
public void secondListener(AjaxBehaviorEvent event)
{
doRenderThird=false;
if(chosenQuery!=0)
{
if(chosenQuery==-1)
{
queryBody="body #1";
}
else
{
queryBody="body #2";
}
doRenderThird=true;
}
System.out.println(chosenQuery);
System.out.println(doRenderThird);
}

How to display and update a "sub-page" within a condition (using primefaces)

I have a scenario to achieve but I don't know how to do it(I'am using primefaces)
The scenario is:
I have a selectOneMenu that contain options that the user can choose one of them,
when the user choose an option ,a "form" or a "Sub-page" should appear below the selectOneMenu , the user should fill it.
The "Sub-page" change according to the choosen option
eg :option1 ---> Sub-Page1
option2 --->Sub-Page2 etc..
JSF
<h:selectOneMenu style="width:200px" immediate="true" value="#{ToolsKPI.myChoice}" onchange="submit()" valueChangeListener="#{BeanTest.selectChangeHandler}" required="true" >
<f:selectItems value="#{ToolsKPI.getMyListKPI()}" />
</h:selectOneMenu>
<p:panel rendered="#{BeanTest.showPanelBool}" >
<h1>KPI1</h1>
<br></br>
<h:inputText value="test1" />
</p:panel>
<p:panel rendered="#{BeanTest.showPanelBool1}" >
<br></br>
<h1>KPI2</h1>
<h:inputText value="test2" />
<h:inputText value="test3" />
</p:panel>
My Bean
public class BeanTest implements java.io.Serializable {
private String myChoice;
private boolean showPanelBool;
private boolean showPanelBool1;
public BeanTest() {
}
//getters ande setters
public void selectChangeHandler(ValueChangeEvent event){
myChoice = (String) event.getNewValue(); // Must however be the exact page URL. E.g. "contact.jsf".
FacesContext.getCurrentInstance().getExternalContext();
if(myChoice.equals("Number Of Closed issues") ){
this.showPanelBool = true;
} else{
this.showPanelBool = false;
this.showPanelBool1 = true;
}
FacesContext.getCurrentInstance().renderResponse();
}
}
You should really post some semblance of the code: a picture something. What you're probably looking for is a "p:outputPanel" component or something similar.
http://www.primefaces.org/showcase/ui/outputPanel.jsf
You can do something with a ValueChangeListener akin to
public void onSelectItem(ValueChangeEvent evt)
{
if("someValue".equals(evt.getNewValue())
showPageOne = true;//boolean
else
showPageTwo = true;//boolean
}
//JSF
<h:form id="myForm">
<h:selectOneMenu valueChangeListener="#{myBean.onSelectItem}" update="myForm">
<f:selectItems value="items">
</h:selectOneMenu>
<p:outputPanel rendered="#{myBean.showPageOne}">
hi I'm page one
</p:outputPanel>
<p:outputPanel rendered="#{myBean.showPageTwo}">
hi I'm page two
</p:outputPanel>
</h:form>
Sorry for the pseudo code, the examples on Primefaces will really help you here. OutputPanel has saved my bacon often. You'll need to specify an update target, but the example is really clear and easy to run. Play with it.
(Also Primefaces is fantastic you'll likely be very happy with their component suite)

Prime faces validation for treeview

I´m currently exploring Prime faces, and I´m wondering how we can validate the selection from a treeview? With an for example inputtextarea, this is very simple
<p:inputTextarea id="description"
value="#{createAdd.add.description}" cols="50" rows="5"
validatorMessage="Max length is 25" required="true"
requiredMessage="Description is required">
<f:validateLength maximum="25" />
</p:inputTextarea>
<p:message for="description" />
Here we get valdation for the textinput using the required attribute and the tag. But for the TreeView component there is no required attribute, and the tags does not work.
So, given for example
<p:tree value="#{treeBean.root}" var="node" dynamic="true"
id="category" selectionMode="single"
selection="#{createAdd.add.category}" >
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
How can we validate that one node has actually been selected?
I think validation has to be done in the action method of your submit button.
If I get it right you have two beans for your p:tree: the TreeBean and the CreatAdd bean. I assume your action method is in the CreateAdd bean:
If your submit button looks like this:
<h:commandButton value="#{bundle.del}" action="#{CreateAdd.doSubmitAction}"/>
Then you could check the selected value in the action method:
public String doSubmitAction() {
if(category == null) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "No node selected", "No node selected");
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
return null;
}
// do other stuff
}
You can do validation in your backing bean.

Categories

Resources