Spaces:
Sleeping

doluong6 commited on
Commit
b4040ed
·
verified ·
1 Parent(s): a9f323b

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +33 -31
static/script.js CHANGED
@@ -94,41 +94,43 @@ function chonMuc(muc) {
94
  }
95
  }
96
  if (muc === "dm") {
97
- const old = document.getElementById("dmcontent");
98
- if (old) old.remove(); // xoá bảng cũ nếu có
 
 
 
 
99
 
100
- fetch(`${BACKEND_URL}/danh_muc_thiet_bi`)
101
- .then(res => res.json())
102
- .then(json => {
103
- const data = json.data;
104
- let html = "<h2>Danh mục dụng cụ thiết bị hóa lý</h2><table id='bangDanhMuc' class='display' style='width:100%'><tbody>";
105
-
106
- data.forEach(row => {
107
- html += "<tr>";
108
- row.forEach(cell => {
109
- html += `<td>${cell}</td>`;
110
- });
111
- html += "</tr>";
112
- });
113
 
114
- html += "</tbody></table>";
115
-
116
- const div = document.createElement("div");
117
- div.id = "dmcontent";
118
- div.className = "fade-in";
119
- div.innerHTML = html;
120
- document.querySelector(".main").appendChild(div);
 
 
 
 
121
 
122
- $(document).ready(function () {
123
- $('#bangDanhMuc').DataTable();
 
 
 
 
 
 
124
  });
125
- })
126
- .catch(err => {
127
- alert("Lỗi khi tải danh mục: " + err);
128
- });
129
-
130
- return; // thoát không xử lý các mục khác
131
- }
132
  }
133
 
134
 
 
94
  }
95
  }
96
  if (muc === "dm") {
97
+ fetch("/danh_muc_thiet_bi")
98
+ .then(res => res.json())
99
+ .then(data => {
100
+ if (data && data.length > 0) {
101
+ const table = $('#bangDanhMucThietBi');
102
+ table.empty(); // Clear cũ
103
 
104
+ // Tạo header
105
+ let thead = '<thead><tr>';
106
+ Object.keys(data[0]).forEach(key => {
107
+ thead += `<th>${key}</th>`;
108
+ });
109
+ thead += '</tr></thead>';
110
+ table.append(thead);
 
 
 
 
 
 
111
 
112
+ // Tạo body
113
+ let tbody = '<tbody>';
114
+ data.forEach(row => {
115
+ tbody += '<tr>';
116
+ Object.values(row).forEach(val => {
117
+ tbody += `<td>${val}</td>`;
118
+ });
119
+ tbody += '</tr>';
120
+ });
121
+ tbody += '</tbody>';
122
+ table.append(tbody);
123
 
124
+ // Khởi tạo lại DataTable nếu chưa có
125
+ if (!$.fn.DataTable.isDataTable('#bangDanhMucThietBi')) {
126
+ $('#bangDanhMucThietBi').DataTable();
127
+ }
128
+ }
129
+ })
130
+ .catch(err => {
131
+ console.error("Lỗi khi tải danh mục thiết bị:", err);
132
  });
133
+ }
 
 
 
 
 
 
134
  }
135
 
136