Blogger Information
Blog 59
fans 6
comment 0
visits 52194
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Ajax-post无刷新分页技术-js-44课8.20
希望
Original
659 people have browsed it

一、Ajax-post无刷新分页技术

1、新建demo3.html,制作表单和样式,引入jquery-3.5.1.js、并指定url地址:page_data.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Ajax-post无刷新分页技术</title>
  7. <style>
  8. table {
  9. border-collapse: collapse;
  10. border: 1px solid;
  11. text-align: center;
  12. margin: auto;
  13. width: 500px;
  14. }
  15. table caption {
  16. font-size: 1.2rem;
  17. margin-bottom: 10px;
  18. }
  19. th,
  20. td {
  21. border: 1px solid;
  22. padding: 5px;
  23. }
  24. thead tr:first-of-type {
  25. background-color: #ddd;
  26. }
  27. p {
  28. text-align: center;
  29. }
  30. p a {
  31. text-decoration: none;
  32. border: 1px solid;
  33. padding: 0 8px;
  34. }
  35. .active {
  36. background-color: rgb(223, 87, 121);
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <table>
  42. <caption>
  43. 用户信息表
  44. </caption>
  45. <thead>
  46. <tr>
  47. <th>id</th>
  48. <th>姓名</th>
  49. <th>年龄</th>
  50. <th>性别</th>
  51. <th>职位</th>
  52. <th>手机号</th>
  53. </tr>
  54. </thead>
  55. <tbody></tbody>
  56. </table>
  57. <!-- 分页条 -->
  58. <p></p>
  59. <script src="../0818/lib/jquery-3.5.1.js"></script>
  60. <script>
  61. // 默认是第一页
  62. var page = 1;
  63. // 默认显示的是第一页
  64. getPageData(page);
  65. // 获取分页数据的函数
  66. function getPageData(page) {
  67. $.ajax({
  68. type: "post",
  69. url: "page_data.php",
  70. data: { page: page },
  71. dataType: "json",
  72. success: show,
  73. });
  74. }
  75. // show()显示数据
  76. function show(data) {
  77. console.log(data);
  78. // 将json中的数据解析出来填充到表格中
  79. console.log(data.users);
  80. // 1. 将当前页面的数据渲染出来
  81. var str = "";
  82. data.users.forEach(function (user) {
  83. str += "<tr>";
  84. str += "<td>" + user.id + "</td>";
  85. str += "<td>" + user.name + "</td>";
  86. str += "<td>" + user.age + "</td>";
  87. str += "<td>" + user.sex + "</td>";
  88. str += "<td>" + user.position + "</td>";
  89. str += "<td>" + user.mobile + "</td>";
  90. str += "</tr>";
  91. });
  92. $("tbody").html(str);
  93. // 2. 将分页条显示出来
  94. var str = "";
  95. for (var i = 1; i < data.pages; i++) {
  96. str += '<a href="" data-index=' + i + ">" + i + "</a>";
  97. }
  98. $("p").html(str);
  99. // 高亮显示,遍历所有分页
  100. for (var i = 1; i <= data.pages; i++) {
  101. if (data.postPage == i) {
  102. var finda = "p a:nth-of-type(" + data.postPage + ")";
  103. $(finda).addClass("active");
  104. }
  105. }
  106. // 3. 添加分页点击事件
  107. $("p a").click(function (ev) {
  108. ev.preventDefault();
  109. // 获取当前要显示的新页面,根据自定义属性
  110. var page = $(this).attr("data-index");
  111. // $("tbody").html("");
  112. getPageData(page);
  113. });
  114. }
  115. </script>
  116. </body>
  117. </html>

2、创建page_data.php,连接数据库

  1. <?php
  2. // 1. 连接数据库
  3. $pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'melinda123');
  4. // 2. 获取页码
  5. $page = $_POST['page'] ?? 1;
  6. // 3. 每页显示数量
  7. $num = 8;
  8. // 4. 每页显示偏移量
  9. $offset = ($page - 1) * $num;
  10. // 5. 总页数
  11. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `users`";
  12. // echo $sql;
  13. $pages = $pdo->query($sql)->fetch()['total'];
  14. // echo $pages;
  15. // 6. 分页数据
  16. $sql = "SELECT * FROM `users` LIMIT {$num} OFFSET {$offset}";
  17. $users = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  18. // ajax分页数据一定是返回二部分
  19. // 1. 总页数, 提供给前端自动生成分页条
  20. // 2. 分页数据
  21. echo json_encode(['pages' => $pages, 'users' => $users,'postPage'=>$page]);
  22. die;
  • 总结:
  • 获取所有数据,设置每页数量,计算总页数,制作分页、点击、高亮
  • 用Ajax、json、post、show()、click事件、active等来实现无刷新点击分页高亮显示
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