【问题标题】:How to dynamically disable/enable commandbutton in datatable in JSF/PrimeFaces如何在 JSF/PrimeFaces 的数据表中动态禁用/启用命令按钮
【发布时间】:2019-05-24 22:06:20
【问题描述】:

我在 primefaces 数据表中显示一些对象的列表。除了与对象相关的列之外,我还添加了两个包含命令按钮的列。这些命令按钮中的每一个都对 bean 中所需的方法进行 ajax 调用。我想在单击任何按钮时禁用该行中的两个命令按钮,并在成功完成操作后再次启用它们。方法执行由 ajaxStatus 模拟,运行良好。

以下是选定的xhtml代码

<h:form id="form1" >
<p:remoteCommand name="onload" action="#{controlBean.init}" autoRun="true" />  
   <h:outputtext>Sample Project</h:outputtext>
  <p:ajaxStatus style="display:block;margin-bottom:2em;height:24px;">
    <f:facet name="prestart">
    <h:outputText value="Starting..." /> </f:facet>
    <f:facet name="error"> <h:outputText value="Error" />
    </f:facet>
    <f:facet name="success"> <h:outputText value="Success" />
    </f:facet>
    <f:facet name="default"> <h:outputText value="Idle" />
    </f:facet>
    <f:facet name="start"> <h:outputText value="Please Wait" />
        <p:graphicImage name="/images/ajaxloadingbar.gif" />
    </f:facet>
</p:ajaxStatus>
      <p:dataTable var="appl" id="tbl1" rowIndexVar="rowIndx"  value="#{controlBean.applsList}">

<p:column headerText="Name">
    <h:outputText value="#{appl.applName}" />
</p:column>

<p:column headerText="Type">
    <h:outputText value="#{appl.applType}" />
</p:column>

<p:column headerText="Desc" rendered="true">
    <h:outputText value="#{appl.applDesc}" />
</p:column>

<p:column headerText="Start Appl" id="col4">
    <p:commandButton value="Submit" ajax="true"
    process="@this"
    widgetVar="startButtonVar"
    id="startBtn" update="msgs, col4, startBtn" 
    action="#{controlBean.startAction}" style="margin-right:20px;" 
    styleClass="ui-priority-primary"
    disabled="#{controlBean.startBtnDisabled}"> 

        <f:setPropertyActionListener value="#{appl}" target="#{controlBean.selectedAppl}"/>
    </p:commandButton> 
</p:column>
<p:column headerText="Stop Appl" id="col5">
    <p:commandButton value="Submit" ajax="true" 
    process="@this"
    widgetVar="stopButtonVar"
    id="stopBtn" update="msgs, col5" 
    action="#{controlBean.stopAction}" style="margin-right:20px;" 
    styleClass="ui-priority-primary" 
    disabled="#{controlBean.btnDisabled}">
        <f:setPropertyActionListener value="#{appl}" target="#{controlBean.selectedAppl}"/>

    </p:commandButton>

</p:column>

 </p:dataTable>
</h:form>

支持bean的方法如下:

  public void startAction() 
{
    System.out.println("In start action method");
    this.btnDisabled = true;

    for(int i=0;i<100000;i++)
    {
        if(i%10000 == 0) 
        {
            System.out.println(i);
        }
        for(int j=0;j<10000;j++)
        {
            for(int k=0;k<1000;k++)
            {

            }
        }
    }
    MessageUtils.info(getSelectedAppl().getApplName()+" has been started");
    this.btnDisabled = false;

}

public void stopAction() 
{
    System.out.println("In stop action method");
    this.btnDisabled = true;
    for(int i=0;i<100000;i++)
    {
        if(i%10000 == 0) 
        {
            System.out.println(i);
        }
        for(int j=0;j<10000;j++)
        {
            for(int k=0;k<1000;k++)
            {

            }
        }
    }
    MessageUtils.info(getSelectedAppl().getApplName()+" has been stopped");
   this.btnDisabled = false;

}

除了禁用/启用命令按钮外,一切正常。在这方面的任何帮助将不胜感激。 问候

【问题讨论】:

    标签: jsf dynamic primefaces datatable commandbutton


    【解决方案1】:

    您可以在 onclick javascript 处理程序上禁用它们:

    <p:commandButton id="startBtn"
                     widgetVar="startButtonVar"
                     onclick="PF('startButtonVar').disable();"
                     .....
    

    onclick 将在 AJAX 调用之前执行。返回时,随着按钮的更新,它将重新启用。

    【讨论】:

    • 感谢您的回复。这个特殊的解决方案不起作用。虽然它禁用了数据表中的一个按钮,但不是被点击的那个
    • @FahimAshraf:然后调整它以匹配您确实想要禁用的按钮。海报无法知道这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    相关资源
    最近更新 更多