【问题标题】:Conditionally open global p:confirmDialog with p:confirm使用 p:confirm 有条件地打开全​​局 p:confirmDialog
【发布时间】:2014-12-15 13:33:12
【问题描述】:

我想基于一个 bean 布尔值打开一个全局 p:confirmDialog。我想要这样的东西:

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

但渲染在那里不起作用(我希望)。

另外,我不想复制p:commandButton 并使用它的渲染属性来实现这一点。

有没有什么方法可以在不改变太多东西的情况下完成这项工作?我必须用很多按钮来完成。

【问题讨论】:

    标签: jsf jsf-2 primefaces dialog


    【解决方案1】:

    要尝试的一件事是使用您的布尔值来呈现您的 &lt;p:confirmDialog&gt;

    <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>
    

    如果这对你不起作用,我看到的唯一其他方法是你试图避免的两个 &lt;p:commandButton&gt; 选项。

    【讨论】:

    • 嗯,谢谢,但我不知道这是否适合我。我正在为整个应用程序使用该对话框,并且我必须控制在每种情况下都正确设置布尔变量。这只是我想有条件地打开它的一些特殊情况。这不能做吗? :_
    • 不幸的是,无法使用&lt;p:confirm&gt; 标签上的属性有条件地呈现。你将不得不使用一个布尔变量来完成你想要的。将布尔值初始化为 false,然后在少数需要 &lt;p:confirmDialog&gt; 的特殊情况下将其更改为 true。
    • 是的……谢谢……可惜了。我现在正在考虑以某种方式以编程方式进行操作
    【解决方案2】:

    您必须使用可见而不是渲染。代码将是这样的:

        <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>
    

    如果您需要在完成时显示对话框,请在适当的事件中设置 dlgWgt.show()。

    【讨论】:

    • 条件在哪里?该对话框每次都显示。另外,visible 属性默认为 false。
    • 我解开你的问题的方式,不需要布尔变量,对话框总是隐藏,但呈现,任何按钮都需要显示对话框,调用小部件变量 show() 函数.如果渲染为假(隐式或使用条件),您将永远无法访问它,因为它根本不存在(它没有渲染,因此您无法通过 ajax 调用更新它)。这就是我在我的应用程序中处理相同情况的方式。我希望我已经正确理解了这个问题。
    • visible="true" 在加载时显示 &lt;p:confirmDialog&gt;,默认情况下为 false,因为您希望它不是在页面加载时从您的 &lt;p:commandButton&gt; 触发的。
    【解决方案3】:

    这有点笨拙,但您可以在 commandButton 和 p:confirm 标签之间使用 JSTL "c:if" 标签。因为 JSTL 标记在 Facelet 页面的执行流程中更早地被解释,所以 p:confirm 标记不会抱怨它的父级不是有效的命令按钮。例如:

          <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】:

      我知道这个问题有点老了,但我最近在条件/动态确认方面遇到了同样的问题,上述解决方案对我来说都不是很好。

      经过一些测试,我使用 disabled 属性(在 PrimeFaces 6.0 中引入)构建了我的按钮,如下所示:

      <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>
      

      【讨论】:

      • PrimeFaces 的确认禁用了哪个版本?
      • 我使用的是 PrimeFaces 6.0,但不知道他们何时添加了该功能
      • disabled 属性和 c:if 解决方案都有效,但我更喜欢这个。
      • 由于我没有在问题上指定版本,我将把它标记为最佳答案。他们在 PrimeFaces 6.0 上引入了 disabled 属性。对于旧版本,请检查 c:if answer,这也可以。谢谢!
      • rendered 属性不起作用,但 disabled 属性起作用。 (PF 7.0)
      猜你喜欢
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      相关资源
      最近更新 更多