首页 > 其他分享 >黑马AJAX图书管理案例

黑马AJAX图书管理案例

时间:2024-08-07 15:53:31浏览次数:8  
标签:publisher edit value AJAX querySelector document data 黑马 图书

黑马AJAX图书管理案例

注意 bootstrap的css与js这里是本地导入,需要自行导入一下

这里是bootstrap下载链接

然后直接复制到vscode打开就行

代码如下

总体

<!DOCTYPE html>
<html lang="cmn-hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../bootstrap-5.3.0-alpha1-dist/css/bootstrap.min.css">
    <script src="../bootstrap-5.3.0-alpha1-dist/js/bootstrap.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <title>Document</title>
    <style>
      table{
        text-align: center;
        border: 1px solid black;
      }
      table th,td{
        border: 1px solid black;
        padding: 10px;
      }
      caption{
        text-align: center;
      }
      table td span{
        cursor: pointer;
      }
      .modal-body input{
        display: block;
        margin: 20px;
      }
    </style>
</head>
<body>
  <table>
    <caption>我是标题</caption>
    <thead>
      <tr>
        <th>序号</th>
        <th>书名</th>
        <th>作者</th>
        <th>出版社</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>
          <span class="del">删除</span>
          <span class="edit">编辑</span>
        </td>
      </tr>
    </tbody>
  </table>
  <!-- 增加按钮 -->
  <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#add">
    增加
  </button>
  
  <!-- 增加弹窗 -->
  <div class="modal fade" id="add" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h1 class="modal-title fs-5" id="exampleModalLabel">增加图书</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <input type="text" class="bookname" placeholder="书名">
          <input type="text" class="author" placeholder="作者">
          <input type="text" class="publisher" placeholder="出版社">
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
          <button type="button" class="btn btn-primary" data-bs-dismiss="modal">保存</button>
        </div>
      </div>
    </div>
  </div>

    <!-- 修改弹窗 -->
    <div class="modal fade" id="edit" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h1 class="modal-title fs-5" id="exampleModalLabel">修改图书</h1>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <input type="text" class="bookname" placeholder="书名">
            <input type="text" class="author" placeholder="作者">
            <input type="text" class="publisher" placeholder="出版社">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">保存</button>
          </div>
        </div>
      </div>
    </div>
  <script>
    const creator = '阮小航'
    // 渲染函数
    function show(){
      axios({
      url: 'http://hmajax.itheima.net/api/books',
      params: {
        creator
      }
    }).then(function(r){
      const content = r.data.data.map((item,index) => {
        return `
        <tr>
        <td>${index + 1}</td>
        <td>${item.bookname}</td>
        <td>${item.author}</td>
        <td>${item.publisher}</td>
        <td data-id=${item.id}>
          <span class="del">删除</span>
          <span class="edit" data-bs-toggle="modal" data-bs-target="#edit">编辑</span>
        </td>
        </tr>
        `
      }).join('')
      document.querySelector('tbody').innerHTML = content
    })
    }
    show()
    // 绑定增加函数
    document.querySelector('#add .btn-primary').addEventListener('click',()=>{
      axios({
        url: 'http://hmajax.itheima.net/api/books',
        method: 'post',
        data: {
          bookname: document.querySelector('.bookname').value,
          author: document.querySelector('.author').value,
          publisher: document.querySelector('.publisher').value,
          creator
        }
      }).then(()=>{
        show()
        // 清除表单内容 保证下次添加表单为空
        document.querySelector('.bookname').value=''
        document.querySelector('.author').value=''
        document.querySelector('.publisher').value=''
      })
    })
    // 存放操作图书的id变量
    let edit_id = 0
    // 通过事件委托绑定修改与删除操作
    document.querySelector('tbody').addEventListener('click', e =>{
      // 删除操作
      if(e.target.classList.contains('del')){
        axios({
          url: `http://hmajax.itheima.net/api/books/${e.target.parentNode.dataset.id}`,
          method: 'delete'
        }).then(show)
      }
      // 修改操作
      if(e.target.className==='edit'){
        axios({
          url: `http://hmajax.itheima.net/api/books/${e.target.parentNode.dataset.id}`
        }).then(r=>{
          document.querySelector('#edit .modal-body .bookname').value=r.data.data.bookname
          document.querySelector('#edit .modal-body .author').value=r.data.data.author
          document.querySelector('#edit .modal-body .publisher').value=r.data.data.publisher
          edit_id = e.target.parentNode.dataset.id
        })
      }
    })
    // 修改操作
    document.querySelector('#edit .modal-footer .btn-primary').addEventListener('click',()=>{
      axios({
        url: `http://hmajax.itheima.net/api/books/${edit_id}`,
        method: 'put',
        data: {
          bookname: document.querySelector('#edit .modal-body .bookname').value,
          author: document.querySelector('#edit .modal-body .author').value,
          publisher: document.querySelector('#edit .modal-body .publisher').value,
          creator
        }
      }).then(show)
    })
  </script>
