如何在表单reRender之后重新执行javascript函数? [英] How to re-execute javascript function after form reRender?

查看:170
本文介绍了如何在表单reRender之后重新执行javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表单重新呈现后重新执行javascript。简单地说,在XHTML内容之后放入javascript将无法帮助,因为它是部分请求。另外,我不能使用onComplete作为我重新渲染表单的commandButton是在几个地方使用的JSF组件。我需要在本地获得修复。

I need to re-execute javascript just after form gets re-rendered. Simply, putting in javascript after XHTML content won't help since its a partial request. Also, I cannot use "onComplete" as the commandButton from which I am re-rendering form is in JSF component used at several places. I need to get the fix in locally.

有什么办法吗?我想知道f:ajax在这种情况下是否会有所帮助。

Is there any way? I was wondering whether f:ajax would help in this case.

如果有人知道的话,请告诉我。

Please let me know, if anyone has clue about it.

推荐答案

最简单的方法是将JS函数调用放入要更新的组件本身。

Simplest way is to just put the JS function call in the to-be-updated component itself.

<h:form>
    ...
    <h:commandButton value="submit"><f:ajax render="@form" /></h:commandButton>
    <h:outputScript>someFunction();</h:outputScript>
</h:form>

这种方式在页面加载时执行,如果表单由ajax更新。

This way it's executed as the page loads and also if the form get updated by ajax.

至于< f:ajax> 本身,你也可以使用它的 onevent 属性。

As to the <f:ajax> itself, you could also use its onevent attribute.

<f:ajax ... onevent="handleAjax" />

with

function handleAjax(data) {
    var status = data.status;

    switch(status) {
        case "begin":
            // This is invoked right before ajax request is sent.
            break;

        case "complete":
            // This is invoked right after ajax response is returned.
            break;

        case "success":
            // This is invoked right after successful processing of ajax response and update of HTML DOM.
            someFunction();
            break;
    }
}

您可以通过<$ c $添加全局挂钩c> jsf.ajax.addOnEvent()这样您就不需要在 onevent 属性中指定它>< f:ajax> 如果功能要求适用于每个ajax请求。

You could add a global hook by jsf.ajax.addOnEvent() so that you don't need to specify it in every single onevent attribute of <f:ajax> if the functional requirement applies to every ajax request.

jsf.ajax.addOnEvent(handleAjax);

另一种方法是使用 OmniFaces JSF实用程序库,提供 < h: onloadScript> 正是出于此目的。您可以根据需要将其放在头部。

An alternative is to use the OmniFaces JSF utility library which offers the <h:onloadScript> for exactly this purpose. You can place it in the head so you want.

<h:onloadScript>someFunction();</h:onloadScript>

这会自动在视图上的每个ajax请求中重新执行,这样您就不需要了将其复制到视图中可以进行ajax更新的多个单独位置,或者在每个< f:ajax render> 中重复其父ID。

This is automatically re-excuted on every single ajax request on the view so that you don't need to copy it over multiple individual places in the view which can be ajax-updated, or to repeat its parent ID in every <f:ajax render>.

这篇关于如何在表单reRender之后重新执行javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