oncomplete方法中的PrimeFaces属性未更新

PrimeFaces Attribute in oncomplete method not updating

本文关键字:属性 更新 PrimeFaces 方法 oncomplete      更新时间:2023-12-14

我有这个代码:

<div id="visibleButtons">
    <p:commandButton id="hint" action="#{tutorSession.hintRequest}" value="Hint Please" update="@parent" oncomplete="playNextHint(#{tutorSession.giveWarning});">
    </p:commandButton>
    <p:commandButton id="newType" action="#{tutorSession.newPathRequest}" value="Something Else" update="@parent" oncomplete="playNextHint(#{tutorSession.giveWarning});">
    </p:commandButton>
    #{tutorSession.giveWarning}<!-- I will remove this line once the buttons are working -->
</div>

现在的情况是,javascript函数playNextHint使用上一次调用中的giveWarning值执行,而不是当前调用。我检查了服务器是否返回了正确的值;事实上,我在底部添加了一点文本输出,以验证是否返回了正确的值,它总是以我期望的方式显示,但javascript仍然使用旧值。

对可能发生的事情有什么想法吗?

javascript方法,如果它是相关的:

function playNextHint(giveWarning){
    alert(giveWarning);
}

提前谢谢。

编辑:我找到了一个解决办法,使用一个带值的隐藏字段,然后在javascript中获取该字段的值,但这很混乱,必须有更好的方法,因为我将为这个web应用程序做很多这样的事情。

编辑2:对不起,我留下了一些代码用于演示,这似乎让人们感到困惑。首先,我不想显示giveWarning。这只是为了让我看到它得到了正确的价值。我把它留在里面,这样人们就可以看到正确的价值观在哪里出现,而不是在哪里没有。

编辑3:当按下按钮时,我想做的是在服务器上计算giveWarning的新值(该部分有效),然后将其发送到javascript函数,以根据它进行逻辑处理(警报是一个占位符,用于测试)。问题是,javascript函数得到的值似乎总是比它应该得到的值落后一次,尽管当我只是将值发送到屏幕(div的最后一行)时,它显示了它应该得到什么。抱歉所有的编辑;就像我说的,这是我第一次发帖。

我认为您只是采取了不恰当的方法。我刚刚制作了一个新的<outputLabel>,并给它一个id,以便在javascript方法中使用它,并添加了update="label"来更新标签,并消除向该js方法传递任何参数。你可以查看这个

.xhtml

<h:form id="myForm"> 
      <p:commandButton id="hint" action="#{tutorSession.hintRequest}" value="Hint Please" update="label" oncomplete="playNextHint();">
      </p:commandButton>
      <p:commandButton id="newType" action="#{tutorSession.newPathRequest}" value="Something Else"  update="label" oncomplete="playNextHint();">
      </p:commandButton>
      <p:commandButton id="skip" action="#{tutorSession.giveUp}" value="Skip">
      </p:commandButton>
      <h:outputLabel value="#{tutorSession.giveWarning}" id="label" />
</h:form>

和您的js功能

function playNextHint(){
     alert($('#myForm'':label').text());
}

以及在您的ManagedBean tutorSession

    // other stuff here
    private String giveWarning;
    // setters / getters for giveWarning
    public void hintRequest() {
        setGiveWarning("hint");
    }
    public void newPathRequest() {
        setGiveWarning("newPath");
    }

我知道这是一个老问题,但我遇到了同样的问题,并使用单独的remoteCommand:解决了它

<p:remoteCommand name="playNextHintComplete"
                 oncomplete="playNextHint(#{tutorSession.giveWarning})"/>
<p:commandButton value="Hint Please"
                 action="#{tutorSession.hintRequest}"
                 process="@form"
                 update="@form"
                 oncomplete="playNextHintComplete()"/>