Conditionally open global p:confirmDialog with p:c

2019-04-15 09:37发布

I want to open a global p:confirmDialog based on a bean boolean value. I would like to have something like this:

<p:commandButton value="Save" actionListener="#{bean.save}" 
                 update="@form" oncomplete="jsfunction();">
    <p:confirm header="Confirm" message="Are you sure?" rendered="#{bean.boolean}"/>
</p:commandButton>

But rendered doesn't work there (I wish).

Also, I don't want to duplicate the p:commandButton and use its rendered attribute to achieve this.

Is there any way to get this done without changing too many things? I have to do it in a lot of buttons.

4条回答
Viruses.
2楼-- · 2019-04-15 10:16

One thing to try would be using your boolean value to render your <p:confirmDialog>

<p:commandButton value="Save" actionListener="#{bean.save()}" 
                 update="@form" oncomplete="jsfunction();">
    <p:confirm header="Confirm" message="Are you sure?" />
</p:commandButton>

<p:confirmDialog global="true" rendered="#{bean.boolean}">
    <p:commandButton value="Yes" type="button"  />
    <p:commandButton value="No" type="button" />
</p:confirmDialog>

If that does not work for you the only other way I see is the two <p:commandButton> option you are trying to avoid.

查看更多
老娘就宠你
3楼-- · 2019-04-15 10:25

It's a bit hacky, but you can use a JSTL "c:if" tag between your commandButton and the p:confirm tag. Because JSTL tags are interpreted earlier in the execution flow of a Facelet page, the p:confirm tag won't complain that its parent isn't a valid commandButton. For example:

      <p:commandButton action="#{myBean.myMethod} value="Submit">
          <c:if test="#{myBeanBean.warningEnabled}">
            <p:confirm header="Confirmation" message="#{myBean.warningMessage}" icon="ui-icon-alert"/>
          </c:if>
      </p:commandButton>
查看更多
时光不老,我们不散
4楼-- · 2019-04-15 10:26

I know that this question is a bit old, but I was recently having the same problem with conditional/dynamic confirmations and none of the above solutions worked fine for me.

After some tests, I built my button using the attribute disabled (introduced in PrimeFaces 6.0) as in:

<p:confirmDialog global="true">
    <p:commandButton value="Yes" type="button" />
    <p:commandButton value="No" type="button" />
</p:confirmDialog>

<p:commandButton actionListener="#{myBean.myMethod} value="Submit">      
    <p:confirm header="Confirmation" message="Are you sure?" disabled="#{myBean.boolean}"/>
</p:commandButton>
查看更多
Juvenile、少年°
5楼-- · 2019-04-15 10:27

You must use visible instead of rendered. the code will be like this :

    <p:commandButton value="Save" actionListener="#{bean.save()}" 
                 update="@form" onstart="dlgWgt.show();" oncomplete="jsfunction();">
    <p:confirm header="Confirm" message="Are you sure?" />
</p:commandButton>

<p:confirmDialog global="true" visible="false" widgetVar="dlgWgt">
    <p:commandButton value="Yes" type="button"  />
    <p:commandButton value="No" type="button" />
</p:confirmDialog>

if you need the dialog be shown oncomplete, set the dlgWgt.show() in the proper event.

查看更多
登录 后发表回答