Files
CKB/public/js/pages/back/user.js
2025-05-27 19:09:17 +07:00

105 lines
3.5 KiB
JavaScript

$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var table = $('#kt_table').DataTable({
processing: true,
serverSide: true,
ajax: $("input[name='ajax_url']").val(),
columns: [
{data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false},
{data: 'dealer_name', name: 'd.name'},
{data: 'role_name', name: 'r.name'},
{data: 'name', name: 'users.name'},
{data: 'email', name: 'users.email'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
$("#addUser").click(function() {
$("#userModal").modal("show")
let form_action = $("input[name='store_url']").val()
$("#userForm").attr('action', form_action)
$("#userForm input[name='_method']").remove()
$("#userForm").attr('data-form', 'store')
$("#userForm").trigger("reset")
$("#modalHeading").html("Tambah Pengguna")
})
function destroyUser(id) {
let action = $("#destroyUser"+id).attr("data-action")
Swal.fire({
title: 'Hapus User?',
text: "Semua data yang terkait dengan Pengguna ini juga akan terhapus!",
showCancelButton: true,
confirmButtonColor: '#d33',
cancelButtonColor: '#dedede',
confirmButtonText: 'Hapus'
}).then((result) => {
if (result.value) {
$.ajax({
url: action,
type: 'POST',
data: {
_token: $('meta[name="csrf-token"]').attr('content'),
_method: 'DELETE'
},
success: function(res) {
Swal.fire(
'Pengguna Dihapus!'
)
table.ajax.reload()
}
})
}
})
}
function editUser(id) {
let form_action = $("#editUser"+id).attr("data-action")
let edit_url = $("#editUser"+id).attr("data-url")
$("#userModal").modal("show")
$("#userForm").append('<input type="hidden" name="_method" value="PUT">')
$("#userForm").attr('action', form_action)
$("#userForm").attr('data-form', 'update')
$.get(edit_url, function(res) {
$("#userForm input[name='name']").val(res.data.name)
$("#userForm input[name='email']").val(res.data.email)
$("#userForm select[name='dealer_id'] option[value='"+ res.data.dealer_id +"']").prop('selected', true);
$("#userForm select[name='role'] option[value='"+ res.data.role_id +"']").prop('selected', true);
})
}
$(document).ready(function () {
$("#userForm").submit(function(e) {
e.preventDefault();
let dataForm = $("#userForm").attr('data-form')
if(dataForm == 'store') {
$.ajax({
url: $('#userForm').attr("action"),
type: 'POST',
data: $('#userForm').serialize(),
success: function(res) {
$("#userModal").modal("hide")
$('#userForm').trigger("reset")
table.ajax.reload()
}
})
}else if(dataForm == 'update') {
$.ajax({
url: $('#userForm').attr("action"),
type: 'POST',
data: $('#userForm').serialize(),
success: function(res) {
$("#userModal").modal("hide")
$('#userForm').trigger("reset")
table.ajax.reload()
}
})
}
})
});