如何避免重复单击表单中的按钮?

时间:2011-06-26 06:10:10

标签: jsf primefaces

我的代码:

<h:form id="newBSTypePanel" >
    <h:panelGrid columns="2" id="newRecod" >
        <h:outputText value="Name"/><h:inputText value="#{treeTableController.newBStypeBean.currentObject.TYPENAME.value}" required="true" />
        <p:commandButton value="save" action="#{treeTableController.saveNewNodes}" oncomplete="Dlg.hide()" update="productDataForm"/>
        <p:commandButton value="close" oncomplete="Dlg.hide()" />
    </h:panelGrid>
</h:form>

保存操作有很多功能。如果我反复单击该按钮,它可能会在数据库中保存一些记录。那不是我的愿望。如何防止多次点击并解决此问题?

4 个答案:

答案 0 :(得分:35)

<p:commandButton>的客户端API小部件:

  • PrimeFaces.widget.CommandButton

  • 方法参数返回类型说明

  • disable() - void禁用按钮
  • enable() - void启用按钮

所以你可以这样使用:

<p:commandButton widgetVar="saveButton"
                 onclick="saveButton.disable()"
                 value="save"
                 action="#{treeTableController.saveNewNodes}" 
                 oncomplete="saveButton.enable();Dlg.hide()"
                 update="productDataForm"/>

答案 1 :(得分:5)

对于较新版本的PrimeFaces,解决方案是:

 <p:commandButton widgetVar="saveButton"
                 onclick="PF('saveButton').disable()"
                 value="save"
                 action="#{treeTableController.saveNewNodes}" 
                 oncomplete="PF('saveButton').enable();PF('Dlg').hide()"
                 update="productDataForm"/>

答案 2 :(得分:3)

使用Javascript和计时器

<script>

function disableClick(){
   document.getElementById('saveButton').disables = true;
   setTimeout('document.getElementById(\'saveButton\').disables = false', 5000)"
}
</script>


 <h:form id="newBSTypePanel" >
    <h:panelGrid columns="2" id="newRecod" >
        <h:outputText value="Name"/><h:inputText value="#{treeTableController.newBStypeBean.currentObject.TYPENAME.value}" required="true" />
        <p:commandButton value="save" action="#{treeTableController.saveNewNodes}" oncomplete="Dlg.hide()" onclick="disableClick()" id="saveButton" update="productDataForm"/>
        <p:commandButton value="close" oncomplete="Dlg.hide()" />
    </h:panelGrid>
</h:form>

答案 3 :(得分:2)

作为一种通用方法,您可以自定义按钮渲染器。

我将此渲染器用于PrimeFaces p:commandButton

public class CommandButtonSingleClickRenderer extends CommandButtonRenderer {

  @Override
  public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    CommandButton button = (CommandButton) component;
    if (button.isRendered() && !button.isDisabled()) {
      button.setOnclick(prefix(button.getOnclick(),
                        "if(this!==null){disableButton(this.id)}"));
      button.setOncomplete(prefix(button.getOncomplete(),
                           "if(this!==null){enableButton(this.source)}"));
    }
    super.encodeEnd(context, component);
  }

  protected String prefix(final String base, final String prefix) {
    return base == null ? prefix : prefix + base;
  }

}

faces-config.xml:

<render-kit>
  <renderer>
    <component-family>org.primefaces.component</component-family>
    <renderer-type>org.primefaces.component.CommandButtonRenderer</renderer-type>
    <renderer-class>com.whatever.CommandButtonSingleClickRenderer</renderer-class>
  </renderer>
</render-kit>

JavaScript:

function disableButton(id) {
  if (id === null) {
    return;
  }
  var widget = PrimeFaces.getWidgetById(id);
  if (widget !== null) {
    widget.disable();
  }
}

function enableButton(id) {
  if (id === null) {
    return;
  }
  var widget = PrimeFaces.getWidgetById(id);
  if (widget !== null) {
    widget.enable();
  }
}