【问题标题】:How to enable/disable primefaces commandButton?如何启用/禁用primefaces commandButton?
【发布时间】:2013-10-04 07:45:33
【问题描述】:

我在屏幕上有两个按钮。首次加载页面时,我希望 button2 被禁用,直到 button1 被单击。 当 button1 被点击时,button2 必须被启用。

我试过了:

<p:commandButton value="Normalize"
    actionListener="#{mainTable.normalize}" update="dataTable"
    id="normalize" styleClass="ui-priority-primary"
    style="font-size: 14px">
    <f:setPropertyActionListener value="#{true}"
        target="#{mainTable.disable}" />
</p:commandButton>
<p:commandButton value="To Verify Next->" action="verify.xhtml"
    actionListener="#{mainTable.verify}" id="next"
    styleClass="ui-priority-primary" style="font-size: 14px"
    disabled="#{!(bean.disable)}">
</p:commandButton>

我的豆子:

@ManagedBean
@SessionScoped
public class MainTable
{

    private boolean disable;

    public MainTable()
    {
        disable = false;
    }
    public boolean isDisable()
    {
        return disable;
    }

    public void setDisable(boolean disable)
    {
        this.disable = disable;
    }
}

但它不起作用。当我单击按钮 1 时,按钮 2 仍然被禁用。 怎么了?

【问题讨论】:

  • 调试你的代码。是否设置了 disable 属性?另外,当您拥有normalize 动作侦听器时,为什么还要使用f:setPropertyActionListener 标签?只需在调用normalize 时将其设置为true。
  • 我复制了你的代码,它对我来说非常有效。
  • @XtremeBiker 我也试过了(在 normalize 方法中设置)但它也不起作用。
  • 抱歉是关于 line: disabled="#{!(bean.disable)}"> 应该是 mainTable.disable 问题解决了。
  • 很高兴看到它成功了。尝试为您自己的问题发布答案,而不是编辑问题本身以在此处发布答案。

标签: jsf jsf-2 primefaces commandbutton


【解决方案1】:

尝试在单击第一个按钮时更新第二个按钮,例如 dataTable


您应该将disabled="#{!(bean.disable)}"&gt; 中的bean 替换为mainTable => disabled="#{!(mainTable.disable)}"&gt;

【讨论】:

  • 任何错误信息?也许尝试将两个按钮包装在一个容器中,比如 primefaces outputPanel (p:outputPanel)
  • 我的回答对您有帮助吗?我看到你解决了它,但不更新按钮它不应该工作。
【解决方案2】:

您应该在输出面板中交换 2 个按钮,并将此输出面板更改为下一个按钮,如下所示:

<p:outputPanel id="pnltest">
  <p:commandButton value="Normalize"
    actionListener="#{mainTable.normalize}" update="dataTable,pnltest"
    ...
  </p:commandButton>
  <p:commandButton value="To Verify Next->" action="verify.xhtml"
    actionListener="#{mainTable.verify}" id="next"
    ...
  </p:commandButton>
<p:outputPanel>

【讨论】:

  • 这应该如何解决?此问题与rendered 属性无关,而与disabled 属性有关。
  • @XtremeBiker 这是我的经验,它适用于某些项目:-)
  • @XtremeBiker 在某些情况下,OP 示例工作正常,但在某些情况下我必须按照我的回答去做。
  • 但是当你不需要更新整个面板时......只需更新目标命令按钮就可以了。
  • 谢谢@RongNK,问题是我的疏忽。
猜你喜欢
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多