一、无刷新分页

1.实现原理:前端通过ajax请求获取服务器数据,再由JS把数据组成html渲染到页面中
2.服务器php代码(获取分页数据信息)

  1. <?php
  2. require('connection.php');//链接数据库操作
  3. //获取当前数据记录总条数
  4. $sql="select count(*) as 'nums' from v_staffs;";
  5. $stmt=$pdo->prepare($sql);
  6. $stmt->execute();
  7. $result=$stmt->fetch();
  8. // var_dump($result);
  9. $nums=$result['nums'];
  10. $num=10;//每页显示记录数
  11. $pages=ceil($nums/$num);//获取总页数
  12. // echo $pages;
  13. // 获取当前分页的数据记录信息
  14. $sql='select * from v_staffs limit ?,?;';
  15. // echo $_GET['p'];
  16. $page=$_GET['p']??1;//页码数
  17. $start=($page-1)*$num;//通过页码数获取起始记录数
  18. $stmt=$pdo->prepare($sql);
  19. $stmt->bindParam(1,$start,PDO::PARAM_INT);
  20. $stmt->bindParam(2,$num,PDO::PARAM_INT);
  21. $stmt->execute();
  22. // echo print_r($stmt->fetchAll(),true);
  23. $rows=$stmt->fetchAll();
  24. $res=['data'=>$rows,'pages'=>$pages];
  25. echo json_encode($res);
  26. // return json_encode($rows);

2.前端js获取数据请求和页面渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  9. </head>
  10. <body>
  11. <main>
  12. <table border="1" cellpadding='3' cellspacing='0'>
  13. <caption>员工信息表</caption>
  14. <tr id="title">
  15. <th>编号</th>
  16. <th>姓名</th>
  17. <th>年龄</th>
  18. <th>性别</th>
  19. <th>工资</th>
  20. <th>邮箱</th>
  21. <th>职位</th>
  22. <th>负责区域</th>
  23. <th>操作</th>
  24. </tr>
  25. </table>
  26. <!--页码部分 -->
  27. <div id='page'>
  28. </div>
  29. </main>
  30. </body>
  31. <script>
  32. //页面主体内容获取和生成函数
  33. function getrows(data) {
  34. //如果页面已经记录信息需要移除
  35. if ($($("table>tbody")[0]).children().length > 1) {
  36. $($("table>tbody")[0]).children().remove("tr:not(#title)");
  37. };
  38. //生成html内容
  39. var content = "";
  40. data.forEach((item) => {
  41. content += `<tr>
  42. <td>${item["id"]}</td>
  43. <td>${item["name"]}</td>
  44. <td>${item["age"]}</td>
  45. <td>${item["gender"]}</td>
  46. <td>${item["salary"]}</td>
  47. <td>${item["email"]}</td>
  48. <td>${item["postion"]}</td>
  49. <td>${item["area"]}</td>
  50. <td><button><a href="http://php.edu/test/edit.php?id=${item["id"]}" target="_blank">编辑</a></button><button class="delete" data-index="${item["id"]}">删除</button></td>
  51. </tr>
  52. `;
  53. });
  54. //渲染到页面中取
  55. $('#title').after(content);
  56. }
  57. //生成分条条函数
  58. function getpage(pages) {
  59. //如果没有分页条则生成
  60. if ($("#page").children().length == 0) {
  61. var plink = "<span>首页</span><span>&lt</span>";
  62. for (var i = 1; i <= pages; i++) {
  63. plink += `<span>${i}</span>`;
  64. }
  65. plink += "<span>&gt;</span><span>尾页</span>"
  66. // console.log(plink);
  67. $("#page").append(plink);
  68. }
  69. // console.log($("#page").children().length);
  70. }
  71. //ajax获取数据信息函数
  72. function getdata(page) {
  73. $.ajax({
  74. type: 'GET',
  75. url: 'http://php.edu/test/staffs_api.php',
  76. data: {
  77. p: page
  78. },
  79. dataType: 'json',
  80. success: function(res) {
  81. // console.log(res);
  82. var data = res['data'];
  83. var pages = res['pages'];
  84. // console.log(data, pages);
  85. //调用函数生成页面信息
  86. getrows(data);
  87. getpage(pages);
  88. }
  89. });
  90. }
  91. //首次打开页面默认获取首页数据信息并展示
  92. window.onload = function() {
  93. getdata(1);
  94. }
  95. //通过委托事件实现翻页(上一页下一页功能没有实现)
  96. $('#page').click(function(ev) {
  97. // console.log(ev.target.textContent, ev.currentTarget);
  98. var currentpage = ev.target.textContent;
  99. switch (currentpage) {
  100. case "首页":
  101. getdata(1);
  102. break;
  103. case "尾页":
  104. var count = $(ev.currentTarget).children().length - 4;
  105. getdata(count);
  106. break;
  107. case "<":
  108. console.log('上一页');
  109. break;
  110. case ">":
  111. console.log("下一页");
  112. break;
  113. default:
  114. getdata(currentpage);
  115. }
  116. });
  117. //删除数据功能,通过ajaxComplete()函数来获取动态生成的信息
  118. $(document).ajaxComplete(function() {
  119. // console.log($('.delete'));
  120. $('.delete').click(function(ev) {
  121. var id = $(ev.target).data("index");
  122. // console.log(id);
  123. if (window.confirm("确定删除吗?")) {
  124. $.ajax({
  125. type: 'GET',
  126. url: 'http://php.edu/test/delete.php',
  127. data: {
  128. id: id
  129. },
  130. dataType: 'json',
  131. success: function(res) {
  132. if (res[0] > 0) {
  133. return alert("删除成功");
  134. } else {
  135. return alert("删除失败");
  136. }
  137. }
  138. });
  139. window.location.reload();
  140. } else {
  141. return false;
  142. }
  143. });
  144. });
  145. </script>
  146. <style>
  147. main {
  148. width: 1000px;
  149. margin: 10px auto;
  150. }
  151. table {
  152. width: 1000px;
  153. }
  154. #page {
  155. margin-top: 10px;
  156. width: 1000px;
  157. display: flex;
  158. justify-content: space-around;
  159. }
  160. #page>span {
  161. width: 46px;
  162. height: 20px;
  163. background-color: lightgray;
  164. /* border: 1px solid black; */
  165. text-align: center;
  166. color: whitesmoke;
  167. line-height: 20px;
  168. }
  169. #page>span:hover {
  170. cursor: pointer;
  171. background-color: white;
  172. color: red;
  173. }
  174. th,
  175. td {
  176. text-align: center;
  177. }
  178. </style>
  179. </html>

