add dashboard inside and outside system and fix timeout when search filter
This commit is contained in:
@@ -18,12 +18,15 @@ class GeneralTable {
|
||||
server: {
|
||||
url: (prev, keyword) => `${prev}?search=${keyword}`,
|
||||
},
|
||||
debounceTimeout: 1000,
|
||||
},
|
||||
pagination: this.options.pagination || {
|
||||
limit: 15,
|
||||
server: {
|
||||
url: (prev, page) =>
|
||||
`${prev}${prev.includes("?") ? "&" : "?"}page=${page + 1}`,
|
||||
`${prev}${prev.includes("?") ? "&" : "?"}page=${
|
||||
page + 1
|
||||
}`,
|
||||
},
|
||||
},
|
||||
sort: this.options.sort || true,
|
||||
@@ -48,22 +51,29 @@ class GeneralTable {
|
||||
processData(data) {
|
||||
return data.data.map((item) => {
|
||||
return this.columns.map((column) => {
|
||||
return item[column] || '';
|
||||
return item[column] || "";
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
handleActions() {
|
||||
document.addEventListener("click", (event) => {
|
||||
if (event.target && event.target.classList.contains('btn-edit')) {
|
||||
if (event.target && event.target.classList.contains("btn-edit")) {
|
||||
this.handleEdit(event);
|
||||
}
|
||||
else if (event.target && event.target.classList.contains('btn-delete')) {
|
||||
} else if (
|
||||
event.target &&
|
||||
event.target.classList.contains("btn-delete")
|
||||
) {
|
||||
this.handleDelete(event);
|
||||
}
|
||||
else if (event.target && event.target.classList.contains('btn-create')) {
|
||||
} else if (
|
||||
event.target &&
|
||||
event.target.classList.contains("btn-create")
|
||||
) {
|
||||
this.handleCreate(event);
|
||||
} else if (event.target && event.target.classList.contains('btn-bulk-create')) {
|
||||
} else if (
|
||||
event.target &&
|
||||
event.target.classList.contains("btn-bulk-create")
|
||||
) {
|
||||
this.handleBulkCreate(event);
|
||||
}
|
||||
});
|
||||
@@ -72,28 +82,28 @@ class GeneralTable {
|
||||
// Fungsi untuk menangani create
|
||||
handleCreate(event) {
|
||||
// Menggunakan model dan ID untuk membangun URL dinamis
|
||||
const model = event.target.getAttribute('data-model'); // Mengambil model dari data-model
|
||||
const model = event.target.getAttribute("data-model"); // Mengambil model dari data-model
|
||||
window.location.href = `${this.baseUrl}/${model}/create`;
|
||||
}
|
||||
|
||||
handleBulkCreate(event) {
|
||||
// Menggunakan model dan ID untuk membangun URL dinamis
|
||||
const model = event.target.getAttribute('data-model');
|
||||
const model = event.target.getAttribute("data-model");
|
||||
window.location.href = `${this.baseUrl}/${model}/bulk-create`;
|
||||
}
|
||||
|
||||
// Fungsi untuk menangani edit
|
||||
handleEdit(event) {
|
||||
const id = event.target.getAttribute('data-id');
|
||||
const model = event.target.getAttribute('data-model'); // Mengambil model dari data-model
|
||||
console.log('Editing record with ID:', id);
|
||||
const id = event.target.getAttribute("data-id");
|
||||
const model = event.target.getAttribute("data-model"); // Mengambil model dari data-model
|
||||
console.log("Editing record with ID:", id);
|
||||
// Menggunakan model dan ID untuk membangun URL dinamis
|
||||
window.location.href = `${this.baseUrl}/${model}/${id}/edit`;
|
||||
}
|
||||
|
||||
// Fungsi untuk menangani delete
|
||||
handleDelete(event) {
|
||||
const id = event.target.getAttribute('data-id');
|
||||
const id = event.target.getAttribute("data-id");
|
||||
console.log(id);
|
||||
// if (confirm("Are you sure you want to delete this item?")) {
|
||||
// this.deleteRecord(id);
|
||||
@@ -105,7 +115,7 @@ class GeneralTable {
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: "#d33",
|
||||
cancelButtonColor: "#3085d6",
|
||||
confirmButtonText: "Yes, delete it!"
|
||||
confirmButtonText: "Yes, delete it!",
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
this.deleteRecord(id);
|
||||
@@ -114,8 +124,8 @@ class GeneralTable {
|
||||
text: "Your record has been deleted.",
|
||||
icon: "success",
|
||||
showConfirmButton: false, // Menghilangkan tombol OK
|
||||
timer: 2000 // Menutup otomatis dalam 2 detik (opsional)
|
||||
});
|
||||
timer: 2000, // Menutup otomatis dalam 2 detik (opsional)
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -123,8 +133,9 @@ class GeneralTable {
|
||||
async deleteRecord(id) {
|
||||
try {
|
||||
console.log(id);
|
||||
const response = await fetch(`${this.apiUrl}/${id}`, { // Menambahkan model dalam URL
|
||||
method: 'DELETE',
|
||||
const response = await fetch(`${this.apiUrl}/${id}`, {
|
||||
// Menambahkan model dalam URL
|
||||
method: "DELETE",
|
||||
headers: this.options.headers || {
|
||||
Authorization: `Bearer ${document
|
||||
.querySelector('meta[name="api-token"]')
|
||||
@@ -136,10 +147,14 @@ class GeneralTable {
|
||||
location.reload();
|
||||
} else {
|
||||
const data = await response.json();
|
||||
showErrorAlert(`Failed to delete record: ${data.message || "Unknown error"}`);
|
||||
showErrorAlert(
|
||||
`Failed to delete record: ${
|
||||
data.message || "Unknown error"
|
||||
}`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting data:', error);
|
||||
console.error("Error deleting data:", error);
|
||||
showErrorAlert("Error deleting data. Please try again.");
|
||||
}
|
||||
}
|
||||
@@ -148,7 +163,7 @@ class GeneralTable {
|
||||
// Fungsi untuk menampilkan alert
|
||||
function showErrorAlert(message) {
|
||||
const alertContainer = document.getElementById("alert-container");
|
||||
|
||||
|
||||
alertContainer.innerHTML = `
|
||||
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
||||
${message}
|
||||
@@ -157,4 +172,4 @@ function showErrorAlert(message) {
|
||||
`;
|
||||
}
|
||||
|
||||
export default GeneralTable;
|
||||
export default GeneralTable;
|
||||
|
||||
Reference in New Issue
Block a user