简体   繁体   English

在jsf 2.0(myfaces)中调用ajax,在渲染完成之前调用ajax标记中的一个Javascript函数

[英]ajax call in jsf 2.0 (myfaces), the onevent Javascript function in the ajax tag gets called before the rendering is complete

This is my first time asking a question on a forum, since usually my questions have already been asked and answered. 这是我第一次在论坛上提问,因为我的问题通常已被提问和回答。 I haven't found an answer to this problem that works for me, so here goes: 我还没有找到适合我的这个问题的答案,所以这里有:

I'm making an Ajax call in JSF 2.0 like this: 我在JSF 2.0中进行Ajax调用,如下所示:

< f :ajax listener="#{myReferenceController.clearRequiredReferenceNumber}"
    onevent="resetFocus" execute="@form"
    render=":resultsForm:ResultsDisplay" />

Everything in the listener works perfectly, and the data is then rendered as expected in the data table that I have in my .xhtml page. 侦听器中的所有内容都能正常工作,然后在我的.xhtml页面中的数据表中按预期呈现数据。 The problem is that the Javascript that I call in the onevent seems to be getting called before the rendering is complete, and therefore the process of resetting the focus to a column in my datatable doesn't work, since the datatable is removed and then re-added to the DOM when the Ajax is finished re-rendering. 问题是,我在调用JavaScript onevent似乎得到调用之前渲染完成的,因此在我的焦点重置为塔的处理datatable不起作用,因为该datatable被删除,然后重新 - 当Ajax完成重新渲染时添加到DOM。

I'm looking in my Javascript for the status of "success", in hopes that at this point, the rendering would have completed. 我正在查看我的Javascript中的“成功”状态,希望此时渲染已经完成。 Alas, this is not the case, and my getElementById (actually dojo.byId ) is not finding the element in the datatable. 唉,事实并非如此,我的getElementById (实际上是dojo.byId )没有找到数据表中的元素。 I know my Javascript function works under normal circumstances, since I'm calling this same function in a situation where there is not an Ajax call, and everything works perfectly there. 我知道我的Javascript函数在正常情况下工作,因为我在没有Ajax调用的情况下调用同一个函数,并且一切都在那里完美运行。

If I could just avoid rendering the cell in the table that I'm trying to set focus to, that would be great, but my listener is making changes to this cell in the ajax call. 如果我可以避免渲染我正试图设置焦点的表格中的单元格,那将是很好的,但我的听众正在ajax调用中对此单元格进行更改。 I'm at my wits end, so any ideas on this would be very appreciated. 我在我的智慧结束,所以对此的任何想法都将非常感激。

-- in response to Balusc (heard good things about you, btw) - 回应Balusc(听到关于你的好消息,顺便说一句)

Hmm, I was actually on the right track I think, but still seem to be having troubles. 嗯,我认为我实际上是在正确的轨道上,但似乎仍然有麻烦。 I am checking for "success" and still even in success, am not able to set the focus here. 我正在检查“成功”,甚至还是成功,我无法在这里设定重点。 Here's my Javascript function that is checking for "success": This function works in another situation, where it's not attached to an Ajax event. 这是我检查“成功”的Javascript函数:此函数在另一种情况下工作,它没有附加到Ajax事件。

function resetFocus(data) {
    var theRow = dojo.byId("resultsForm:selectedRow").value;               
    if (data.status == "success") {
        dojo.query('[widgetId]',dojo.byId('theResultsDataTable'))
            .forEach(function(node) {
                var widget = dijit.byNode(node);
                var theId = widget.attr("id")                                             
                if (theId.indexOf(':' + theRow + ':') != -1) {
                    if (theId.indexOf('theOrppoNum') != -1) {
                        widget.focus();
                        widget.attr("isFocused",true);
                    }
                }
            });
    }
}

The function attached to the onevent attribute will actually be invoked three times. 连接到该功能onevent属性实际上将被调用三次 One time before the ajax request is been sent, one time after the ajax response is been arrived and one time when the HTML DOM is successfully updated. 在发送ajax请求之前,有一次是在ajax响应到达之后,一次是在成功更新HTML DOM之后。 You should be checking the status property of the given data argument for that. 您应该检查给定数据参数的status属性。

function onEventFunction(data) {
    var status = data.status; // Can be "begin", "complete" or "success".

    switch (status) {
        case "begin": // Before the ajax request is sent.
            // ...
            break;

        case "complete": // After the ajax response is arrived.
            // ...
            break;

        case "success": // After update of HTML DOM based on ajax response..
            // ...
            break;
    }
}

In your particular case, you thus just need to add a check if the status is success . 在您的特定情况下,您只需要添加一个检查状态是否success

function resetFocus(data) {
    if (data.status == "success") {
        // Do your job here.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM