2012-11-09 52 views
1

我遇到了a4j:commandButton的问题。我试图完成的是:a4j:commandButton onbegin事件触发表单加载

我有一个提交和取消按钮的形式。当用户点击提交按钮时,我希望禁用这两个按钮,并弹出一个要求用户确认的提示。一旦用户确认,如果有验证错误,我希望重新启用提交和取消按钮。我尝试了很多解决方案,但似乎没有任何工作。如果您有比以下更好的解决方案,请发布。我的问题属于下面的代码

     <h:panelGrid id="submitPanel" columns="2"> 
         <a4j:commandButton id="addRequestButton" render="addRequestButton,cancelButton" onbegin="#{addRequestBean.disableSubmitButton()}" styleClass="LoginButtonStyle" value="Submit" 
              disabled="#{addRequestBean.submitEnabled}" oncomplete="#{addRequestBean.enableSubmitButton()}"> 
          <rich:componentControl target="popup" operation="show" /> 
         </a4j:commandButton> 
         <h:commandButton id="cancelButton" styleClass="LoginButtonStyle" value="Cancel" action="main" disabled="#{addRequestBean.submitEnabled}"> 
         </h:commandButton> 
        </h:panelGrid> 
        <rich:popupPanel id="popup" modal="true" autosized="true" resizeable="false"> 
         <f:facet name="header"> 
          <h:outputText value="Verify"></h:outputText> 
         </f:facet> 
         <h:panelGrid id="popupgrid" columns="1"> 
          <h:outputText styleClass="labelFontStyle" escape="false" value="All changes are final. &lt;br /&gt;Do you wish to continue?"></h:outputText> 
          <h:outputLabel styleClass="labelFontStyle" value="" for=""> 
          </h:outputLabel> 
         </h:panelGrid> 
         <h:panelGrid columns="2"> 
          <h:commandButton styleClass="LoginButtonStyle" value="Ok" action="#{addRequestBean.saveRequest}" onclick="#{rich:component('popup')}.hide()"> 
          </h:commandButton> 
          <h:commandButton styleClass="LoginButtonStyle" value="Cancel" onclick="#{rich:component('popup')}.hide()"> 
          </h:commandButton> 
         </h:panelGrid> 
        </rich:popupPanel> 

ViewScoped Bean。

我想onbegin事件设置支持bean变量为禁用,并oncomplete方法重新启用它。麻烦的是onbegin事件在表单加载时触发,因此禁用了Submit和Cancel按钮。必须有我做错的事,如果没有,是否有解决方法?再次如果有比这更好的解决方案,请张贴。

感谢

回答

0

虽然我不认为需要显示模式弹出后禁用两个按钮(通过定义一个模式弹出意味着其他行动将有可能在该网页上,直到弹出被关闭或刷新页面),而你也没有说明是否要在默认情况下(这是你的代码表示反正禁用提交按钮),您可以通过

  1. 在包装纸两个按钮来实现它一个<a4j:outputPanel/>。这样,无论如何,这两个按钮都会被该页面上的任何ajax动作重新渲染。还有什么是action="main"做取消按钮?那不是工作。此外,您的<h:commandButton/>有选择意味着它不会触发一个Ajax请求的视图中的任何更新

    <a4j:outputPanel ajaxRendered="true" layout="block"> 
    <a4j:commandButton id="addRequestButton" styleClass="LoginButtonStyle" action = "#{addRequestBean.toggleSubmitButtonState}" value="Submit" disabled="#{addRequestBean.submitEnabled}" oncomplete="#{rich:component('popup')}.show()"/> 
    <h:commandButton id="cancelButton" styleClass="LoginButtonStyle" value="Cancel" action="main" disabled="#{addRequestBean.submitEnabled}"/> 
    </a4j:outputPanel> 
    
  2. 添加您的按钮切换按钮状态

    public void toggleSubmitButtonState(){ 
        this.submitEnabled = !submitEnabled;  
    
        } 
    
    的方法表达

    你真的不应该将两个按钮的状态绑定到相同的布尔值,因为语义上,他们做的不同的事情,但我会坚持你已经有这里

  3. 我建议你把那个弹出的表示它目前与所有其他组件共享,但是,我们再次处理您拥有的内容。再一次,你在这里单独选择<h:commandButton/>将确保你没有做任何Ajax。无论是添加<f:ajax/>或转换的按钮<a4j:commandButton/>

    <h:commandButton styleClass="LoginButtonStyle" value="Ok" action="#{addRequestBean.saveRequest}" oncomplete="#{rich:component('popup')}.hide()"/> 
        <h:commandButton styleClass="LoginButtonStyle" value="Cancel" onclick="#{rich:component('popup')}.hide()"/> <!-- This won't work as is, it will refresh the entire page instead of just firing the javascript event. Change to a4j--> 
    
  4. 一个<f:event/>事件处理程序添加到该网页,检查验证失败。

    和支持bean,我们将添加方法来检查验证失败

    public void checkValidationFailures(){ 
    
    FacesContext context = FacesContext.getCurrentInstance(); 
    if (context.isPostback() && context.isValidationFailed()){ //if the request is a postback and validation failed 
    
        this.submitEnabled = true;    
    
        } 
    } 
    

    这好像你的saveRequest方法中抛出一个ValidatorException才会工作。否则,您必须在方法中自己处理失败,并明确将submitEnabled的值设置为true,然后重新设置按钮。这就是为什么你可能应该将弹出框中的这些按钮转换为a4j