Ajax获取PHP端csv转的json数据并js前端排序与分页

打印 上一主题 下一主题

主题 1384|帖子 1384|积分 4152


  1. <?php
  2. setlocale(LC_ALL, 'C'); //window:删除行首双斜杠
  3. if($_GET["act"]=="list"){
  4. $csvFile = 'book.csv'; // 文件路径:制表符分隔文件
  5. $data = [];
  6. if (($handle = fopen($csvFile, 'r')) !== false) {
  7.     $header = fgetcsv($handle,0,"\t"); // 读取表头
  8.     while (($row = fgetcsv($handle,0,"\t")) !== false) {
  9.         $data[] = array_combine($header, $row); // 将表头与数据组合
  10.     }
  11.     fclose($handle);
  12. }
  13. header('Content-Type: application/json');
  14. echo json_encode($data);
  15. exit();
  16. }
  17. ?>
  18. <!DOCTYPE html>
  19. <html>
  20. <head>
  21.     <meta charset="UTF-8">
  22.     <title>数据表格</title>
  23. <style>
  24. .container {
  25.     margin: 20px;
  26. }
  27. table {
  28.     width: 100%;
  29.     border-collapse: collapse;
  30.     margin-bottom: 20px;
  31. }
  32. th, td {
  33.     border: 1px solid #ddd;
  34.     padding: 5px;
  35.     text-align: left;
  36. }
  37. th {
  38.     background-color: #f5f5f5;
  39.     position: relative;
  40.     padding-right: 25px;
  41.     cursor: pointer;
  42. }
  43. .sort-arrows {
  44.     position: absolute;
  45.     right: 5px;
  46.     top: 50%;
  47.     transform: translateY(-50%);
  48.     display: flex;
  49.     flex-direction: column;
  50.     line-height: 8px;
  51. }
  52. .arrow {
  53.     font-size: 8px;
  54.     color: #666;
  55. }
  56. .arrow.active {
  57.     color: red;
  58. }
  59. .pagination {
  60.     display: flex;
  61.     gap: 10px;
  62.     align-items: center;
  63.     justify-content: center;
  64. }
  65. button {
  66.     padding: 5px 10px;
  67.     cursor: pointer;
  68. }
  69. select {
  70.     padding: 5px;
  71. }
  72. tr:nth-child(even) {
  73.     background-color: #f9f9f9;
  74. }
  75. tr:hover {
  76.     background-color: #f5f5f5;
  77. }
  78. </style>
  79. </head>
  80. <body>
  81.     <div class="container">
  82.         <table id="dataTable">
  83.             <thead>
  84.                 <tr id="headerRow">
  85.                     <!-- 表头将由JS动态生成 -->
  86.                 </tr>
  87.             </thead>
  88.             <tbody id="tableBody">
  89.                 <!-- 数据行将由JS动态生成 -->
  90.             </tbody>
  91.         </table>
  92.         <div class="pagination">
  93.             <button id="firstPage">首页</button>
  94.             <button id="prevPage">上一页</button>
  95.             <select id="pageSelect"></select>
  96.             <button id="nextPage">下一页</button>
  97.             <button id="lastPage">尾页</button>
  98.         </div>
  99.     </div>
  100. <script>
  101. class DataTable {
  102.     constructor() {
  103.         this.data = [];
  104.         this.currentPage = 1;
  105.         this.rowsPerPage = 10;
  106.         this.sortColumn = null;
  107.         this.sortDirection = 'asc';
  108.         
  109.         this.init();
  110.     }
  111.     init() {
  112.         this.loadData();
  113.         this.bindEvents();
  114.     }
  115.     async loadData() {
  116.         try {
  117.             const response = await fetch('?act=list&tt=tt');
  118.             const jsonData = await response.json();
  119.             this.data = jsonData;
  120.             this.setupTable();
  121.             this.renderTable();
  122.             this.setupPagination();
  123.         } catch (error) {
  124.             console.error('数据加载失败:', error);
  125.         }
  126.     }
  127.     setupTable() {
  128.         if (this.data.length === 0) return;
  129.         
  130.         const headerRow = document.getElementById('headerRow');
  131.         headerRow.innerHTML = '';
  132.         
  133.         // 创建表头
  134.         Object.keys(this.data[0]).forEach(key => {
  135.             const th = document.createElement('th');
  136.             th.innerHTML = `
  137.                 ${key}
  138.                 <div class="sort-arrows">
  139.                     <span class="arrow up" data-column="${key}">▲</span>
  140.                     <span class="arrow down" data-column="${key}">▼</span>
  141.                 </div>
  142.             `;
  143.             headerRow.appendChild(th);
  144.         });
  145.     }
  146.     renderTable() {
  147.         const tableBody = document.getElementById('tableBody');
  148.         tableBody.innerHTML = '';
  149.         // 排序数据
  150.         let sortedData = [...this.data];
  151.         if (this.sortColumn) {
  152.             sortedData.sort((a, b) => {
  153.                 const aVal = a[this.sortColumn];
  154.                 const bVal = b[this.sortColumn];
  155.                 if (this.sortDirection === 'asc') {
  156.                     return aVal > bVal ? 1 : -1;
  157.                 } else {
  158.                     return aVal < bVal ? 1 : -1;
  159.                 }
  160.             });
  161.         }
  162.         // 分页
  163.         const start = (this.currentPage - 1) * this.rowsPerPage;
  164.         const paginatedData = sortedData.slice(start, start + this.rowsPerPage);
  165.         // 渲染数据行
  166.         paginatedData.forEach(row => {
  167.             const tr = document.createElement('tr');
  168.             Object.values(row).forEach(value => {
  169.                 const td = document.createElement('td');
  170.                 td.textContent = value;
  171.                 tr.appendChild(td);
  172.             });
  173.             tableBody.appendChild(tr);
  174.         });
  175.     }
  176.     setupPagination() {
  177.         const totalPages = Math.ceil(this.data.length / this.rowsPerPage);
  178.         const pageSelect = document.getElementById('pageSelect');
  179.         pageSelect.innerHTML = '';
  180.         for (let i = 1; i <= totalPages; i++) {
  181.             const option = document.createElement('option');
  182.             option.value = i;
  183.             option.textContent = `第${i}页`;
  184.             pageSelect.appendChild(option);
  185.         }
  186.     }
  187.     bindEvents() {
  188.         // 排序事件
  189.         document.getElementById('headerRow').addEventListener('click', (e) => {
  190.             const arrow = e.target.closest('.arrow');
  191.             if (!arrow) return;
  192.             const column = arrow.dataset.column;
  193.             const isUp = arrow.classList.contains('up');
  194.             // 重置所有箭头样式
  195.             document.querySelectorAll('.arrow').forEach(a => a.classList.remove('active'));
  196.             
  197.             // 设置当前箭头样式
  198.             arrow.classList.add('active');
  199.             this.sortColumn = column;
  200.             this.sortDirection = isUp ? 'asc' : 'desc';
  201.             this.renderTable();
  202.         });
  203.         // 分页事件
  204.         document.getElementById('firstPage').addEventListener('click', () => {
  205.             this.currentPage = 1;
  206.             this.renderTable();
  207.         });
  208.         document.getElementById('prevPage').addEventListener('click', () => {
  209.             if (this.currentPage > 1) {
  210.                 this.currentPage--;
  211.                 this.renderTable();
  212.             }
  213.         });
  214.         document.getElementById('pageSelect').addEventListener('change', (e) => {
  215.             this.currentPage = parseInt(e.target.value);
  216.             this.renderTable();
  217.         });
  218.         document.getElementById('nextPage').addEventListener('click', () => {
  219.             const totalPages = Math.ceil(this.data.length / this.rowsPerPage);
  220.             if (this.currentPage < totalPages) {
  221.                 this.currentPage++;
  222.                 this.renderTable();
  223.             }
  224.         });
  225.         document.getElementById('lastPage').addEventListener('click', () => {
  226.             this.currentPage = Math.ceil(this.data.length / this.rowsPerPage);
  227.             this.renderTable();
  228.         });
  229.     }
  230. }
  231. // 初始化表格
  232. document.addEventListener('DOMContentLoaded', () => {
  233.     new DataTable();
  234. });
  235. </script>
  236. </body>
  237. </html>
复制代码
 数据示范:制表符分隔的,可以直接excel复制粘贴
  1. 姓名        学号        身份证号        科目1        科目2        科目3        科目4        科目5        科目.        科目N
  2. 李一        10001001        90001001        87        84        75        91        83        76        87
  3. 李二        10001002        90001002        95        81        81        71        60        82        99
  4. 李三        10001003        90001003        68        80        65        79        68        71        91
  5. 李四        10001004        90001004        82        80        75        90        87        64        81
  6. 李五        10001005        90001005        60        64        61        71        73        85        61
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表