Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $("body").on("click", ".modal-show", function(event) {
  2.     event.preventDefault();
  3.  
  4.     var me = $(this),
  5.         url = me.attr("href"),
  6.         title = me.attr("title");
  7.  
  8.     $("#modal-title").text(title);
  9.     $("#modal-btn-save")
  10.         .removeClass("hide")
  11.         .text(me.hasClass("edit") ? "Ubah" : "Simpan");
  12.  
  13.     $.ajax({
  14.         url: url,
  15.         dataType: "html",
  16.         success: function(response) {
  17.             $("#modal-body").html(response);
  18.         }
  19.     });
  20.  
  21.     $("#modal").modal("show");
  22. });
  23.  
  24. $("#modal-btn-save").click(function(event) {
  25.     event.preventDefault();
  26.  
  27.     var form = $("#modal-body form"),
  28.         url = form.attr("action"),
  29.         method = $("input[name=_method]").val() == undefined ? "POST" : "PUT";
  30.  
  31.     form.find(".help-block").remove();
  32.     form.find(".form-group").removeClass("has-error");
  33.  
  34.     $.ajax({
  35.         url: url,
  36.         method: method,
  37.         data: form.serialize(),
  38.         success: function(response) {
  39.             form.trigger("reset");
  40.             $("#modal").modal("hide");
  41.             $("#datatable")
  42.                 .DataTable()
  43.                 .ajax.reload();
  44.  
  45.             Swal.fire({
  46.                 type: "success",
  47.                 title: "Sukses !",
  48.                 text: "Data berhasil disimpan !"
  49.             });
  50.         },
  51.         error: function(xhr) {
  52.             var res = xhr.responseJSON;
  53.             if ($.isEmptyObject(res) == false) {
  54.                 $.each(res.errors, function(key, value) {
  55.                     $("#" + key)
  56.                         .closest(".form-group")
  57.                         .addClass("has-error")
  58.                         .append(
  59.                             '<span class="help-block"><strong>' +
  60.                                 value +
  61.                                 "</strong></span>"
  62.                         );
  63.                 });
  64.             }
  65.         }
  66.     });
  67. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement