function checkPromoId(promoid) { return promoid.match(/A[1-5]/) != null; } function editRow() { var row = $(this).parent().parent(); switch ($("title").html()) { case "Rentrée ISEN Brest - Documents - Administration": var rang = $(row.children()[0]); var promo = $(row.children()[1]); var rangValeur = rang.html(); var promoValeur = $(promo[0]).attr("class"); var promos = $("#promo").children(); var str = '"; promo.html(str); $(this).addClass("fa-check"); $(this).removeClass("fa-pencil"); $(this).off("click"); $(this).on("click", function () { rangValeur = $(rang.children()[0]).val(); rang.html(rangValeur); promoValeur = promo.children().val(); promo.addClass(promoValeur); var promoName = ""; for (var i = 0; i < promos.length; i++) { if (promos[i].value === promoValeur) { promoName = promos[i].text; } } promo.html(promoName); $(this).addClass("fa-pencil"); $(this).removeClass("fa-check"); $.ajax({ method: "PUT", url: "document/" + row[0].id, data: "rang=" + rangValeur + "&promo=" + promoValeur, processData: false, contentType: false }); $(this).off("click"); $(this).on("click", editRow); }); break; case "Rentrée ISEN Brest - Promotions - Administration": var promoName = $(row.children()[1]); var name = $(promoName[0]).text(); $(promoName).html( '' ); $(this).addClass("fa-check"); $(this).removeClass("fa-pencil"); $(this).off("click"); $(this).on("click", function () { name = $(promoName.children()[0]).val(); $(promoName).html(name); $(this).addClass("fa-pencil"); $(this).removeClass("fa-check"); $.ajax({ method: "PUT", url: "promo/" + row[0].id, data: "libelle=" + name, processData: false, contentType: false }); $(this).off("click"); $(this).on("click", editRow); }); break; } } $('.fa-pencil').on("click", editRow); $('.fa-trash-o').on("click", function () { var row = $(this).parent().parent(); var url = ""; switch($("title").html()) { case "Documents": url = "document/"+row[0].id; break; case "Promotions": url = "promo/"+row[0].id; } $.ajax({ method: "DELETE", url: url, processData: false, contentType: false }); row.remove(); }); $('#addForm').submit(function(e) { e.preventDefault(); var data = new FormData(); switch ($("title").html()) { case "Documents": $("#helpBlock2").remove(); $(".has-error").removeClass("has-error"); if (!$("#rang").val().length) { var block = $("#rang").parent(); block.addClass("has-error"); block.html(block.html()+'Vous devez spécifier un rang.'); } else if (!$("#libelle").val().length) { var block = $("#libelle").parent(); block.addClass("has-error"); block.html(block.html()+'Vous devez spécifier un libellé.'); } else if (!$("#file").val().length) { var block = $("#file").parent(); block.addClass("has-error"); block.html(block.html()+'Vous devez sélectionner un fichier.'); } else { data.append("document", $("#file")[0].files[0]); data.append("promo", $("#promo").val()); data.append("rang", $("#rang").val()); data.append("libelle", $("#libelle").val()); $.ajax({ method: "POST", url: "document", data: data, dataType: 'json', processData: false, contentType: false, complete: function (result) { result = result.responseJSON; var promoName = ""; var promoValeur = $("#promo").val(); for (var i = 0; i < $("#promo").children().length; i++) { if ($("#promo").children()[i].value === promoValeur) { promoName = $("#promo").children()[i].text; } } $("table").append('