黑马AJAX图书管理案例

黑马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>
  • 23
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下为B站黑马JavaScriptAjax案例代码——图书管理案例搜索事件: ```javascript /** * 搜索事件 */ function searchBook() { // 获取搜索框内容 var searchInput = document.querySelector("#searchInput"); var keywords = searchInput.value.trim(); // 如果搜索框内容为空,提示用户 if (keywords == "") { alert("请输入关键字进行搜索!"); return; } // 发送Ajax请求 var xhr = new XMLHttpRequest(); // 创建XmlHttpRequest对象 xhr.open("GET", "/api/book/search?keywords=" + keywords, true); // 配置请求 xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { // 处理响应数据 var books = JSON.parse(xhr.responseText); // 将响应数据转成JavaScript对象 renderBooks(books); // 渲染图书列表 } }; xhr.send(null); // 发送请求 } /** * 渲染图书列表 * @param {*} books 图书列表数据 */ function renderBooks(books) { // 获取图书列表容器 var bookList = document.querySelector("#bookList"); var html = ""; if (books.length > 0) { // 遍历图书列表数据,生成HTML字符串 for (var i = 0; i < books.length; i++) { html += '<tr>'; html += '<td>' + books[i].title + '</td>'; html += '<td>' + books[i].author + '</td>'; html += '<td>' + books[i].category + '</td>'; html += '<td>' + books[i].price + '</td>'; html += '<td>' + books[i].publisher + '</td>'; html += '</tr>'; } } else { // 如果没有搜索到任何图书,提示用户 html = '<tr><td colspan="5">没有搜索到任何图书!</td></tr>'; } // 将生成的HTML字符串添加进图书列表容器 bookList.innerHTML = html; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阮小航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值