Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var t;//data table
  2.  
  3.     $("#crea-form").submit(function (event) {
  4.         // désactive bouton création
  5.         disableCreaButton(true);
  6.         // empêche le comportement par défaut du form càd l'envoi direct du formulaire
  7.         event.preventDefault();
  8.         creaViaAjax();
  9.     });
  10.  
  11.  
  12.     $("#rechid-form").submit(function (event) {
  13.  
  14.         // désactive bouton recherche
  15.         disableSearchButton(true);
  16.         // empêche le comportement par défaut du form càd l'envoi direct du formulaire
  17.         event.preventDefault();
  18.         rechViaAjaxId();
  19.     });
  20.  
  21.  
  22.     $('#edit-form').submit(function (event) {
  23.         event.preventDefault();
  24.         var cours = {};
  25.         cours["idcours"] = $("#id").val();
  26.         cours["matiere"] = $("#matiere").val();
  27.         cours["heures"] = $("#heures").val();
  28.         sauveViaAjax(cours);
  29.         document.location.reload(true);
  30.     });
  31.  
  32.     var ficlangue = $("#container").data("langue");
  33.     t = $('#tabcours').DataTable({
  34.         "language": {
  35.          // "url": "https://cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json"
  36.             "url": ficlangue
  37.             },
  38.         columns: [
  39.             {data: "idcours"},
  40.             {data: "matiere"},
  41.             {data: "heures"}
  42.  
  43.         ]
  44.     });  //pour dataTables
  45.  
  46.     $('#tabcours tbody').on('click', 'tr', function () {
  47.         if ($(this).hasClass('selected')) {
  48.             $(this).removeClass('selected');
  49.  
  50.         } else {
  51.             t.$('tr.selected').removeClass('selected');
  52.             $(this).addClass('selected');
  53.  
  54.             var data = t.row('.selected').data().idcours;
  55.             $('#id').val(data);
  56.             data = t.row('.selected').data().matiere;
  57.             $('#matiere').val(data);
  58.             data = t.row('.selected').data().heures;
  59.             $('#heures').val(data);
  60.         }
  61.        
  62.     });
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. function sauveViaAjax(cours) {
  71.     $.ajax({
  72.         type: "POST",
  73.         contentType: "application/json",
  74.         url: "../../coursrest/updatecours",
  75.         data: JSON.stringify(cours),
  76.         dataType: 'json',
  77.         timeout: 100000,
  78.         success : function (data) {
  79.             alert("sauvegarde effectuée");
  80.         },
  81.        error : function (e) {
  82.             alert("erreur de sauvegarde : " + e);
  83.         }
  84.      });
  85. }
  86.  
  87.  
  88. function rechAllViaAjax() {
  89.     $.ajax({
  90.         type: "POST",
  91.         contentType: "application/json",
  92.         url: "../../coursrest/touscours",
  93.         dataType: 'json',
  94.         timeout: 100000,
  95.         success : function (data) {
  96.             console.log("SUCCESS: ", data);
  97.             displayrechall(data);
  98.          },
  99.         error: function (e) {
  100.             alert("aucun cours trouvé");
  101.             $('#result').html("");
  102.         }
  103.        
  104.     });
  105. }
  106.  
  107. function rechViaAjaxId() {
  108.  
  109.     var rech = {};
  110.     rech["idcours"] = $("#idgest").val();
  111.     $.ajax({
  112.         type: "POST",
  113.         contentType: "application/json",
  114.         url: "../../coursrest/rechid",
  115.         data: JSON.stringify(rech),
  116.         dataType: 'json',
  117.         timeout: 100000,
  118.         success: function (data) {
  119.               console.log("SUCCESS: ", data);
  120.              displayRechId(data);
  121.               disableSearchButton(false);
  122.             },
  123.        error: function (e) {
  124.            alert("aucun cours trouvé");
  125.             $('#resultcrea').html("");
  126.             disableSearchButton(false);
  127.         }
  128.       });
  129. }
  130.  
  131. function creaViaAjax() {
  132.     var cours = {};
  133.     cours["matiere"] = $("#matiere").val();
  134.     cours["heures"] = $("#heures").val();
  135.     $.ajax({
  136.         type: "POST",
  137.         contentType: "application/json",
  138.         url: "../../coursrest/creacours",
  139.         data: JSON.stringify(cours),
  140.         dataType: 'json',
  141.         timeout: 100000,
  142.         success : function (data) {
  143.             alert("cours créé");
  144.             console.log("SUCCESS: ", data);
  145.             displaycrea(data);
  146.              disableCreaButton(false);
  147.         },
  148.         error: function (e) {
  149.             alert("erreur de création : " + e);
  150.             $('#resultcrea').html("");
  151.              disableCreaButton(false);
  152.         }
  153.      
  154.     });
  155.  
  156. }
  157.  
  158. function disableSearchButton(flag) {
  159.     $("#btn-rech").prop("disabled", flag);
  160. }
  161.  
  162. function disableCreaButton(flag) {
  163.     $("#btn-crea").prop("disabled", flag);
  164. }
  165.  
  166.  
  167. function displaycrea(data) {
  168.     var htmlrow = "";
  169.     htmlrow += "<tr>" +
  170.             "<td>" + data.idcours + "</td>" +
  171.             "<td>" + data.matiere + "</td>" +
  172.             "<td>" + data.heures + "</td>" +
  173.             "</tr>";
  174.  
  175.     $('#resultcrea').html(htmlrow);
  176. }
  177.  
  178. function displayRechId(data) {
  179.     $('#id').val(data.idcours);
  180.     $('#matiere').val(data.matiere);
  181.     $('#heures').val(data.heures);
  182. }
  183. function displayrechall(data) {
  184.     t.clear().draw(); //pour dataTables-efface valeurs actuelles
  185.     console.log("debut");
  186.     $.each(data, function (cle, valeur) {
  187.         t.row.add(valeur).draw(false);
  188.         console.log(valeur);
  189.    });
  190.  
  191. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement