Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
fix edit category
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhtnguyen committed Dec 8, 2023
1 parent f516953 commit 1ae1a4e
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions src/main/webapp/assets/js/admin/edit-category.js
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();
});

0 comments on commit 1ae1a4e

Please sign in to comment.