</body>
</html>

标签:publisher,edit,value,AJAX,querySelector,document,data,黑马,图书
From: https://blog.csdn.net/2301_80293400/article/details/140994003

相关文章

  • SSM+MySQL四川工商学院图书馆管理系统-计算机毕设 附源码 03457
    SSM四川工商学院图书馆管理系统目 录摘 要1绪论1.1课题背景1.2研究目的和意义1.3国内外研究现状1.4系统设计思想1.5本章小结2 开发环境及相关技术介绍2.1MySQL数据库的介绍2.2 B/S架构的介绍2.3 Java语言2.4 SSM框架2.5本章小结3 ......
  • Java一一一简易图书管理系统
    Java一一一简易图书管理系统1.需求分析功能需求:添加图书删除图书更新图书信息查询图书列出所有图书2.设计实体类:Book业务逻辑类:LibraryManager3.实现3.1Book类publicclassBook{privateStringid;privateStringtitle;privateStringau......
  • 25届计算机毕业设计选题-基于springboot的二手图书交易系统
    博主介绍:✌十余年IT大项目实战经验、在某机构培训学员上千名、专注于本行业领域✌技术范围:Java实战项目、Python实战项目、微信小程序/安卓实战项目、爬虫+大数据实战项目、Nodejs实战项目、PHP实战项目、.NET实战项目、Golang实战项目。主要内容:系统功能设计、开题报告......
  • 基于SSM的图书馆借阅管理系统
    ✌博主介绍:全网粉丝50W+,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌技术范围:SpringBoot、Vue、SSM、HLMT、Jsp、SpringCloud......
  • jQuery入门(五)Ajax和json
    一、Ajax简介AJAX(AsynchronousJavaScriptAndXML):异步的JavaScript和XML。本身不是一种新技术,而是多个技术综合。用于快速创建动态网页的技术。一般的网页如果需要更新内容,必需重新加载个页面。而AJAX通过浏览器与服务器进行少量数据交换,就可以使网页实现异......
  • Day18_2--Vue.js Ajax(使用 Axios)基础入门学习
    Vue.js中的Ajax请求(使用Axios)什么是Axios?Axios是一个基于Promise的HTTP客户端,可以用于浏览器和Node.js环境中。它是现代化的Ajax库,用来替代传统的XMLHttpRequest。为什么选择Axios?简单易用:Axios提供了简洁且强大的API,使得发送HTTP请求变得非常简单......
  • JavaEE 图书管理系统
    基于阿里巴巴的fastjson框架搭建的JavaEE版本的图书管理系统,项目架构如下:fastjson包的阿里云下载镜像如下:CentralRepository:com/alibaba/fastjson2/fastjson2/2.0.8 运行效果:BeanBook.javapackageBean;publicclassBook{ privateStringtitle;//书名 pr......
  • Django5+Vue3:OA系统前后端分离项目实战-异步优化Ajax请求(12)
    Django5+Vue3系列文章前言本节开始,全文仅对会员开放。若点赞和收藏数量超过100,全文将免费开放。此项目采用Django框架的5.0.7版本进行开发。Django5.0支持的Python版本为3.10、3.11和3.12。OA系统系列文章将持续更新,直至项目的Docker部署阶段。专栏链接:......
  • JavaWeb之servlet关于Ajax实现前后端分离
    一、什么是Ajax:AJAX=AsynchronousJavaScriptandXML(异步的JavaScript和XML)。AJAX不是新的编程语言,而是一种使用现有标准的新方法。AJAX最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容。AJAX不需要任何浏览器插件,但需要用户允......
  • SpringBoot+Vue图书(图书借阅)管理系统-附项目源码与配套文档
    摘 要 本论文阐述了一套先进的图书管理系统的设计与实现,该系统采用Java语言,结合现代Web开发框架和技术,旨在为图书馆提供高效、灵活且用户友好的资源管理解决方案。系统利用SpringBoot框架为核心,整合MyBatisORM工具,以及MySQL数据库,构建了一个高性能、可扩展的后端服务。前......