This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f516953
commit 1ae1a4e
Showing
1 changed file
with
43 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,57 @@ | ||
var selectedRowIndex; | ||
|
||
var categoryId; | ||
|
||
function openEditModal() { | ||
$('#editCategoryModal').modal('show'); | ||
// Lưu index của hàng được chọn | ||
selectedRowIndex = $(this).closest('tr').index(); | ||
} | ||
|
||
// Function to populate fields in the edit modal with existing data | ||
function populateEditModalFields(row) { | ||
var categoryName = row.cells[1].textContent.trim(); | ||
|
||
document.getElementById('editCategoryName').value = categoryName; | ||
function openEditModel(id) { | ||
categoryId = id; | ||
|
||
openEditModal(); | ||
} | ||
|
||
function saveChangesEditCategory() { | ||
// Lấy giá trị từ modal | ||
var editedCategoryName = document.getElementById('editCategoryName').value; | ||
|
||
// Kiểm tra giá trị có đọc được không | ||
console.log("Edited Category Name:", editedCategoryName); | ||
|
||
// Cập nhật hàng trong bảng | ||
var rowToUpdate = $('#datatablesSimple tbody tr').eq(selectedRowIndex); | ||
console.log("Row to update:", rowToUpdate); | ||
|
||
// Kiểm tra có lấy được hàng cần update không | ||
if (rowToUpdate.length > 0) { | ||
rowToUpdate.find('td:eq(1)').text(editedCategoryName); | ||
|
||
// Đóng modal | ||
closeEditCategoryModal(); | ||
} else { | ||
console.error("Row to update not found!"); | ||
} | ||
let editedCategoryName = document.getElementById('editCategoryName').value; | ||
|
||
// Get the category ID from the row or another source | ||
|
||
const url = contextPath + '/admin/category?id=' + categoryId + '&name=' + editedCategoryName; | ||
|
||
$.ajax({ | ||
url: url, | ||
type: "POST", | ||
dataType: "json", | ||
success: function (data) { | ||
if (data.status === "success") { | ||
Swal.fire({ | ||
title: "Success!", | ||
text: "Category information updated successfully!", | ||
icon: "success" | ||
}).then((result) => { | ||
if (result.isConfirmed) { | ||
window.location.reload(); | ||
} | ||
}); | ||
} else { | ||
Swal.fire({ | ||
title: "Oops...", | ||
text: "Something went wrong!", | ||
icon: "error" | ||
}); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
// Function to close the edit modal | ||
function closeEditCategoryModal() { | ||
function closeEditModal() { | ||
$('#editCategoryModal').modal('hide'); | ||
} | ||
|
||
// Event delegation to handle dynamically added elements | ||
$(document).on('click', '.btn-tiny', function () { | ||
openEditModal.call(this); // Phải gọi hàm openEditModal với this là element được click | ||
populateEditModalFields($(this).closest('tr')[0]); | ||
$(document).on('click', '#cancelEditBtn', function () { | ||
closeEditModal(); | ||
}); | ||
|
||
$(document).on('click', '#saveCategoryChangesBtn', function () { | ||
saveCategoryChanges(); | ||
}); |