前端(AJAX)学习笔记(CLASS 2):图书管理案例以及图片上传

embedded/2025/2/22 17:47:09/

* BootStrap弹框

功能:不离开当前页面,显示单独内容,供用户操作

步骤:

1、引入bootstrap.css和bootstrap.js

2、准备弹框标签,确认结构

3、通过自定义属性,控制弹框的显示和隐藏

其中的bootstrap.css链接为:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

bootstrap链接为:

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>

以下是模板代码:

      <div class="modal fade" id="exampleModal" 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">Modal title</h1><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body">...</div><div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button><button type="button" class="btn btn-primary">Save changes</button></div></div></div></div>

利用自定义属性控制弹框的显示和隐藏

data-bs-toggle="modal"

data-bs-target="#exampleModal"

示例的按钮:

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">Launch demo modal
</button>

如果使用js进行控制:

        //创建弹框对象const modal=new bootstrap.Modal('css选择器')//显示弹框modal.show()//隐藏弹框modal.hide()

1、渲染列表

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

其中的图书列表接口为:

http://ajax-api.itheima.net/api/books

首先基本的html与css代码为以下:

    <!-- 操作列 --><div class="head"><h1 class="title">图书管理</h1><button class="add_btn">添加</button></div>
    <!-- 图书管理列表 --><table><thead><tr><th>序号</th><th>书名</th><th>作者</th><th>出版社</th><th>操作</th></th></thead><tbody><!-- <tr><th>1</th><th>《java程序设计》</th><th>xxx</th><th>高等教育出版社</th><th><span class="del">删除</span><span class="edi">编辑</span></th></tr> --></tbody></table>

以下代码为使用BooyStrap的添加图书弹窗

    <!-- 添加图书弹窗 --><div class="modal fade" id="exampleModal" 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"><div>书名</div><input class="bookname" type="text" placeholder="请输入书籍名称"><div>作者</div><input class="author" type="text" placeholder="请输入作者名称"><div>出版社</div><input class="publisher" type="text" 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">确定</button></div></div></div></div>

下面为添加图书的js部分:

        const tbody=document.querySelector('tbody')function getBook() {axios({url:'http://ajax-api.itheima.net/api/books'}).then(res => {console.log(res)tbody.innerHTML=res.data.data.map((item,index) =>`<tr><th>${index+1}</th><th>${item.bookname}</th><th>${item.author}</th><th>${item.publisher}</th><th class='${item.id}'><span class="del">删除</span><span class="edi">编辑</span></th></tr>`)})}getBook()// 添加图书操作//创建弹框对象const modal=new bootstrap.Modal('.modal')const add_btn=document.querySelector('.add_btn')        const bookDec=document.querySelectorAll('.modal-body input')//显示弹框        add_btn.addEventListener('click',() => {for(let i=0;i<bookDec.length;i++){bookDec[i].value=null}modal.show() })// 添加图书操作document.querySelector('.btn-primary').addEventListener('click',() =>{axios({url:'http://ajax-api.itheima.net/api/books',method:'post',data:{bookname: bookDec[0].value,author:bookDec[1].value,publisher:bookDec[2].value}}).then(res => {getBook()modal.hide()})})

2、删除数据

当点击删除按钮时,该行的数据将会被删除,并在页面中重新渲染

        // 删除操作document.querySelector('tbody').addEventListener('click',e => {if(e.target.className==='del'){console.log(e.target.parentNode.className);const id=e.target.parentNode.classNameaxios({url:`http://ajax-api.itheima.net/api/books/${id}`,method:'delete'}).then(res => {getBook()})}})

3、修改数据

修改数据部分包括了数据回显以及修改数据

当点击编辑按钮时,会弹出编辑图书弹框

      <!-- 编辑图书弹窗 --><div class="modal fade modal_edi" id="exampleModal" 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 modal_body_edi"><div>书名</div><input class="bookname" type="text" placeholder="请输入书籍名称"><div>作者</div><input class="author" type="text" placeholder="请输入作者名称"><div>出版社</div><input class="publisher" type="text" 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 edi_btn">修改</button></div></div></div></div>

