Spaces:
Sleeping

doluong6 commited on
Commit
047246d
·
verified ·
1 Parent(s): 25b308b

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +44 -25
static/script.js CHANGED
@@ -1362,46 +1362,65 @@ async function hienThiBangGia5() {
1362
  const response = await fetch(`${BACKEND_URL}/bang_gia_dungtich_luuluong`);
1363
  const data = await response.json();
1364
 
1365
- if (!data || !data.columns || !data.rows) {
1366
- console.warn("Không có dữ liệu bảng giá 5.");
1367
  return;
1368
  }
1369
 
1370
- const tableId = "#tableBangGia5";
1371
-
1372
- // Clear DataTable nếu đã khởi tạo
1373
- if ($.fn.DataTable.isDataTable(tableId)) {
1374
- $(tableId).DataTable().clear().destroy();
1375
- }
1376
-
1377
- // Xóa nội dung cũ
1378
- $(`${tableId} thead`).empty();
1379
- $(`${tableId} tbody`).empty();
1380
-
1381
  // Tạo header
1382
- const headerRow = $("<tr></tr>");
1383
  data.columns.forEach(col => {
1384
- headerRow.append(`<th>${col}</th>`);
1385
  });
1386
- $(`${tableId} thead`).append(headerRow);
 
1387
 
1388
  // Tạo body
 
1389
  data.rows.forEach(row => {
1390
- const rowHtml = $("<tr></tr>");
1391
- row.forEach(cell => {
1392
- rowHtml.append(`<td>${cell}</td>`);
 
 
 
 
 
 
 
1393
  });
1394
- $(`${tableId} tbody`).append(rowHtml);
1395
  });
 
1396
 
1397
- // Khởi tạo lại DataTable
1398
- $(tableId).DataTable({
 
1399
  scrollX: true,
1400
- pageLength: 25
 
 
 
 
 
 
 
 
1401
  });
1402
 
1403
- } catch (error) {
1404
- console.error("Lỗi khi tải bảng giá 5:", error);
 
 
 
 
 
 
 
 
 
 
1405
  }
1406
  }
1407
 
 
 
1362
  const response = await fetch(`${BACKEND_URL}/bang_gia_dungtich_luuluong`);
1363
  const data = await response.json();
1364
 
1365
+ if (!data || !data.rows || data.rows.length === 0) {
1366
+ $("#tableBangGia5 tbody").html("<tr><td colspan='100%'>Không có dữ liệu</td></tr>");
1367
  return;
1368
  }
1369
 
 
 
 
 
 
 
 
 
 
 
 
1370
  // Tạo header
1371
+ let theadHtml = "<tr>";
1372
  data.columns.forEach(col => {
1373
+ theadHtml += `<th>${col}</th>`;
1374
  });
1375
+ theadHtml += "</tr>";
1376
+ $("#tableBangGia5 thead").html(theadHtml);
1377
 
1378
  // Tạo body
1379
+ let tbodyHtml = "";
1380
  data.rows.forEach(row => {
1381
+ tbodyHtml += "<tr>";
1382
+ row.forEach((cell, i) => {
1383
+ const columnName = data.columns[i];
1384
+ if (columnName.toLowerCase().includes("giá") && !isNaN(cell)) {
1385
+ tbodyHtml += `<td style="text-align:right">${Number(cell).toLocaleString("vi-VN")}</td>`;
1386
+ } else if (columnName.toLowerCase().includes("tên") || columnName.toLowerCase().includes("thiết bị")) {
1387
+ tbodyHtml += `<td style="text-align:left">${cell}</td>`;
1388
+ } else {
1389
+ tbodyHtml += `<td>${cell}</td>`;
1390
+ }
1391
  });
1392
+ tbodyHtml += "</tr>";
1393
  });
1394
+ $("#tableBangGia5 tbody").html(tbodyHtml);
1395
 
1396
+ // Khởi tạo DataTable
1397
+ const table = $("#tableBangGia5").DataTable({
1398
+ destroy: true,
1399
  scrollX: true,
1400
+ autoWidth: false,
1401
+ pageLength: 25,
1402
+ language: {
1403
+ search: "🔍 Tìm kiếm:",
1404
+ lengthMenu: "Hiển thị _MENU_ dòng",
1405
+ info: "Đang hiển thị _START_ đến _END_ của _TOTAL_ dòng",
1406
+ paginate: { previous: "←", next: "→" },
1407
+ emptyTable: "Không có dữ liệu"
1408
+ }
1409
  });
1410
 
1411
+ // Căn chỉnh lại
1412
+ setTimeout(() => {
1413
+ table.columns.adjust().draw(false);
1414
+ }, 100);
1415
+
1416
+ // Hiển thị đúng bảng
1417
+ $(".fade-in").hide();
1418
+ $("#banggia5").show();
1419
+
1420
+ } catch (err) {
1421
+ console.error("Lỗi khi tải bảng giá 5:", err);
1422
+ alert("Không thể tải bảng giá Dung tích – Lưu lượng.");
1423
  }
1424
  }
1425
 
1426
+