【问题标题】:How to execute commandLink'actionListener function first then only execute JavaScript prompt?如何先执行 commandLink'actionListener 函数然后只执行 JavaScript 提示?
【发布时间】:2012-10-25 11:23:40
【问题描述】:

基本上我有一个记录列表。每条记录(VO bean)都会附加一个警报标志alertFlag。当 alertFlag 为 true 时,会提示一个警告框。否则不会提示。

VO 豆:

@ManagedBean(name = "theVo")
@SessionScoped
public class TheVO {
   private boolean alertFlag;

   /** getter and setter **/
}

这是 XHTML 代码:

<t:dataTable var="theRecord" value="#{theVo.theList}">

  <t:column>
    <h:commandLink value="#{theRecord.recNo}" 
      actionListener="#{theEnquiry.doSetAlertFlag(theRecord.paramKey)}" 
      immediate="true" 
      action="#{theDetail.doShowDetailsPage(theRecord)}" 
      onclick="readAlertFlag(#{theEnquiry.alertFlag})"/>
    </h:commandLink>
  </t:column>

  ...
  ...
</t:dataTable>

这里是theEnquiry豆:

@ManagedBean(name = "theEnquiry")
@SessionScoped
public class Enquiry {

   private boolean alertFlag = false;

   /** getter and setter of alertFlag **/

   public void doSetAlertFlag(Integer theParamKey) {

      // check whether the alert flag is true or false.
      // if alert flag is true, otherwise set it false
   }
}

这是 JavaScript 代码:

function readAlertFlag(alertFlag) {

  if( alertFlag == "true" )
    alert('alertFlag is true');
  else
    alert('alertFlag is false');
}

我目前的情况是 JavaScript 警告框将始终首先执行,然后只有 doSetAlertFlag() 被执行。但我的期望是先执行doSetAlertFlag(),因为我希望在提示之前先完成检查。如果这不能在 JavaScript 提示符下完成,我愿意接受替代解决方案。

【问题讨论】:

    标签: javascript jsf-2 facelets managed-bean


    【解决方案1】:

    让 JSF 有条件地渲染脚本。将onclick 替换为&lt;h:outputScript rendered&gt;

    <h:commandLink ... />
    <h:outputScript rendered="#{theEnquiry.alertFlag}">showAlert()</h:outputScript>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多