partial update transaction work with stock product
This commit is contained in:
@@ -1,104 +1,221 @@
|
||||
// Global variables
|
||||
let ajaxUrl, storeUrl, table;
|
||||
|
||||
$.ajaxSetup({
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
|
||||
}
|
||||
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
|
||||
},
|
||||
});
|
||||
|
||||
var table = $('#kt_table').DataTable({
|
||||
processing: true,
|
||||
serverSide: true,
|
||||
ajax: $("input[name='ajax_url']"),
|
||||
columns: [
|
||||
{data: 'category_name', name: 'c.name'},
|
||||
{data: 'name', name: 'w.name'},
|
||||
{data: 'shortname', name: 'w.shortname'},
|
||||
{data: 'desc', name: 'w.desc'},
|
||||
{data: 'action', name: 'action', orderable: false, searchable: false},
|
||||
]
|
||||
$(document).ready(function () {
|
||||
// Get URLs from hidden inputs
|
||||
ajaxUrl = $('input[name="ajax_url"]').val();
|
||||
storeUrl = $('input[name="store_url"]').val();
|
||||
|
||||
// Initialize DataTable
|
||||
table = $("#kt_table").DataTable({
|
||||
processing: true,
|
||||
serverSide: true,
|
||||
ajax: {
|
||||
url: ajaxUrl,
|
||||
},
|
||||
columns: [
|
||||
{ data: "category_name", name: "c.name" },
|
||||
{ data: "name", name: "w.name" },
|
||||
{ data: "shortname", name: "w.shortname" },
|
||||
{ data: "desc", name: "w.desc" },
|
||||
{
|
||||
data: "action",
|
||||
name: "action",
|
||||
orderable: false,
|
||||
searchable: false,
|
||||
className: "text-center",
|
||||
width: "auto",
|
||||
render: function (data, type, row) {
|
||||
return data;
|
||||
},
|
||||
},
|
||||
],
|
||||
responsive: true,
|
||||
autoWidth: false,
|
||||
columnDefs: [
|
||||
{
|
||||
targets: -1, // Action column
|
||||
className: "text-center",
|
||||
width: "auto",
|
||||
orderable: false,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// Initialize Select2
|
||||
$("#category_id").select2({
|
||||
placeholder: "-- Pilih Kategori --",
|
||||
allowClear: true,
|
||||
width: "100%",
|
||||
dropdownParent: $("#workModal"),
|
||||
language: {
|
||||
noResults: function () {
|
||||
return "Tidak ada hasil yang ditemukan";
|
||||
},
|
||||
searching: function () {
|
||||
return "Mencari...";
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Modal close handlers
|
||||
$(document).on("click", '[data-dismiss="modal"]', function () {
|
||||
$(this).closest(".modal").modal("hide");
|
||||
});
|
||||
|
||||
$(document).on("click", ".modal .close", function () {
|
||||
$(this).closest(".modal").modal("hide");
|
||||
});
|
||||
|
||||
$(document).on("click", ".modal", function (e) {
|
||||
if (e.target === this) {
|
||||
$(this).modal("hide");
|
||||
}
|
||||
});
|
||||
|
||||
$(document).on("keydown", function (e) {
|
||||
if (e.keyCode === 27) {
|
||||
$(".modal.show").modal("hide");
|
||||
}
|
||||
});
|
||||
|
||||
// Bootstrap 5 fallback
|
||||
$(document).on("click", '[data-bs-dismiss="modal"]', function () {
|
||||
$(this).closest(".modal").modal("hide");
|
||||
});
|
||||
|
||||
// Alternative close button selectors
|
||||
$(document).on("click", ".btn-secondary", function () {
|
||||
if ($(this).closest(".modal").length) {
|
||||
$(this).closest(".modal").modal("hide");
|
||||
}
|
||||
});
|
||||
|
||||
// Force close function
|
||||
window.closeModal = function (modalId) {
|
||||
$("#" + modalId).modal("hide");
|
||||
};
|
||||
|
||||
// Modal hidden event
|
||||
$("#workModal").on("hidden.bs.modal", function () {
|
||||
$("#workForm").trigger("reset");
|
||||
$("#category_id").val("").trigger("change");
|
||||
$('#workForm input[name="_method"]').remove();
|
||||
});
|
||||
|
||||
// Add Work
|
||||
$("#addWork").click(function () {
|
||||
$("#workModal").modal("show");
|
||||
let form_action = storeUrl;
|
||||
$("#workForm").attr("action", form_action);
|
||||
$("#workForm input[name='_method']").remove();
|
||||
$("#workForm").attr("data-form", "store");
|
||||
$("#workForm").trigger("reset");
|
||||
$("#workForm textarea[name='desc']").val("");
|
||||
|
||||
// Reset Select2
|
||||
$("#category_id").val("").trigger("change");
|
||||
});
|
||||
|
||||
// Submit Form
|
||||
$("#workForm").submit(function (e) {
|
||||
e.preventDefault();
|
||||
let dataForm = $("#workForm").attr("data-form");
|
||||
if (dataForm == "store") {
|
||||
$.ajax({
|
||||
url: $("#workForm").attr("action"),
|
||||
type: "POST",
|
||||
data: $("#workForm").serialize(),
|
||||
success: function (res) {
|
||||
$("#workModal").modal("hide");
|
||||
$("#workForm").trigger("reset");
|
||||
$("#category_id").val("").trigger("change");
|
||||
table.ajax.reload();
|
||||
Swal.fire({
|
||||
icon: "success",
|
||||
title: "Berhasil!",
|
||||
text: "Data pekerjaan berhasil disimpan.",
|
||||
timer: 2000,
|
||||
showConfirmButton: false,
|
||||
});
|
||||
},
|
||||
});
|
||||
} else if (dataForm == "update") {
|
||||
$.ajax({
|
||||
url: $("#workForm").attr("action"),
|
||||
type: "POST",
|
||||
data: $("#workForm").serialize(),
|
||||
success: function (res) {
|
||||
$("#workModal").modal("hide");
|
||||
$("#workForm").trigger("reset");
|
||||
$("#category_id").val("").trigger("change");
|
||||
table.ajax.reload();
|
||||
Swal.fire({
|
||||
icon: "success",
|
||||
title: "Berhasil!",
|
||||
text: "Data pekerjaan berhasil diupdate.",
|
||||
timer: 2000,
|
||||
showConfirmButton: false,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
$("#addWork").click(function() {
|
||||
$("#workModal").modal("show")
|
||||
let form_action = $("input[name='store_url']").val()
|
||||
$("#workForm").attr('action', form_action)
|
||||
$("#workForm input[name='_method']").remove()
|
||||
$("#workForm").attr('data-form', 'store')
|
||||
$("#workForm").trigger("reset")
|
||||
$("#workForm textarea[name='desc']").val("")
|
||||
})
|
||||
|
||||
// Global functions for edit and delete
|
||||
function destroyWork(id) {
|
||||
let action = $("#destroyWork"+id).attr("data-action")
|
||||
let action = $("#destroyWork" + id).attr("data-action");
|
||||
Swal.fire({
|
||||
title: 'Hapus Pekerjaan?',
|
||||
title: "Hapus Pekerjaan?",
|
||||
text: "Anda tidak akan bisa mengembalikannya!",
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#d33',
|
||||
cancelButtonColor: '#dedede',
|
||||
confirmButtonText: 'Hapus'
|
||||
confirmButtonColor: "#d33",
|
||||
cancelButtonColor: "#dedede",
|
||||
confirmButtonText: "Hapus",
|
||||
}).then((result) => {
|
||||
if (result.value) {
|
||||
$.ajax({
|
||||
url: action,
|
||||
type: 'POST',
|
||||
type: "POST",
|
||||
data: {
|
||||
_token: $('meta[name="csrf-token"]').attr('content'),
|
||||
_method: 'DELETE'
|
||||
_token: $('meta[name="csrf-token"]').attr("content"),
|
||||
_method: "DELETE",
|
||||
},
|
||||
success: function(res) {
|
||||
success: function (res) {
|
||||
Swal.fire(
|
||||
'Dealer Dihapus!'
|
||||
)
|
||||
table.ajax.reload()
|
||||
}
|
||||
})
|
||||
"Berhasil!",
|
||||
"Pekerjaan berhasil dihapus.",
|
||||
"success"
|
||||
);
|
||||
if (table) {
|
||||
table.ajax.reload();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function editWork(id) {
|
||||
let form_action = $("#editWork"+id).attr("data-action")
|
||||
let edit_url = $("#editWork"+id).attr("data-url")
|
||||
$("#workModal").modal("show")
|
||||
$("#workForm").append('<input type="hidden" name="_method" value="PUT">')
|
||||
$("#workForm").attr('action', form_action)
|
||||
$("#workForm").attr('data-form', 'update')
|
||||
$.get(edit_url, function(res) {
|
||||
$("#workForm input[name='name']").val(res.data.name)
|
||||
$("#workForm input[name='shortname']").val(res.data.shortname)
|
||||
$("#workForm textarea[name='desc']").html(res.data.desc)
|
||||
$("#workForm option[value='"+ res.data.category_id +"']").prop('selected', true);
|
||||
})
|
||||
let form_action = $("#editWork" + id).attr("data-action");
|
||||
let edit_url = $("#editWork" + id).attr("data-url");
|
||||
$("#workModal").modal("show");
|
||||
$("#workForm").append('<input type="hidden" name="_method" value="PUT">');
|
||||
$("#workForm").attr("action", form_action);
|
||||
$("#workForm").attr("data-form", "update");
|
||||
$.get(edit_url, function (res) {
|
||||
$("#workForm input[name='name']").val(res.data.name);
|
||||
$("#workForm input[name='shortname']").val(res.data.shortname);
|
||||
$("#workForm textarea[name='desc']").html(res.data.desc);
|
||||
$("#workForm select[name='category_id']")
|
||||
.val(res.data.category_id)
|
||||
.trigger("change");
|
||||
});
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
$("#workForm").submit(function(e) {
|
||||
e.preventDefault();
|
||||
let dataForm = $("#workForm").attr('data-form')
|
||||
if(dataForm == 'store') {
|
||||
$.ajax({
|
||||
url: $('#workForm').attr("action"),
|
||||
type: 'POST',
|
||||
data: $('#workForm').serialize(),
|
||||
success: function(res) {
|
||||
$("#workModal").modal("hide")
|
||||
$('#workForm').trigger("reset")
|
||||
table.ajax.reload()
|
||||
}
|
||||
})
|
||||
}else if(dataForm == 'update') {
|
||||
$.ajax({
|
||||
url: $('#workForm').attr("action"),
|
||||
type: 'POST',
|
||||
data: $('#workForm').serialize(),
|
||||
success: function(res) {
|
||||
$("#workModal").modal("hide")
|
||||
$('#workForm').trigger("reset")
|
||||
table.ajax.reload()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user