testajax.html.twig 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. {% extends "@App/videotheque/base.html.twig" %}
  2. {% block title %}Ajax{% endblock %}
  3. {% block titre %}Ajax{% endblock %}
  4. {% block body %}
  5. <form method="post" action="">
  6. <input type="text" id="mot_cle">
  7. </form>
  8. <div id="results">
  9. <ul class="result">
  10. </ul>
  11. </div>
  12. {% endblock %}
  13. {% block javascripts %}
  14. <script src="{{ asset('bundles/app/assets/oXHR.js') }}"></script>
  15. <script>
  16. $(document).ready( function() {
  17. if (!jQuery('body').hasClass('searching')) {
  18. $('#mot_cle').keyup(function (e) {
  19. var contenu = $('#mot_cle').val();
  20. $('#results').html('');
  21. if (contenu.length > 2) {
  22. jQuery('#results').addClass('miniload');
  23. jQuery('#results').append('<div id="chargement">Chargement...<div id="spinner" class="spinner"></div></div>');
  24. setTimeout(function() {
  25. $.ajax({
  26. type: 'POST',
  27. url: "{{ path('ajax_requete') }}",
  28. data: 'mot_cle=' + contenu,
  29. success: function (data) {
  30. jQuery('body').addClass('searching');
  31. $('#results').html('');
  32. jQuery('#chargement').remove();
  33. parseJson(data);
  34. return;
  35. },
  36. complete : function(){
  37. jQuery('#chargement').remove();
  38. return jQuery('body').removeClass('searching');
  39. }
  40. });
  41. }, 1000);
  42. }
  43. });
  44. }
  45. });
  46. function parseJson(data) {
  47. console.log (data);
  48. let text = "";
  49. for (i = 0; i < data.length; i++)
  50. {
  51. let realisateur = data[i];
  52. console.log("OBJECT JSON %o", realisateur);
  53. let {id, nom, prenom} = JSON.parse(realisateur);
  54. text += prenom + " " + nom + '\n';
  55. }
  56. $('#results').html(text);
  57. }
  58. </script>
  59. {% endblock %}