Close sweetalert2 when finding the data

Asked

Viewed 852 times

0

I wish that when finding the data the sweetalert2 closed the modal.

But it returns nothing in . then

Here is 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?

2 answers

2

I found out!

In case anyone needs it, here’s 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>
<script src="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 I did?

I left sweetalert2 charging while I was doing the process of getting the information

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

You got the information, I told you to shut it down

swal.close()

0

Have you checked to see if anything’s coming result?

And .then that is: What will be executed When closing, either by clicking the ok or cancel button, to close a modal you must call the Hide method.

Handle 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 of a Modal closure

  • Yes. No result only returns the object "dismiss" : true

  • And in this case I’m using the sweetalert

Browser other questions tagged

You are not signed in. Login or sign up in order to post.