AJAX综合案例——图书管理

ops/2025/2/7 0:34:46/

黑马程序员视频地址:

AJAX-Day02-10.案例_图书管理AJAX-Day02-10.案例_图书管理_总结_V1.0是黑马程序员前端AJAX入门到实战全套教程,包含学前端框架必会的(ajax+node.js+webpack+git),一套全覆盖的第25集视频,该合集共计140集,视频收藏或关注UP主,及时了解更多相关视频内容。https://www.bilibili.com/video/BV1MN411y7pw?vd_source=0a2d366696f87e241adc64419bf12cab&spm_id_from=333.788.player.switch&p=25https://www.bilibili.com/video/BV1MN411y7pw?vd_source=0a2d366696f87e241adc64419bf12cab&spm_id_from=333.788.player.switch&p=25

步骤一:Bootstrap弹框

1. 引入bootstrap.css 和 bootstrap.js

 <!-- 引入bootstrap.css --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet"><!-- 引入bootstrap.js --><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.min.js"></script>

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

<div class="modal" tabindex="-1"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">Modal title</h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><p>Modal body text goes here.</p></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>

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

1)弹框显示 

<!--第一步:触发弹框,按钮绑定自定义属性:data-bs-toggle="modal"和data-bs-target="CSS选择器"-->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target=".myalert">显示弹框</button><!-- 第二步:给弹框加上myalert类,以便区分同一页面不同弹窗 --><div class="modal myalert" tabindex="-1"><!--省略代码好几行--></div>

2)弹框隐藏

<!--给需要有隐藏效果的按钮加上自定义属性data-bs-dismiss="modal"-->
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>

4.通过JS控制,控制弹框显示或隐藏

1)弹窗显示

//第一步:给弹窗模块实例化,从而获取show方法与hide方法
const modalDom = document.querySelector(".name-box")
const modal = new bootstrap.Modal(modalDom)
//编辑按钮绑定事件
document.querySelector(".edit-btn").addEventListener("click", () => {//第二步:调用显示弹窗函数modal.show()
})

实例化(new)的作用:继承bootstraop.Modal原型对象里的方法,见下图观察bootstrap对象

console.dir(bootstrap)

2)弹窗隐藏

//保存按钮绑定事件document.querySelector(".save-btn").addEventListener("click", () => {//隐藏弹框modal.hide()})

步骤二:渲染列表(查)

