Sweet alert problem in JSF


I am trying to integrate the sweet alert plugin in a boto in JSF to show a presentable confirmation window, the problem is that pressing the button opens the sweet alert but still executes the action of the controller without taking into account the confirmation and refreshes the page, that is to say that the sweet alert if it leaves, but refreshes the page and therefore it closes.

This is the code I use in the javascipt

function dialog() {
                title: "Are you sure?",
                text: "Your will not be able to recover this imaginary file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete it!",
                cancelButtonText: "No, cancel plx!",
                closeOnConfirm: false,
                closeOnCancel: false},
            function (isConfirm) {
                if (isConfirm) {
                    swal("Deleted!", "Your imaginary file has been deleted.", "success");
                    return true;
                } else {
                    swal("Cancelled", "Your imaginary file is safe :)", "error");
                    return false;


And this is the code on the JSF page

<h:commandLink id="boton" onclick="return dialog()" action="#{productosController.refrescar()}">
                    <i class="fa fa-pencil"></i>                         

Someone who can give me your help?

asked by JADER FELIPE DÍAZ SABOYÁ 10.12.2018 в 21:16

1 answer


Try as follows: When you create the swal you can put buttons that have a value, then capturing that value you can make a switch and make the option associated with the value eg accept, cancel. I leave an example made in html in case it serves as a guide:

<div id="uno">Prueba</div>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
swal("Prueba SweetAlert", {
  buttons: {
aceptar: true,
cancelar: true
.then((value) => {
  switch (value) {
case "cancelar":
   swal("SE CANCELA LA ACCION", "Cancelado", "error");
case "aceptar":
  $('#uno').html("TEXTO MODIFICADO")
  swal("SE ACEPTA LA ACCION", "Se cambio el texto del div a TEXTO MODIFICADO", "success");
answered by 10.12.2018 в 21:46