Blogger Information
Blog 26
fans 2
comment 0
visits 24415
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用AJAX实现无刷新分页
leverWang
Original
669 people have browsed it

index.html

  1. <!-- @format -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <script src="https://s2.pstatp.com/cdn/expire-1-M/jquery/3.4.0/jquery.min.js"></script>
  9. <script src="data.js"></script>
  10. <style>
  11. table {
  12. border-collapse: collapse;
  13. border: 1px solid;
  14. text-align: center;
  15. margin: auto;
  16. width: 500px;
  17. }
  18. table caption {
  19. font-size: 1.2rem;
  20. margin-bottom: 10px;
  21. }
  22. th,
  23. td {
  24. border: 1px solid;
  25. padding: 5px;
  26. }
  27. thead tr:first-of-type {
  28. background-color: #ddd;
  29. }
  30. p {
  31. text-align: center;
  32. }
  33. p a {
  34. text-decoration: none;
  35. border: 1px solid;
  36. padding: 0 8px;
  37. }
  38. .active {
  39. background-color: #f00;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <table>
  45. <caption>
  46. 用户信息表
  47. </caption>
  48. <thead>
  49. <tr>
  50. <th>id</th>
  51. <th>姓名</th>
  52. <th>年龄</th>
  53. <th>性别</th>
  54. <th>职位</th>
  55. <th>手机号</th>
  56. </tr>
  57. </thead>
  58. <tbody></tbody>
  59. </table>
  60. <p></p>
  61. </body>
  62. </html>

data.js

  1. $(document).ready(function () {
  2. var page = 1;
  3. getData(page);
  4. //发送请求
  5. function getData(page) {
  6. $.ajax({
  7. type: "POST",
  8. url: "data.php",
  9. data: { page: page },
  10. dataType: "json",
  11. success: show,
  12. });
  13. }
  14. //定义数据渲染方法
  15. function show(data) {
  16. // console.log(data);
  17. //填充默认数据
  18. console.log(page);
  19. var str = "";
  20. data.res.forEach(function (item) {
  21. str += "<tr>";
  22. str += "<td>" + item.id + "</td>";
  23. str += "<td>" + item.name + "</td>";
  24. str += "<td>" + item.age + "</td>";
  25. str += "<td>" + item.sex + "</td>";
  26. str += "<td>" + item.position + "</td>";
  27. str += "<td>" + item.mobile + "</td>";
  28. str += "</tr>";
  29. });
  30. $("tbody").html(str);
  31. //分页
  32. var str = "";
  33. for (var i = 1; i < data.pages; i++) {
  34. str += '<a href="" data-index=' + i + ">" + i + "</a>";
  35. }
  36. //为当前的页码添加样式
  37. $("p")
  38. .html(str)
  39. .find("a")
  40. .eq(page - 1)
  41. .first()
  42. .addClass("active");
  43. $("p a").on("click", function (ev) {
  44. console.log(this);
  45. ev.preventDefault();
  46. // // 获取当前要显示的新页面,根据自定义属性
  47. page = $(this).attr("data-index");
  48. getData(page);
  49. });
  50. }
  51. });

data.php

  1. <?php
  2. $num = 10;
  3. $page = $_POST['page'] ?? 1;
  4. $offset = ($page - 1) * $num;
  5. $dsn = "mysql:host=localhost;dbname=phpedu";
  6. $pdo = new PDO($dsn, 'root', 'root');
  7. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `Total` FROM users ";
  8. $pages = $pdo->query($sql)->fetch()['Total'];
  9. $stmt = $pdo->prepare('select * from users limit ? offset ?');
  10. $stmt->bindParam(1, $num, PDO::PARAM_INT);
  11. $stmt->bindParam(2, $offset, PDO::PARAM_INT);
  12. $stmt->execute();
  13. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  14. echo json_encode(['pages'=>$pages,'res'=>$res]);

效果如下:

总结:学会使用JQ的ajax方法请求数据,并结合前面的知识将数据动态渲染到页面中.
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