Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 698
A.Chakroun Avatar asked Feb 08 '23 01:02

A.Chakroun


1 Answers

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)" />
like image 113
BalusC Avatar answered Feb 11 '23 00:02

BalusC