datatables.blade.php 1.8 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. <!-- 引入 Bootstrap JS(可选,用于交互功能) -->
  12. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  13. @foreach($datas as $country=>$data)
  14. <div class="container mt-5">
  15. <h2 class="mb-4">{{$country}}</h2>
  16. <table class="table table-bordered" id="dataTable_{{$country}}">
  17. <thead class="table-dark">
  18. <tr>
  19. <th>ID</th>
  20. <th>Abbreviation</th>
  21. <th>Name</th>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. <!-- 数据行将通过 JavaScript 动态生成 -->
  26. </tbody>
  27. </table>
  28. </div>
  29. <script>
  30. // 将 PHP 数组转换为 JSON 并传递给 JavaScript
  31. const data_{{$country}} = @json($data);
  32. if (data_{{$country}}.length > 0) {
  33. data_{{$country}}.forEach(item => {
  34. const row = document.createElement('tr');
  35. const idCell = document.createElement('td');
  36. idCell.textContent = item.id;
  37. row.appendChild(idCell);
  38. const abbreviationCell = document.createElement('td');
  39. abbreviationCell.textContent = item.abbreviation;
  40. row.appendChild(abbreviationCell);
  41. const nameCell = document.createElement('td');
  42. nameCell.textContent = item.name;
  43. row.appendChild(nameCell);
  44. document.querySelector('#dataTable_{{$country}} tbody').appendChild(row);
  45. });
  46. } else {
  47. }
  48. </script>
  49. @endforeach
  50. </body>
  51. </html>