Fares El Williams Fares El Williams - 3 months ago 24
jQuery Question

Show a modal when a link is trigger

I have a PHP process that lasts long enough generating an excel file, I would like to trigger a modal appears telling the user to wait a bit.

Here's my code

$('#export-excel').click(function(){
this.href = this.href + location.search;
$.ajax({
beforeSend: function() {
$('#myModal').show();
},
statusCode:{
500: function(){
//Error PHP
},
404: function(){
//Erreur dans ta route
},
200: function(){
$('#myModal').hide();
}
}
});
});


and the link is

<a id="export-excel" class="btn btn-sm btn-menu" href="{{ path('plateforme_reassort_generationexcel') }}" title="Exporte les articles filtrés au format Excel">Export Excel</a>


if someone got something for me that would be good !

Answer

jQuery Documentation: states usage as:

$('#myModal').modal()                      // initialized with defaults
$('#myModal').modal({ keyboard: false })   // initialized with no keyboard
$('#myModal').modal('show')                // initializes and invokes show immediately
$('#export-excel').click(function(){
        this.href = this.href + location.search;
        $.ajax({
            beforeSend: function() {
                $('#myModal').modal('show');
            },
            statusCode:{
                500: function(){
                    //Error PHP
                },
                404: function(){
                    //Erreur dans ta route
                },
                200: function(){
                    $('#myModal').modal('hide');
                }
            }
        });
    });

BASIC EXAMPLE:

$(function() {
  $(".custom-close").on('click', function() {
    $('#myModal').modal('hide');
  });
  setTimeout(function() {
    $('#myModal').modal('show');
    setTimeout(function() {
      $('#myModal').modal('hide');
    }, 2000)
  }, 500)
});
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">


        WILL CLOSE IN 2 secs....


      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

Comments