Spaces:
Sleeping

doluong6 commited on
Commit
26689f2
·
verified ·
1 Parent(s): 6ac1906

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +11 -5
static/script.js CHANGED
@@ -97,16 +97,23 @@ function chonMuc(muc) {
97
  const section = document.getElementById("dm");
98
  if (section) section.style.display = "block"; // ✅ hiện vùng hiển thị
99
  fetch(`${BACKEND_URL}/danh_muc_thiet_bi`)
100
- .then(res => res.json())
 
 
 
101
  .then(data => {
102
  const table = $('#bangDanhMucThietBi');
103
- table.empty(); // Xoá nội dung cũ nếu có
 
 
 
 
 
104
 
105
- // Nếu chưa có DataTable thì tạo mới
106
  if (!$.fn.DataTable.isDataTable('#bangDanhMucThietBi')) {
107
  table.DataTable({
108
  data: data,
109
- columns: Object.keys(data[0]).map(key => ({ title: key, data: key })),
110
  paging: true,
111
  searching: true,
112
  scrollX: true,
@@ -114,7 +121,6 @@ function chonMuc(muc) {
114
  autoWidth: false
115
  });
116
  } else {
117
- // Nếu đã có DataTable rồi thì cập nhật dữ liệu
118
  const dt = table.DataTable();
119
  dt.clear().rows.add(data).draw();
120
  }
 
97
  const section = document.getElementById("dm");
98
  if (section) section.style.display = "block"; // ✅ hiện vùng hiển thị
99
  fetch(`${BACKEND_URL}/danh_muc_thiet_bi`)
100
+ .then(res => {
101
+ if (!res.ok) throw new Error("Lỗi từ server: " + res.status);
102
+ return res.json();
103
+ })
104
  .then(data => {
105
  const table = $('#bangDanhMucThietBi');
106
+ table.empty();
107
+
108
+ if (!Array.isArray(data) || data.length === 0) {
109
+ table.html("<p style='color:red'>Không có dữ liệu để hiển thị.</p>");
110
+ return;
111
+ }
112
 
 
113
  if (!$.fn.DataTable.isDataTable('#bangDanhMucThietBi')) {
114
  table.DataTable({
115
  data: data,
116
+ columns: Object.keys(data[0] || {}).map(key => ({ title: key, data: key })),
117
  paging: true,
118
  searching: true,
119
  scrollX: true,
 
121
  autoWidth: false
122
  });
123
  } else {
 
124
  const dt = table.DataTable();
125
  dt.clear().rows.add(data).draw();
126
  }