Spaces:
Sleeping

doluong6 commited on
Commit
3e0f502
·
verified ·
1 Parent(s): 9d2e9e0

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +32 -13
static/script.js CHANGED
@@ -713,24 +713,43 @@ async function hienThiHSKH() {
713
  }
714
 
715
  async function luuHSKH() {
716
- const table = $('#bangHSKH').DataTable();
717
- const data = table.rows().data().toArray();
718
- if (data.length === 0) return alert("Không có dữ liệu để lưu.");
719
 
720
- const headers = table.columns().header().toArray().map(th => th.textContent.trim());
 
 
 
 
721
 
722
- try {
723
- const res = await fetch(`${BACKEND_URL}/cap_nhat_hskh`, {
 
 
 
 
 
 
724
  method: "POST",
725
- headers: { "Content-Type": "application/json" },
726
- body: JSON.stringify({ columns: headers, data: data })
 
 
 
 
 
727
  });
728
 
729
- const result = await res.json();
730
- alert(result.message || "✅ Đã lưu dữ liệu hồ sơ!");
731
- } catch (err) {
732
- console.error("❌ Lỗi khi lưu hồ sơ:", err);
733
- alert("❌ Không thể lưu dữ liệu hồ sơ.");
 
 
 
 
734
  }
735
  }
736
 
 
713
  }
714
 
715
  async function luuHSKH() {
716
+ try {
717
+ const table = $('#bangHSKH').DataTable();
718
+ const rawData = table.rows().data().toArray();
719
 
720
+ // Lấy lại tiêu đề từ <thead> (đảm bảo đúng thứ tự)
721
+ const headers = [];
722
+ $("#bangHSKH thead tr:last-child th").each(function () {
723
+ headers.push($(this).text().trim());
724
+ });
725
 
726
+ // Nếu <thead> có 2 dòng (như bạn đang dùng), thêm các tiêu đề từ dòng trên (rowspan)
727
+ const topHeaders = $("#bangHSKH thead tr:first-child th[rowspan='2']");
728
+ topHeaders.each(function (index) {
729
+ headers.splice(index, 0, $(this).text().trim());
730
+ });
731
+
732
+ // Gửi dữ liệu lên backend
733
+ const response = await fetch(`${BACKEND_URL}/cap_nhat_hskh`, {
734
  method: "POST",
735
+ headers: {
736
+ "Content-Type": "application/json"
737
+ },
738
+ body: JSON.stringify({
739
+ columns: headers,
740
+ rows: rawData
741
+ })
742
  });
743
 
744
+ if (response.ok) {
745
+ alert("✅ Dữ liệu đã được lưu thành công!");
746
+ } else {
747
+ const err = await response.json();
748
+ alert("❌ Lỗi khi lưu: " + err.detail);
749
+ }
750
+ } catch (error) {
751
+ console.error("Lỗi khi gửi dữ liệu:", error);
752
+ alert("❌ Không thể gửi dữ liệu để lưu.");
753
  }
754
  }
755