I need to show in another tab
the generate Report with bean method (bean.generateReporte())
. And show a message when the Report is not possible to be generated (not data is found according to criteria).
my first code in the .xhtml was:
<p:commandButton id="printDeliveryList" label="Generate Delivery List" styleClass="btn-success"
style="float:left" icon="fa fa-print"
action="#{bean.generateReporte()}"
onclick="this.form.target = '_blank'"
ajax="false"
/>
I was reading this question How to prevent commandButton from executing action when onclick method return false and this question How to call JS function on click of JSF button and block page reload/navigation? and this post https://www.adictosaltrabajo.com/2011/09/14/primefaces-client-side-ajax-events/ And this documentation https://www.primefaces.org/showcase/ui/misc/requestContext.xhtml
I was thinking in onsuccess
event.
Here my code later....
In the bean
side I have:
public void generateReporte() {
// blabla
if (success) {
addGeneratedToCallback(true);
} else {
addGeneratedToCallback(false);
}
}
private void addGeneratedToCallback(boolean value) {
PrimeFaces.current().ajax().addCallbackParam("generated", value);
}
In the .xhtml
side
<script type="text/javascript">
function handleComplete(xhr, status, args) {
if (args.validationFailed) {
PrimeFaces.debug("Validation Failed");
} else {
if (args.generated) {
// open a new tab in the browser
//window.open(this.form.target = '_blank');
form.target = '_blank';
} else {
// show message
}
}
}
</script>
<p:commandButton id="printListaEntrega" label="Generar Lista de Entrega" styleClass="btn-success"
style="float:left" icon="fa fa-print"
action="#{bean.generateReporte()}"
ajax="false"
oncomplete="handleComplete(xhr, status, args)"
/>
The Result of my code
With ajax="false"
I got this when the generation of Report is not possible.
When I remove ajax="false"
from the p:commandButton
and the report is generated I got. (Not open the new tab in browser And Can't download the report)
Is it possible to set contitional ajax="bean.someMethodOrProperty"
?