将 html 元素传递给 a4j:jsFunction 的 oncomplete 函数

Passing html element to oncomplete function of a4j:jsFunction

我需要将 <a4j:jsFunction> 的调用 html 元素传递给它的 oncomplete 函数。我看到了 this 的回答,但我认为将 html 元素(我想是作为一个字符串?)传递给 bean 并返回是非常优雅的,我认为它有点混乱。

这是我的代码

<td class="col-md-2 clickable text-right"
        onclick="orderBy('getAmount','DESC')">
    #{msg.label_amount}
</td>

<a4j:jsFunction name="orderBy" action="#{backingBeanRef['orderBy']}"
        oncomplete="afterOrderBy()" 
        render="list_form" >
    <a4j:param name="ref" assignTo="#{backingBeanRef['orderByMethodName']}"/>                               
    <a4j:param name="ref2" assignTo="#{backingBeanRef['orderBySortOrder']}"/>
</a4j:jsFunction>

问题

有没有办法将 html 元素传递给 a4j:jsFunction,并让那个元素将它传递给它的 oncomplete 回调函数,而不发送 html元素到bean 和返回?

理想的解决方案 是向调用端添加一个参数,如 orderBy(... , this),并将相应的参数添加到 jsFunction,如 <a4j:param name="element"/> :

<td class="col-md-2 clickable text-right"
        onclick="orderBy('getAmount','DESC',this)">
    #{msg.label_amount}
</td>

<a4j:jsFunction name="orderBy" action="#{backingBeanRef['orderBy']}"
                    oncomplete="afterOrderBy(element)" 
                    render="list_form" >
    <a4j:param name="ref" assignTo="#{backingBeanRef['orderByMethodName']}"/>                               
    <a4j:param name="ref2" assignTo="#{backingBeanRef['orderBySortOrder']}"/>
    <a4j:param name="element"/>
</a4j:jsFunction>

但是没用。我在 js 函数的第一行有一个断点,它没有被调用。

我刚刚将 ..,this) 添加到 数量 列(我显示的唯一一列),所以如果我单击其他列(不没有 ..,this)) 我的 js 函数确实被调用,只有参数未定义。

那么,可以这样做吗?

编辑

如果我尝试不添加任何 <a4j:param>,并按照 的指示执行 oncomplete="afterOrderBy(this)",发送到 js 函数的元素是一个跨度(当我 debug/log 它来安慰,它没有.sourcechild,而是

... outerHTML: "<span id=\"sgfrmId:j_idt601\" style=\"display: none;\"></span>" ...

This answer 还指出了一些类似但不完全相同的方法。

编辑2

只需阅读

的文档

[...] It is not recommended to use use keyword this inside the EL-expression, because it will not always point to the component where Ajax request was initiated.[...]

所以 oncomplete="afterOrderBy(this)" 是错误的。

提前致谢。

a4j:jsFunction 仅用于触发 ajax 请求,oncomplete 作为服务器响应的一部分执行(即不在函数的上下文中),只需将元素引用保存到全局变量并让 oncomplete 代码使用该变量。

例如

<td onclick="sourceElement=this; orderBy();">…</td>

afterOrderBy = function() {
     // do something with sourceElement …
}