Callback executes JavaScript on an event of a component instead of an AJAX request. Like p:ajax it is attached to an event of the component, the JavaScript gets source, event, params and ext. Returning false stops the following behaviors of the event, e.g. a p:ajax.
<p:growl id="growl" showDetail="true"/>
<h:form>
<div class="card">
<h5 class="mt-0">Basic</h5>
<p:selectOneMenu id="city" widgetVar="cityMenu" styleClass="mr-3">
<f:selectItem itemLabel="Berlin" itemValue="Berlin"/>
<f:selectItem itemLabel="Paris" itemValue="Paris"/>
<f:selectItem itemLabel="Rome" itemValue="Rome"/>
<p:callback event="change"
execute="document.getElementById('basicOutput').textContent = event + ' of ' + source + ': ' + PF('cityMenu').getSelectedValue();"/>
</p:selectOneMenu>
<span id="basicOutput"></span>
</div>
<div class="card">
<h5 class="mt-0">Event Parameters</h5>
<p class="mt-0">The <i>slideEnd</i> event passes the value of the slider in <i>params</i>.</p>
<p:inputText id="sliderInput" styleClass="mb-3"/>
<p:slider for="sliderInput" styleClass="w-20rem">
<p:callback event="slideEnd"
execute="document.getElementById('sliderOutput').textContent = 'Slide ended at ' + params[0].value;"/>
</p:slider>
<p id="sliderOutput" class="mb-0"></p>
</div>
<div class="card">
<h5 class="mt-0">Before p:ajax</h5>
<p class="mt-0">The AJAX request is only sent if the callback does not return <i>false</i>.</p>
<p:toggleSwitch value="#{callbackView.active}">
<p:callback event="change" execute="return confirm('Send the AJAX request?');"/>
<p:ajax listener="#{callbackView.onChange}" update="growl"/>
</p:toggleSwitch>
</div>
</h:form>
package org.primefaces.showcase.view.ajax;
import java.io.Serializable;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
@Named
@ViewScoped
public class CallbackView implements Serializable {
private boolean active;
public void onChange() {
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "AJAX request", "Active: " + active));
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}