Close sweetalert2 when you find the data

0

I would like the sweetalert2 to find the data to be modal.

But it does not return anything in .then

Here's my code:

swal({
            title: 'Auto close alert!',
            html: 'I will close in <strong></strong> seconds.',

            onOpen: () => {
                var ano = $('.slt-ano').val()
                swal.showLoading()
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
                    }
                })
               return  $.ajax({
                    url : '{{ route('medicao.getDadosToGrapsh') }}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        ano : ano
                    }

                }).done((r)=>{
                    return r
                })

            },

        }).then((result) => {
            console.log( result )
            if (
                // Read more about handling dismissals
                result.dismiss === swal.DismissReason.timer
            ) {
                console.log('I was closed by the timer')
            }
        })

How do I return the data in .then to close the modal?

    
asked by anonymous 18.07.2018 / 16:23

2 answers

0

Have you checked if something is coming in result ?

And .then means: What will be executed when closing, either by clicking the ok button or canceling, to close a modal you must call the hide method.

Treat Ajax success and error,

sucess: function(data){ /* essa função não trata especificamente o dado, ela apenas retorna ou não se o Ajax conseguiu fazer o envio/request da solicitação*/
    if(data == “exe”) {// se data for igual que vc espera ou deseja
        $(“idModal”).close(); // então fecha o modal ou .hide();
    }
}

See a FIDDLE for a Modal closure

    
18.07.2018 / 16:54
0

I found out!

If someone needs it, here is the code

$(document).ready(function(){
  alerta()
})

function alerta(){
  swal({
    title: 'Auto close alert!',
    html: 'I will close in <strong></strong> seconds.',
      onOpen: () => {
         swal.showLoading()
      }
   })

  setTimeout(function(){
    console.log('Terminou o processo, fechar sweetalert2')
    swal.close()
  }, 3000);
  

}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.25.6/sweetalert2.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.25.6/sweetalert2.all.min.js"></script>
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>

What did I do?

I left sweetalert2 loading while I was fetching information

swal({
    title: 'Auto close alert!',
    html: 'I will close in <strong></strong> seconds.',
      onOpen: () => {
         swal.showLoading()
      }
   })

Get the information, I had it closed

swal.close()
    
18.07.2018 / 19:10