问题
My code:
<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>
There is quite a bit of functionality associated with the save action. If I click the button repeatedly, it may save a few records in the database. That's not my wish. How can I prevent multiple clicks and resolve this?
回答1:
The <p:commandButton>
's Client Side API Widget:
PrimeFaces.widget.CommandButton
Method Params Return Type Description
disable()
- void Disables buttonenable()
- void Enables button
So you can just use like this:
<p:commandButton widgetVar="saveButton"
onclick="saveButton.disable()"
value="save"
action="#{treeTableController.saveNewNodes}"
oncomplete="saveButton.enable();Dlg.hide()"
update="productDataForm"/>
回答2:
For the newer versions of PrimeFaces, the solution would be:
<p:commandButton widgetVar="saveButton"
onclick="PF('saveButton').disable()"
value="save"
action="#{treeTableController.saveNewNodes}"
oncomplete="PF('saveButton').enable();PF('Dlg').hide()"
update="productDataForm"/>
回答3:
Use Javascript and Timer
<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>
回答4:
As a generic approach you could customize the button renderer.
I use this renderer for a 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();
}
}
来源:https://stackoverflow.com/questions/6482443/how-to-avoid-repeatedly-click-a-button-in-a-form