再修改数据后,点击修改按钮,将进行修改

       // 点击编辑,获取图书信息const modal_edi=new bootstrap.Modal('.modal_edi')const modal_body_edi=document.querySelectorAll('.modal_body_edi input')document.querySelector('tbody').addEventListener('click',e => {if(e.target.className==='edi'){modal_edi.show()console.log(e.target.parentNode.parentNode.children[0]);const id=e.target.parentNode.classNameaxios({url:`http://ajax-api.itheima.net/api/books/${id}`}).then(res => {console.log(res.data.data)modal_body_edi[0].value=res.data.data.booknamemodal_body_edi[1].value=res.data.data.authormodal_body_edi[2].value=res.data.data.publisher//点击修改按钮,对图书信息进行修改document.querySelector('.edi_btn').addEventListener('click',() => {axios({url:`http://ajax-api.itheima.net/api/books/${id}`,method:'put',data:{bookname:modal_body_edi[0].value,author:modal_body_edi[1].value,publisher:modal_body_edi[2].value}}).then(edi_res =>{getBook()})modal_edi.hide()})}).catch(err => console.log(err))}})

* 图片上传

1、获取图片文件对象

2、使用FormData携带图片文件

const fd=new FormData()
fd.append(参数名,值)

3、提交表单数据到服务器,使用图片url网址


http://www.ppmy.cn/embedded/164398.html

相关文章

MAC快速本地部署Deepseek (win也可以)

MAC快速本地部署Deepseek (win也可以) 下载安装ollama 地址: https://ollama.com/ Ollama 是一个开源的大型语言模型&#xff08;LLM&#xff09;本地运行框架&#xff0c;旨在简化大模型的部署和管理流程&#xff0c;使开发者、研究人员及爱好者能够高效地在本地环境中实验和…

ML.NET库学习009:花卉图像分类模型

文章目录 ML.NET库学习009&#xff1a;花卉图像分类模型进行图像分类训练的实现功能分析代码结构核心组件示例输出代码实现详细步骤说明注意事项 进行图像分类预测的实现主要目的原理概述实现的主要功能主要流程步骤使用的主要函数和方法关键技术功能详细解读&#xff08;1&…

【Rust中级教程】1.15. Trait bounds(Trait 约束)的编译与分派

喜欢的话别忘了点赞、收藏加关注哦&#xff08;加关注即可阅读全文&#xff09;&#xff0c;对接下来的教程有兴趣的可以关注专栏。谢谢喵&#xff01;(&#xff65;ω&#xff65;) 1.15.1. 静态分发(static dispatch) 编译泛型代码或者调用dyn Trait(详见【Rust自学】17.2…

Unity摄像机与灯光相关知识

一、Inspector窗口 Inspector窗口可以查看和编辑对象的属性以及设置 其中包含各种组件&#xff0c;例如用Cube对象来举例 1.Sphere(Mesh)组件&#xff1a; 用来决定对象的网格属性&#xff0c;例如球体网格为Sphere、立方体网格为Cube 2.Mesh Renderer组件&#xff1a; 用来设置…

8.python文件

文章目录 1.**文件**1.1**文件是什么**1.2**文件路径**1.3**文件操作**1.3.1**打开文件**1.3.2**关闭文件**1.3.3**写文件**1.3.4**读文件** 1.4**关于中文的处理**1.5**使用上下文管理器** 大家好&#xff0c;我是晓星航。今天为大家带来的是 python文件 相关的讲解&#xff0…

Deepseek 与 ChatGPT:AI 浪潮中的双子星较量

引言 在人工智能飞速发展的当下&#xff0c;AI 语言模型成为了人们关注的焦点。Deepseek 与 ChatGPT 作为其中的佼佼者&#xff0c;各自展现出独特的魅力&#xff0c;引领着 AI 技术的发展潮流。今天&#xff0c;就让我们深入探讨这两款模型&#xff0c;看看它们在 AI 领域中是…

使用 DeepSeek 生成流程图、甘特图与思维导图:结合 Typora 和 XMind 的高效工作流

在现代工作与学习中&#xff0c;可视化工具如流程图、甘特图和思维导图能够极大地提升信息整理与表达的效率。本文将详细介绍如何使用 DeepSeek 生成 Mermaid 文本&#xff0c;结合 Typora 快速生成流程图和甘特图&#xff0c;并通过 Markdown 格式生成思维导图&#xff0c;最终…

题海拾贝:【枚举】P2010 [NOIP 2016 普及组] 回文日期

Hello大家好&#xff01;很高兴我们又见面啦&#xff01;给生活添点passion&#xff0c;开始今天的编程之路&#xff01; 我的博客&#xff1a;<但凡. 我的专栏&#xff1a;《编程之路》、《数据结构与算法之美》、《题海拾贝》 欢迎点赞&#xff0c;关注&#xff01; 1、题…