Pass source HTML element to oncomplete function of PrimeFaces p:ajax

主宰稳场 提交于 2019-12-12 17:24:32

问题


I would like to pass the source HTML element as argument to JavaScript callback function of <p:ajax oncomplete>. I tried passing this which usually works in onclick and such:

<p:ajax ... oncomplete="callbackFunction(this)" />

It's not working. It seems to be a different object.

How can I achieve this?


回答1:


In context of oncomplete, the this represents the PrimeFaces ajax object which actually contains a lot of information. You could easily have figured out it by inspecting the object in debugger, or by passing to console.log which automatically pretty prints JS objects.

function callbackFunction(arg) {
    console.log(arg);
}

In case of ..

<h:form id="formId">
    <h:commandLink id="linkId" value="test">
        <p:ajax oncomplete="callbackFunction(this)" />
    </h:commandLink>
</h:form>

.. it'll look something like this in console (press F12 to get there):

accepts: Object
async: true
beforeSend: (w,i)
cache: false
complete: (w,i)
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
contents: Object
converters: Object
crossDomain: false
data: "javax.faces.partial.ajax=true&javax.faces.source=formId%3AlinkId&javax.faces.partial.execute=formId%3AlinkId&javax.faces.behavior.event=action&javax.faces.partial.event=click&formId=formId&javax.faces.ViewState=-4870787666399983047%3A-2006040112896840046"
dataType: "xml"
dataTypes: Array[2]
error: (x,i,w)
flatOptions: Object
global: false
hasContent: true
isLocal: false
jsonp: "callback"
jsonpCallback: ()
portletForms: null
processData: true
responseFields: Object
source: a#formId:linkId
success: (x,i,y)
type: "POST"
url: "/playground/test"
xhr: bD()
__proto__: Object

If you look closer, the source property is what you ultimately need.

So, just alter your call accordingly:

<p:ajax ... oncomplete="callbackFunction(this.source)" />


来源:https://stackoverflow.com/questions/34780263/pass-source-html-element-to-oncomplete-function-of-primefaces-pajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!