• AJAX——图书管理案例


    1.渲染列表

    自己的图书数据:给自己起个外号,并告诉服务器,默认会有三本书,基于这三本书做数据的增删改查。

    1. // 目标1:渲染图书列表
    2. // 1.1 获取数据
    3. // 1.2 渲染数据
    4. const creator = '哈哈'
    5. // 封装-获取并渲染图书列表函数
    6. function getBooksList(){
    7. //1.1 获取数据
    8. axios({
    9. url:'http://hmajax.itheima.net/api/books',
    10. params:{
    11. // 外号:获取对应数据
    12. creator
    13. }
    14. }).then(result => {
    15. console.log(result)
    16. const bookList = result.data.data
    17. console.log(bookList)
    18. //1.2 渲染数据
    19. const htmlStr = bookList.map((item, index) => {
    20. return `<tr>
    21. <td>${ index + 1}</td>
    22. <td>${ item.bookname }</td>
    23. <td>${ item.author }</td>
    24. <td>${ item.publisher }</td>
    25. <td>
    26. <span class="del">删除</span>
    27. <span class="edit">编辑</span>
    28. </td>
    29. </tr>
    30. `
    31. }).join('')
    32. console.log(htmlStr)
    33. document.querySelector('.list').innerHTML = htmlStr
    34. })
    35. }
    36. // 网页加载运行,获取并渲染列表一次
    37. getBooksList()

    2.新增图书

    1. /**
    2. * 目标2:新增图书
    3. * 2.1 新增弹框 -> 显示和隐藏
    4. * 2.2 收集表达数据,并提交到服务器保存
    5. * 2.3 刷新图书列表
    6. */
    7. // 2.1 创建弹框对象
    8. const addModalDom = document.querySelector('.add-modal')
    9. const addModal = new bootstrap.Modal(addModalDom)
    10. // 保存按钮 -> 点击 -> 隐藏弹框
    11. document.querySelector('.add-btn').addEventListener('click', () => {
    12. // 2.2 收集表单数据,并提交到服务器保存
    13. const addForm = document.querySelector('.add-form')
    14. const bookObj = serialize(addForm, { hash: true, empty: true})
    15. console.log(bookObj)
    16. // 提交到服务器
    17. axios({
    18. url: 'http://hmajax.itheima.net/api/books',
    19. method: 'POST',
    20. data: {
    21. ...bookObj,
    22. creator
    23. }
    24. }).then(result => {
    25. console.log(result)
    26. // 2.3 添加成功后,重新请求并渲染图书列表
    27. getBooksList()
    28. // 重置表单
    29. addForm.reset()
    30. // 隐藏弹框
    31. addModal.hide()
    32. })
    33. })

    3.删除图书

    1. /**
    2. * 目标3: 删除图书
    3. * 3.1 删除元素绑定点击事件 -> 获取图书
    4. * 3.2 调用删除接口
    5. * 3.3 刷新图书列表
    6. */
    7. // 3.1 删除元素 -> 点击(事件委托)
    8. document.querySelector('.list').addEventListener('click', e => {
    9. // 获取触发事件目标元素
    10. // console.log(e.target)
    11. // 判断点击的是删除元素
    12. if (e.target.classList.contains('del')) {
    13. // console.log('点击删除元素')
    14. // 获取图书id(自定义属性id)
    15. const theId = e.target.parentNode.dataset.id
    16. console.log(theId)
    17. // 3.2 调用删除接口
    18. axios({
    19. url: `http://hmajax.itheima.net/api/books/${theId}`,
    20. method:'DELETE'
    21. }).then(() => {
    22. //3.3 刷新图书列表
    23. getBooksList()
    24. })
    25. }
    26. })

    4.编辑图书

    1. /**
    2. * 目标4: 编辑图书
    3. * 4.1 编辑弹框 -> 显示和隐藏
    4. * 4.2 获取当前编辑图书数据 -> 回显到编辑表单中
    5. * 4.3 提交保存修改,并刷新列表
    6. */
    7. // 4.1 编辑弹框 -> 显示和隐藏
    8. const editDom = document.querySelector('.edit-modal')
    9. const editModal = new bootstrap.Modal(editDom)
    10. //编辑元素 -> 点击 -> 弹框显示
    11. document.querySelector('.list').addEventListener('click', e => {
    12. // 判断点击的是否为编辑元素
    13. if (e.target.classList.contains('edit')) {
    14. // 4.2 获取当前编辑图书数据 -> 回显到编辑表单中
    15. const theId = e.target.parentNode.dataset.id
    16. // console.log(theId)
    17. axios({
    18. url: `http://hmajax.itheima.net/api/books/${theId}`
    19. }).then(result => {
    20. // console.log(result)
    21. const bookObj = result.data.data
    22. // document.querySelector('.edit-form .bookname').value = bookObj.bookname
    23. // document.querySelector('.edit-form .author').value = bookObj.author
    24. // 数据对象“属性”和标签“类名”一致
    25. // 遍历数据对象,使用属性去获取对于的标签,快速赋值
    26. const keys = Object.keys(bookObj) // ['id','bookname','author','publisher']
    27. // console.log(keys)
    28. keys.forEach(key => {
    29. document.querySelector(`.edit-form .${key}`).value = bookObj[key]
    30. })
    31. })
    32. editModal.show()
    33. }
    34. })
    35. // 修改按钮 -> 点击 -> 隐藏弹框
    36. document.querySelector('.edit-btn').addEventListener('click', () => {
    37. // 4.3 提交保存修改,并刷新列表
    38. const editForm = document.querySelector('.edit-form')
    39. const {id, bookname, author, publisher} = serialize(editForm, { hash: true, empty: true})
    40. // console.log(bookObj)
    41. // 保存正在编辑的图书id,隐藏起来,无需让用户修改
    42. //
    43. axios({
    44. url: `http://hmajax.itheima.net/api/books/${id}`,
    45. method: 'PUT',
    46. data: {
    47. bookname,
    48. author,
    49. publisher,
    50. creator
    51. }
    52. }).then(() => {
    53. // 修改成功以后,重新获取并刷新列表
    54. getBooksList()
    55. // 隐藏弹框
    56. editModal.hide()
    57. })
    58. })

    index.html代码 

    1. DOCTYPE html>
    2. 案例-图书管理
    3. 图书管理

  • 序号书名作者出版社操作
    1JavaScript程序设计马特·弗里斯比人民邮电出版社
  • 删除
  • 编辑
  • 添加图书
  • 编辑图书
  • index.css

    1. /* 公共*/
    2. html,
    3. body {
    4. width: 100%;
    5. height: 100%;
    6. }
    7. .container {
    8. width: 1340px;
    9. margin: 0 auto;
    10. padding-top: 60px;
    11. box-sizing: border-box;
    12. }
    13. /* alert提示框 */
    14. .toast {
    15. position: fixed;
    16. top: 20px;
    17. left: 50%;
    18. transform: translateX(-50%);
    19. }
    20. .toast .toast-body {
    21. padding: 0 !important;
    22. }
    23. .toast .alert-success {
    24. margin-bottom: 0 !important;
    25. }
    26. /* 头部导航 */
    27. .container .top {
    28. display: flex;
    29. justify-content: space-between;
    30. }
    31. .container .top h3 {
    32. font-weight: 900;
    33. }
    34. .container .top .plus-btn {
    35. background-color: #539ACB !important;
    36. color: #fff;
    37. border: none;
    38. }
    39. /* 表格部分 */
    40. .table {
    41. margin-top: 20px;
    42. text-align: center;
    43. font-size: 14px;
    44. }
    45. .table-light th {
    46. background-color: #939CA7 !important;
    47. color: #ffffff;
    48. font-family: PingFangSC-Medium;
    49. font-size: 16px;
    50. text-align: center;
    51. font-weight: 500;
    52. border-right: 1px solid lightgray;
    53. }
    54. .table-light th:last-of-type {
    55. border-right: none;
    56. }
    57. /* 表格内容 */
    58. .table tbody td {
    59. color: #696F77;
    60. }
    61. .table .del {
    62. color: #E5964C;
    63. margin-right: 30px;
    64. }
    65. .table .edit {
    66. color: #539ACB;
    67. }
    68. .table tbody tr {
    69. height: 30px;
    70. line-height: 30px;
    71. }
    72. .table tbody tr td:last-of-type span {
    73. cursor: pointer;
    74. }
    75. /* 弹出层 */
    76. .modal .top {
    77. display: flex;
    78. justify-content: center;
    79. background-color: #F0F3F7;
    80. padding: 15px 0;
    81. width: 100%;
    82. position: relative;
    83. color: #1E2023;
    84. }
    85. /* 右上角-关闭按钮 */
    86. .modal .btn-close {
    87. font-size: 12px;
    88. position: absolute;
    89. top: 50%;
    90. transform: translateY(-50%);
    91. right: 23px;
    92. /* 覆盖bootstrap样式 */
    93. margin: 0;
    94. padding: 0;
    95. }
    96. /* 表单容器 */
    97. .form-wrap {
    98. box-sizing: border-box;
    99. background-color: white;
    100. padding: 40px;
    101. }
    102. .form-wrap .form-label {
    103. color: #696F77;
    104. }
    105. /* 修改输入框默认占位文字
    106. webkit内核, firefox18-, firfox19+, 其他
    107. */
    108. .form-wrap input::-webkit-input-placeholder {
    109. color: #BFBFBF !important;
    110. font-size: 14px;
    111. }
    112. /* 底部按钮组 */
    113. .modal-footer{
    114. border-top: 0;
    115. }
    116. .btn-group {
    117. text-align: center;
    118. width: 100%;
    119. }
    120. /* 修改bs的按钮弹性布局-改成自己设置大小 */
    121. .btn-group,
    122. .btn-group-vertical {
    123. display: block;
    124. }
    125. .btn-group button {
    126. width: 120px
    127. }
    128. .btn-group button:first-of-type {
    129. border: 1px solid #E3E3E3;
    130. background-color: #fff;
    131. color: black;
    132. margin-right: 60px;
    133. }
    134. .btn-group button:last-of-type {
    135. background-color: #539ACB;
    136. }
    137. .alert-success {
    138. border-color: transparent;
    139. }
    140. .toast {
    141. border: none;
    142. }

  • 相关阅读:
    Soul CEO张璐团队以用户安全为核心,探索社交平台安全治理新路径
    【区块链 | 智能合约】Ethereum源代码(10)- 以太坊Downloader源码分析
    nodejs+vue+elementui高校人事管理系统
    揭秘一线大厂Redis面试高频考点(3万字长文、吐血整理)
    【深度学习 Pytorch笔记 B站刘二大人 非线性回归 Logistics-Regression 模块实现与源码解读(5/12)】
    org.apache.ibatis.exceptions.PersistenceException:
    数据库数据恢复—Oracle数据库报错ORA-01110错误的数据恢复案例
    流体动力润滑(轴承油膜承载机理)
    【Azure】解析 Microsoft Defender for Cloud:云安全的保护与管理
    基于JAVA-小型酒店管理系统-计算机毕业设计源码+数据库+lw文档+系统+部署
  • 原文地址:https://blog.csdn.net/weixin_48719464/article/details/136811254