Blogger Information
Blog 20
fans 0
comment 0
visits 14964
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
前端用Ajax实现数据异步加载,无刷新分页(核心思路为 事件派发+事件代理)
庖丁
Original
611 people have browsed it

1、贴个动图演示完成的效果

2、附上list.php源代码

其它代码和老师写的基本一样,以后写完整小项目时一并贴出

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>员工管理系统</title>
  8. <link rel="stylesheet" href="style.css">
  9. </head>
  10. <body>
  11. <table>
  12. <caption>员工管理系统</caption>
  13. <thead>
  14. <tr>
  15. <td>编号</td>
  16. <td>姓名</td>
  17. <td>年龄</td>
  18. <td>性别</td>
  19. <td>工资</td>
  20. <td>邮箱</td>
  21. <td>生日</td>
  22. <td>入职时间</td>
  23. <td>操作</td>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. </tbody>
  28. </table>
  29. <p></p>
  30. </body>
  31. <script>
  32. const tbody = document.querySelector("tbody");
  33. const p = document.querySelector("p");
  34. const frag = new DocumentFragment();
  35. const even = new Event('click');
  36. p.addEventListener("click", ev => {
  37. ev.preventDefault();
  38. let page_num;
  39. //利用事件代理的特性判断是否为页面第一次加载,并执行对应操作
  40. if(ev.currentTarget.innerText === ev.target.innerText){
  41. if (ev.target.innerHTML){
  42. page_num = false;
  43. }else{
  44. page_num = 1;
  45. }
  46. }else{
  47. //利用split()方法将url字符串切分为一个数组,取后面的页数
  48. page_num = (ev.target.href).split('p=');
  49. page_num = parseInt(page_num[1]);
  50. }
  51. if (page_num){
  52. // 1、创建xhr对象
  53. const xhr = new XMLHttpRequest();
  54. // 2、配置参数
  55. xhr.open('get','api.php?p=' + page_num);
  56. xhr.responseType = "json";
  57. // 3、处理响应
  58. xhr.onload=()=>{
  59. let staffs = xhr.response.staffs;
  60. let pages = parseInt(xhr.response.pages);
  61. let htmlstr = '';
  62. for(let i = 0; i <staffs.length; i++){
  63. htmlstr = htmlstr + `<tr><td>${staffs[i].sid}</td><td>${staffs[i].name}</td><td>${staffs[i].age}</td><td>${staffs[i].gender}</td><td>${staffs[i].salary}</td><td>${staffs[i].email}</td><td>${staffs[i].birthday}</td><td>${staffs[i].create_at}</td><td><button onclick="location.href='handle.php?action=edit&sid=${staffs[i].sid}'">编辑</button><button onclick="del(${staffs[i].sid})">删除</button></td></tr>`;
  64. };
  65. if (page_num !== 1){
  66. const create_a = document.createElement("a");
  67. create_a.innerText = '首页';
  68. create_a.href = `/list.php?p=1`;
  69. frag.appendChild(create_a);
  70. }
  71. if (page_num !== 1){
  72. const create_a = document.createElement("a");
  73. create_a.innerText = '上一页';
  74. create_a.href = `/list.php?p=${page_num - 1}`;
  75. frag.appendChild(create_a);
  76. }
  77. for(let i = 1; i <= pages; i++){
  78. const create_a = document.createElement("a");
  79. create_a.innerText = i;
  80. create_a.href = `/list.php?p=${i}`;
  81. if (page_num == i){
  82. create_a.classList.add("active")
  83. };
  84. frag.appendChild(create_a);
  85. };
  86. if (page_num !== pages){
  87. const create_a = document.createElement("a");
  88. create_a.innerText = '下一页';
  89. create_a.href = `/list.php?p=${page_num + 1}`;
  90. frag.appendChild(create_a);
  91. }
  92. if (page_num !== pages){
  93. const create_a = document.createElement("a");
  94. create_a.innerText = '尾页';
  95. create_a.href = `/list.php?p=${pages}`;
  96. frag.appendChild(create_a);
  97. }
  98. tbody.innerHTML = htmlstr;
  99. p.innerHTML = '';
  100. p.appendChild(frag);
  101. };
  102. // 4、发送xhr内容
  103. xhr.send(null);
  104. }
  105. });
  106. // 事件派发,触发第一次异步加载内容
  107. p.dispatchEvent(even);
  108. // 删除信息时,弹出消息提示框
  109. function del(sid) {
  110. let url = 'handle.php?action=del&sid='+sid;
  111. return confirm('是否删除编号为: '+sid+' 的员工信息?') ? location.href = url : false;
  112. }
  113. </script>
  114. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post