在PrimeFaces中的某些组件更新时自动调用javascript

Automatically call javascript on update of some component in PrimeFaces

本文关键字:调用 javascript 更新 组件 PrimeFaces      更新时间:2023-09-26

Primefaces 5

我有以下例子:

<script>
function init () { 
  $("#myspan").doSomething;
}
</script>
<h:form rendered="some condition">
  <span id="myspan" />
</h:form>

如何在每次更新表单时自动调用init()?

要重新运行脚本,只需重新呈现调用它的标记即可。假设表单在"更新"时都会重新呈现,则如下所示:

<script>
function init () { 
  $("#myspan").doSomething;
}
</script>
<h:form rendered="some condition">
  <script type="text/javascript">init()</script>
  <span id="myspan" />
</h:form>

替代方案是使用<p:remoteCommand autoRun='true'>

<script>
function init () { 
  $("#myspan").doSomething;
}
</script>
<h:form rendered="some condition">
  <span id="myspan" />
  <p:remoteCommand autoRun="true" oncomplete="init();" />
</h:form>

在Ali的解决方案中,<script>标签被较早地调用为具有<p:remoteComand>的解决方案。如果输出已经显示给用户,则调用remoteCommand。