const creator = "USER_A001" //给服务器一个标识,用来获取某个用户的专属数据 
function getData()
{axios({url: "https://hmajax.itheima.net/api/books",params: {creator}}).then(result => {const bookList = result.data.data    //将服务器返回的数据用一个数组来接收const htmlStr = bookList.map((item, index) => {  //map数组映射return `<tr>          <td>${index + 1}</td>          <td>${item.bookname}</td>          <td>${item.author}</td>          <td>${item.publisher}</td>          <td>            <span class="del">删除</span>            <span class="edit">编辑</span>          </td>        </tr>`}).join("")  //join将数组用双引号内的东西拼接成字符串document.querySelector(".list").innerHTML = htmlStr   //将内容渲染到页面上}).catch(error => {console.log(error)})
}getData()    //调用函数

步骤三:新增图书(增)

//创建弹窗对象
const addModalDom = document.querySelector(".add-modal")
const addModal = new bootstrap.Modal(addModalDom)
//保存按钮绑定事件
document.querySelector(".add-btn").addEventListener("click", () => {//获取表单数据const addDataDom = document.querySelector(".add-form") const addData = serialize(addDataDom, {hash: true, empty: true})//向服务器提交数据axios({url: "https://hmajax.itheima.net/api/books",method: "POST",data: {...addData,creator}}).then(result => {alert("数据提交成功")//清除表单数据addDataDom.reset()//渲染数据getData()//隐藏弹窗addModal.hide()}).catch(error => {alert("数据提交失败,请重试")})})

步骤四:删除图书(删)

document.querySelector(".list").addEventListener("click", e => { //事件委托if(e.target.classList.contains("del"))  //判断触发源{const theId = e.target.parentNode.dataset.idaxios({url: `https://hmajax.itheima.net/api/books/${theId}`,  //以路径方式提交数据method: "DELETE"   //告诉服务器此次事件的方法}).then(result => {alert("成功删除!")getData()    //刷新数据}).catch(error => {alert("删除失败!")})}
})

步骤五:编辑图书(改)

//创建编辑弹窗实例对象
const editModalDom = document.querySelector(".edit-modal")
const editModal = new bootstrap.Modal(editModalDom)
//编辑按钮绑定事件 事件委托
document.querySelector(".list").addEventListener("click", e => {if(e.target.classList.contains("edit")){//获取数据const theId = e.target.parentNode.dataset.idaxios({url: `https://hmajax.itheima.net/api/books/${theId}`}).then(result => {//显示数据const editData = result.data.dataconst key = Object.keys(editData)    //获取对象的所有属性名key.forEach(item => {    //遍历数组,每遍历一次,将获取的数据赋值给拥有对应属性名的元素值document.querySelector(`.edit-modal .${item}`).value = editData[item]})}).catch(error => {alert("获取数据失败!")})//显示弹框editModal.show()}
})//修改按钮绑定事件
document.querySelector(".edit-btn").addEventListener("click", () => {//获取数据const editDataDom = document.querySelector(".edit-form")const {id, bookname, author, publisher} = serialize(editDataDom, {hash: true, empty: true})    //解构//提交数据axios({url: `https://hmajax.itheima.net/api/books/${id}`,method: "PUT",    //看接口文档要求提交的方式,跟接口文档保持一致data: {    //为什么用data而不用params?接口文档要求的!bookname,author,publisher,creator}}).then(result => {alert("修改成功!")//关闭弹窗editModal.hide()//刷新数据getData()}).catch(error => {alert("提交失败!")})})

http://www.ppmy.cn/ops/156299.html

相关文章

SQL server 创建DB Link 详解

在日常工作中&#xff0c;经常涉及到跨库操作&#xff0c;为使跨数据库的操作变得更加灵活高效&#xff0c;我们可以在 SQL Server 中建立数据库链接&#xff08; DB Link&#xff09;&#xff0c;实现 SQL Server 数据库与其他数据库&#xff08;如 Oracle, MySQL 等&#xff…

【Conda 和 虚拟环境详细指南】

Conda 和 虚拟环境的详细指南 什么是 Conda&#xff1f; Conda 是一个开源的包管理和环境管理系统&#xff0c;支持多种编程语言&#xff08;如Python、R等&#xff09;&#xff0c;最初由Continuum Analytics开发。 主要功能&#xff1a; 包管理&#xff1a;安装、更新、删…

使用DeepSeek批量生成文章,对搜索引擎产生一定影响。

使用DeepSeek批量生成文章可以通过API接口或批量任务功能实现。以下是具体步骤和注意事项&#xff1a; --- ### **一、准备工作** 1. **获取API权限** - 注册DeepSeek账号并获取API密钥&#xff08;API Key&#xff09;。 - 阅读API文档&#xff0c;了解支持的模型、…

Continue 与 CodeGPT 插件 的对比分析

以下是 Continue 与 CodeGPT 插件 的对比分析&#xff0c;涵盖功能定位、适用场景和核心差异&#xff1a; 1. 功能定位 工具核心功能技术基础Continue专注于代码自动补全和上下文感知建议&#xff0c;支持多语言&#xff0c;强调低延迟和轻量级集成。基于本地模型或轻量级AI&a…

SpringBoot+Dubbo+zookeeper 急速入门案例

项目目录结构&#xff1a; 第一步&#xff1a;创建一个SpringBoot项目&#xff0c;这里选择Maven项目或者Spring Initializer都可以&#xff0c;这里创建了一个Maven项目&#xff08;SpringBoot-Dubbo&#xff09;&#xff0c;pom.xml文件如下&#xff1a; <?xml versio…

Day51:type()函数

在 Python 中&#xff0c;type() 是一个内置函数&#xff0c;用于返回对象的类型。它可以用于检查变量的类型&#xff0c;也可以用于动态创建新的类型。今天&#xff0c;我们将深入了解 type() 函数的使用方法。 1. 使用 type() 获取变量的类型 最常见的使用方式是将一个对象…

cmd执行mysql命令

安装mysql之后如果想使用cmd执行mysql命令&#xff0c;需要怎么操作呢&#xff0c;下面一起看一下。 安装mysql之后&#xff0c;如果直接去cmd窗口执行MySQL命令&#xff0c;窗口可能会提示mysql不是可执行命令。 需要配置系统的环境变量&#xff0c;将mysql的安装路径配置系…

Spring Boot 配置文件详解:YAML vs Properties

前言 在 Spring Boot 开发中&#xff0c;配置文件是应用运行的核心。无论是开发、测试还是生产环境&#xff0c;配置文件都起到了至关重要的作用。Spring Boot 提供了两种主流的配置文件格式&#xff1a;Properties 和 YAML。它们各有特点&#xff0c;适用于不同的场景。 本文…