二、编辑功能

(一)功能实现原理
1.通过分页的编辑按钮获取要编辑数据记录的id,然后通过编辑页面把要编辑的记录显示出来,
2.通过ajax获取新的数据,通过id,更新信息
(二)实现代码
1.前端代码

  1. <?php
  2. include('connection.php');
  3. $sql="select * from v_staffs where id=:id;";
  4. $stmt=$pdo->prepare($sql);
  5. $id=$_GET["id"];
  6. $stmt->bindParam(':id',$id,PDO::PARAM_INT);
  7. $stmt->execute();
  8. // $stmt->debugDumpParams();
  9. $row=$stmt->fetch();
  10. // var_dump($row);
  11. // exit();
  12. ?>
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <head>
  16. <meta charset="UTF-8">
  17. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  19. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  20. <title>员工信息修改</title>
  21. </head>
  22. <body>
  23. <body>
  24. <table border="1" cellpadding='3' cellspacing='0'>
  25. <caption>修改员工信息</caption>
  26. <tr>
  27. <th>编号</th>
  28. <th>姓名</th>
  29. <th>年龄</th>
  30. <th>性别</th>
  31. <th>工资</th>
  32. <th>邮箱</th>
  33. </tr>
  34. <tr>
  35. <td id="id"><?= $row["id"] ?></td>
  36. <td><input type="text" name="name" value="<?= $row["name"] ?>"></td>
  37. <td><input type="text" name="age" value="<?= $row["age"] ?>"></td>
  38. <td><input type="text" name="gender" value="<?= $row["gender"] ?>"></td>
  39. <td><input type="text" name="salary" value="<?= $row["salary"] ?>"></td>
  40. <td><input type="text" name="email" value="<?= $row["email"] ?>"></td>
  41. </tr>
  42. </table>
  43. <button id="update">提交</button>
  44. </body>
  45. <script>
  46. $("#update").click(function(){
  47. $.ajax({
  48. type: 'POST',
  49. url: 'http://php.edu/test/update.php',
  50. data: {
  51. //获取新数据
  52. name: $("[name='name']").val(),
  53. age:$("[name='age']").val(),
  54. gender:$("[name='gender']").val(),
  55. salary:$("[name='salary']").val(),
  56. email:$("[name='email']").val(),
  57. id:$("#id").text()
  58. },
  59. dataType: 'json',
  60. success: function(res) {
  61. //确认更新成功
  62. alert(res[1]);
  63. window.close();//关闭窗口
  64. }
  65. });
  66. });
  67. </script>
  68. </html>

2.后端代码

  1. <?php
  2. include('connection.php');
  3. $sql="update staffs set name=?,age=?,gender=?,salary=?,email=? where id=?;";
  4. $stmt=$pdo->prepare($sql);
  5. $stmt->execute([$_POST['name'],$_POST["age"],$_POST["gender"],$_POST["salary"],$_POST["email"],$_POST['id']]);
  6. // $stmt->debugDumpParams();
  7. if($stmt->rowCount()>0){
  8. echo json_encode([$stmt->rowCount(),"更新成功"]);
  9. }else{
  10. echo json_encode([0,"更新失败"]);
  11. }

运行效果图

更多相关文章

  1. 第11章 0223-命名空间2与数据库基础1,学习心得、笔记(命名空间引
  2. js的变量与常量、常用数据
  3. 实战:从Mysql数据库frm文件中,提取表结构创建SQL语句
  4. MySQL安全性解决方案
  5. 生产系统数据丢失恢复案例
  6. IntelliJ IDEA 激活码 2021.02.28日更新
  7. 2021-2-28
  8. PHP实战: 实现数据的分页显示和编辑,删除功能.(无刷新,基于ajax)
  9. FreeRTOS例程3-串口中断接收不定长的数据与二值信号量的使用

随机推荐

  1. 报错 src/png_io.c:3:17: error: png.h:
  2. Python语法教程总结规范
  3. python中字典的操作
  4. Python:运算类内建函数列举
  5. Python学习总结-(15)---返回函数和闭包初步
  6. 卸载gcc,ubuntu系统崩溃解决
  7. Python语言及其应用 PDF
  8. Django:测试成功加载静态文件
  9. 如何将json转换为对象?
  10. 环境变量的安装以及python cahrm的安装以