datatable.blade.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!-- resources/views/datatable.blade.php -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>数据表格展示</title>
  7. <!-- 引入 Bootstrap CSS(可选,用于美化表格) -->
  8. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  9. </head>
  10. <body>
  11. <div class="container mt-5">
  12. <h2 class="mb-4">{{$country}}</h2>
  13. <table class="table table-bordered" id="dataTable">
  14. <thead class="table-dark">
  15. <tr>
  16. <th>ID</th>
  17. <th>Abbreviation</th>
  18. <th>Name</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <!-- 数据行将通过 JavaScript 动态生成 -->
  23. </tbody>
  24. </table>
  25. <p id="noData" style="display: none;">没有数据显示。</p>
  26. </div>
  27. <!-- 引入 Bootstrap JS(可选,用于交互功能) -->
  28. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  29. <script>
  30. // 将 PHP 数组转换为 JSON 并传递给 JavaScript
  31. const data = @json($data);
  32. const tableBody = document.querySelector('#dataTable tbody');
  33. const noDataMsg = document.getElementById('noData');
  34. if (data.length > 0) {
  35. data.forEach(item => {
  36. const row = document.createElement('tr');
  37. const idCell = document.createElement('td');
  38. idCell.textContent = item.id;
  39. row.appendChild(idCell);
  40. const abbreviationCell = document.createElement('td');
  41. abbreviationCell.textContent = item.abbreviation;
  42. row.appendChild(abbreviationCell);
  43. const nameCell = document.createElement('td');
  44. nameCell.textContent = item.name;
  45. row.appendChild(nameCell);
  46. tableBody.appendChild(row);
  47. });
  48. } else {
  49. document.getElementById('dataTable').style.display = 'none';
  50. noDataMsg.style.display = 'block';
  51. }
  52. </script>
  53. </body>
  54. </html>