当前位置: 首页 > news >正文

浏览器自带的IndexDB的简单使用示例--小型学生管理系统

浏览器自带的IndexDB的简单使用示例--小型学生管理系统

    • 文章说明
    • 代码
    • 效果展示

文章说明

本文主要为了简单学习IndexDB数据库的使用,写了一个简单的增删改查功能

代码

App.vue(界面的源码)

<template><div style="padding: 30px"><el-button type="primary" @click="openAddDialog" style="float: left; margin-bottom: 20px; margin-right: 20px">新增</el-button><el-input placeholder="输入姓名查找" v-model="data.nameSearchInput" @change="findAllData"style="float: left; width: 400px; margin-bottom: 20px"/><el-table:data="data.dataList"borderstyle="width: 100%":header-cell-style="{ 'text-align': 'center' }":cell-style="{ 'text-align': 'center' }"><el-table-column prop="id" label="编号"/><el-table-column prop="name" label="姓名"/><el-table-column prop="age" label="年龄"/><el-table-column prop="sex" label="性别"/><el-table-column prop="tel" label="电话"/><el-table-column label="操作"><template #default="scope"><el-popconfirmtitle="确定修改该菜单吗?"@confirm="openEditDialog(scope.row)"><template #reference><el-button size="small" type="danger">修改</el-button></template></el-popconfirm><el-popconfirmtitle="确定删除该菜单吗?"@confirm="handleDelete(scope.row)"><template #reference><el-button size="small" type="danger">删除</el-button></template></el-popconfirm></template></el-table-column></el-table><el-dialog v-model="data.addDialogVisible" title="修改" width="30%"><el-inputv-model="data.form.name"placeholder="姓名"maxlength="50"style="margin-bottom: 20px"/><el-inputv-model="data.form.age"placeholder="年龄"maxlength="50"style="margin-bottom: 20px"/><el-inputv-model="data.form.sex"placeholder="性别"maxlength="50"style="margin-bottom: 20px"/><el-inputv-model="data.form.tel"placeholder="电话"maxlength="50"style="margin-bottom: 20px"/><template #footer><span class="dialog-footer"><el-button @click="data.addDialogVisible = false">Cancel</el-button><el-button type="primary" @click="insertData">Confirm</el-button></span></template></el-dialog><el-dialog v-model="data.editDialogVisible" title="修改" width="30%"><el-inputv-model="data.form.name"placeholder="姓名"maxlength="50"style="margin-bottom: 20px"/><el-inputv-model="data.form.age"placeholder="年龄"maxlength="50"style="margin-bottom: 20px"/><el-inputv-model="data.form.sex"placeholder="性别"maxlength="50"style="margin-bottom: 20px"/><el-inputv-model="data.form.tel"placeholder="电话"maxlength="50"style="margin-bottom: 20px"/><template #footer><span class="dialog-footer"><el-button @click="data.editDialogVisible = false">Cancel</el-button><el-button type="primary" @click="updateData">Confirm</el-button></span></template></el-dialog></div>
</template><script>
import {onBeforeMount, reactive} from "vue";
import {db_operation, message} from "@/db_operation";export default {name: "App",setup() {const data = reactive({dbName: "bbyh",tableName: "user",fieldList: ["id", "name", "age", "sex", "tel"],dataList: [],nameSearchInput: "",form: {id: 0,name: "",age: "",sex: "",tel: ""},addDialogVisible: false,editDialogVisible: false,});onBeforeMount(() => {db_operation.open(data.dbName, data.tableName, data.fieldList);setTimeout(() => {findAllData();}, 1000);});function findAllData() {data.dataList = [];db_operation.findAllData((event) => {const row = event.target["result"];if (row) {if (row.value["name"].indexOf(data.nameSearchInput.trim()) > -1) {data.dataList.push(row.value);}row.continue();}});}function openAddDialog() {data.form.name = "";data.form.age = "";data.form.sex = "";data.form.tel = "";data.addDialogVisible = true;}function openEditDialog(row) {data.form.id = row.id;data.form.name = row.name;data.form.age = row.age;data.form.sex = row.sex;data.form.tel = row.tel;data.editDialogVisible = true;}function handleDelete(row) {db_operation.delete(row.id);findAllData();}function insertData() {if (db_operation.add({name: data.form.name.trim(),age: data.form.age.trim(),sex: data.form.sex.trim(),tel: data.form.tel.trim()})) {data.addDialogVisible = false;message("数据添加成功", "success");findAllData();}}function updateData() {db_operation.update(data.form.id, {id: data.form.id,name: data.form.name.trim(),age: data.form.age.trim(),sex: data.form.sex.trim(),tel: data.form.tel.trim()});data.editDialogVisible = false;message("数据更新成功", "success");findAllData();}return {data,findAllData,openAddDialog,openEditDialog,handleDelete,insertData,updateData,}}
};
</script><style>
* {padding: 0;margin: 0;box-sizing: border-box;
}
</style>

IndexDB封装的工具类

import {ElMessage} from 'element-plus';export function message(msg, type) {ElMessage({message: msg,showClose: true,type: type,center: true})
}class Db_operation {request = undefined;db = undefined;dbName = undefined;tableName = undefined;fieldList = undefined;open(dbName, tableName, fieldList, version = 1) {db_operation.dbName = dbName;db_operation.tableName = tableName;db_operation.fieldList = fieldList;const request = window.indexedDB.open(dbName, version);db_operation.request = request;request.onsuccess = function (event) {db_operation.db = event.target["result"];};request.onupgradeneeded = function (event) {const db = event.target.result;db_operation.db = db;if (!db.objectStoreNames.contains(tableName)) {const objectStore = db.createObjectStore(tableName, {keyPath: "id",autoIncrement: true});for (let i = 0; i < fieldList.length; i++) {objectStore.createIndex(fieldList[i], fieldList[i]);}}};}getObjectStore() {const transaction = db_operation.db.transaction(db_operation.tableName, "readwrite");return transaction.objectStore(db_operation.tableName);}add(data) {if (db_operation.dbName === undefined) {message("数据库还未打开", "error");return false;}db_operation.getObjectStore().add(data);return true;}find(field, value, success) {if (db_operation.dbName === undefined) {message("数据库还未打开", "error");return;}const men = db_operation.getObjectStore().index(field);men.get(value).onsuccess = function (event) {success(event);};}findAllData(success) {if (db_operation.dbName === undefined) {message("数据库还未打开", "error");return;}const men = db_operation.getObjectStore().openCursor();men.onsuccess = function (event) {success(event)};}update(idValue, newData) {if (db_operation.dbName === undefined) {message("数据库还未打开", "error");return;}const objectStore = db_operation.getObjectStore();const men1 = objectStore.get(idValue);men1.onsuccess = function () {objectStore.put(newData);};}delete(idValue) {if (db_operation.dbName === undefined) {message("数据库还未打开", "error");return;}const men1 = db_operation.getObjectStore().delete(idValue);men1.onsuccess = function () {message("删除成功", "success");};}
}export const db_operation = new Db_operation();

main.js(引入ElementPlus )

import { createApp } from 'vue'
import App from './App.vue'import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'const app = createApp(App);app.use(ElementPlus);app.mount("#app");

效果展示

简单的增删改查演示

在这里插入图片描述

相关文章:

浏览器自带的IndexDB的简单使用示例--小型学生管理系统

浏览器自带的IndexDB的简单使用示例--小型学生管理系统 文章说明代码效果展示 文章说明 本文主要为了简单学习IndexDB数据库的使用&#xff0c;写了一个简单的增删改查功能 代码 App.vue&#xff08;界面的源码&#xff09; <template><div style"padding: 30px&…...

2024年计算机专业还值得选吗?

个人认为可以 一、就业前景广阔 市场需求旺盛&#xff1a;随着数字化和信息化的快速发展&#xff0c;计算机技术已经渗透到各个行业和领域。无论是传统制造业、金融、医疗&#xff0c;还是新兴的互联网、人工智能等领域&#xff0c;都离不开计算机专业人才的支持。因此&#x…...

JSON.parse(JSON.stringify())导致的响应式属性丢失

console.log("formdata赋值前", this.formdata);console.log("row",row);console.log("row序列化后", JSON.parse(JSON.stringify(row)));this.formdata JSON.parse(JSON.stringify(row)); console.log("formdata赋值后", this.formd…...

SpringBoot引入外部依赖包

将需要引入的文件放置到与src同级别的目录下 如上&#xff0c;在src的同级&#xff0c;新建了一个lib目录&#xff0c;将jar包放置其中 在POM文件下&#xff0c;加入如下配置 <dependency><groupId>com.aliyun</groupId><artifactId>com.aliyun.filed…...

Spring事务介绍、Spring集成MyBatis

目录 1.Spring的事务1.1 什么是事务&#xff1f;1.2 事务的特性&#xff08;ACID&#xff09;1.3 Spring 事务实现方式有哪些&#xff1f;1.4 Spring事务管理接口介绍1.4.1 PlatformTransactionManager:事务管理接口1.4.2 TransactionDefinition:事务属性事务管理器接口1.4.3 T…...

使用GPG来解密和加密文件详解

文章目录 使用私钥解密文件示例步骤 注意事项加密文件前提条件导入公钥加密文件输出加密文件示例步骤注意事项邮箱不是必须的情况1&#xff1a;有多个公钥情况2&#xff1a;只有一个公钥示例步骤示例1&#xff1a;指定公钥ID或邮箱地址示例2&#xff1a;密钥环中只有一个相关的…...

【Flutter】基础教程:从安装到发布

Flutter 是一种流行的开源移动应用开发框架&#xff0c;由 Google 开发&#xff0c;可用于构建高性能、跨平台的移动应用。本教程将带领你从安装 Flutter 开发环境开始&#xff0c;一步步完成第一个程序&#xff0c;并介绍如何将应用发布到各个平台上。 跨端原理的关键点包括&a…...

51单片机学习记录———定时器

文章目录 前言一、定时器介绍二、STC89C52定时器资源三、定时器框图四、定时器模式五、定时器相关寄存器六、定时器练习 前言 一个学习嵌入式的小白~ 有问题评论区或私信指出~ 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、定时器介绍 定时器介…...

C# 热插拔---插件开发

热插拔是以多态&#xff0c;文件监控&#xff0c;反射为基础的。所以用到的是FileSystemWatcher类和 Assembly 类&#xff0c;主要原理就是动态加载dll文件&#xff0c;而要监控dll文件&#xff0c;最好的就是用FileSystemWatcher类&#xff0c;它可以实时监控指定路径下的文件…...

hive优化之逻辑类似or逻辑重复

今天拿到一个二次开发的需求&#xff0c;只是增加一个业务类型&#xff0c;开发起来倒是也蛮轻松。 但是&#xff0c;对自己的要求不难这么低&#xff0c;否则可替代性也太高了。 除了完成自己的那部分开发&#xff0c;当然展现自己实力的&#xff0c;可以是优化。 1&#x…...

ES6+Vue

ES6Vue ES6语法 ​ VUE基于是ES6的&#xff0c;所以在使用Vue之前我们需要先了解一下ES6的语法。 1.什么是ECMAScript6 ECMAScript是浏览器脚本语言的规范&#xff0c;基于javascript来制定的。为什么会出现这个规范呢&#xff1f; 1.1.JS发展史 1995年&#xff0c;网景工…...

如何将重量传感器 HX711 与 Arduino 一起使用

How to use a Weight Sensor / Load Cell HX711 with an Arduino 原文 OVERVIEW We’ve all used a scale to determine the weight of something at some point in our lives. Using a Load Cell or Weight sensor you can add this capability to your Arduino projects.…...

HarmonyOS Next开发学习手册——应用启动框架AppStartup

概述 AppStartup提供了一种简单高效的初始化组件的方式&#xff0c;开发者可以使用AppStartup来显示的设置组件的初始化顺序以及之间的依赖关系&#xff0c;支持异步初始化组件加速应用的启动时间。开发者需要分别为待初始化的组件实现AppStartup提供的 StartupTask 接口&…...

如何在Springboot中添加事务执行?(以MySQL为例)

目录 1. 添加依赖 2. 配置数据库连接 3. 启用事务管理 4. 创建实体类和存储库 5. 创建服务类并使用Transactional注解 6. 编写测试用例 7. 运行应用程序 在Springboot中开启数据库的事务的应用开发过程中非常重要的业务&#xff0c;以下是一个使用MySQL数据库&#xff0…...

优化MySQL并发事务:如何避免更新丢失问题?

背景描述 现在有两个事务&#xff0c;事务A和事务B&#xff0c;他们都需要修改同一行数据&#xff0c;这行数据原始值为100&#xff0c;事务A的操作是数据增加100&#xff0c;事务B的操作也是增加100&#xff0c;预期的最终结果是300&#xff0c;现在如何保证最终的数据是300的…...

物联网设备管理系统设计

一、引言 物联网设备管理系统设计旨在通过物联网技术实现对设备的全面监控、控制和管理&#xff0c;以提高设备的运行效率、降低运维成本&#xff0c;并确保数据的安全性和完整性。本设计将结合当前物联网技术的发展趋势和实际应用需求&#xff0c;提出一个清晰、可扩展的物联网…...

python之Bible快速检索器

内容将会持续更新&#xff0c;有错误的地方欢迎指正&#xff0c;谢谢! python之Bible快速检索器 TechX 坚持将创新的科技带给世界&#xff01; 拥有更好的学习体验 —— 不断努力&#xff0c;不断进步&#xff0c;不断探索 TechX —— 心探索、心进取&#xff01; 助力快…...

微服务-网关

网关&#xff1a;就是网络的关口&#xff0c;负责请求的路由、转发、身份校验 在SpringCloud中网关的实现包括两种&#xff1a; 快速入门 引入依赖 路由属性 网关路由对应的Java类型是RouteDefinition&#xff0c;其中常见的属性有&#xff1a; id&#xff1a;路由唯一标示ur…...

OpenAI项目爆改GLM——以基于llama_index的pdf阅读助手

最近在做大模型agent构建&#xff0c;看了许多不错的开源项目&#xff0c;但是clone下来就是一整个不能用&#xff0c;因为github上开源的项目基本都是基于openai做的。而如果想要转成国内大模型backbone&#xff0c;需要修改的地方挺多的。 现在以一个简单的pdf reader agent…...

如何在Java中处理ParseException异常?

如何在Java中处理ParseException异常&#xff1f; 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01; 在Java编程中&#xff0c;ParseException异常是开发者在处理…...

【Java学习笔记】Arrays类

Arrays 类 1. 导入包&#xff1a;import java.util.Arrays 2. 常用方法一览表 方法描述Arrays.toString()返回数组的字符串形式Arrays.sort()排序&#xff08;自然排序和定制排序&#xff09;Arrays.binarySearch()通过二分搜索法进行查找&#xff08;前提&#xff1a;数组是…...

【Java_EE】Spring MVC

目录 Spring Web MVC ​编辑注解 RestController RequestMapping RequestParam RequestParam RequestBody PathVariable RequestPart 参数传递 注意事项 ​编辑参数重命名 RequestParam ​编辑​编辑传递集合 RequestParam 传递JSON数据 ​编辑RequestBody ​…...

听写流程自动化实践,轻量级教育辅助

随着智能教育工具的发展&#xff0c;越来越多的传统学习方式正在被数字化、自动化所优化。听写作为语文、英语等学科中重要的基础训练形式&#xff0c;也迎来了更高效的解决方案。 这是一款轻量但功能强大的听写辅助工具。它是基于本地词库与可选在线语音引擎构建&#xff0c;…...

用机器学习破解新能源领域的“弃风”难题

音乐发烧友深有体会&#xff0c;玩音乐的本质就是玩电网。火电声音偏暖&#xff0c;水电偏冷&#xff0c;风电偏空旷。至于太阳能发的电&#xff0c;则略显朦胧和单薄。 不知你是否有感觉&#xff0c;近两年家里的音响声音越来越冷&#xff0c;听起来越来越单薄&#xff1f; —…...

十九、【用户管理与权限 - 篇一】后端基础:用户列表与角色模型的初步构建

【用户管理与权限 - 篇一】后端基础:用户列表与角色模型的初步构建 前言准备工作第一部分:回顾 Django 内置的 `User` 模型第二部分:设计并创建 `Role` 和 `UserProfile` 模型第三部分:创建 Serializers第四部分:创建 ViewSets第五部分:注册 API 路由第六部分:后端初步测…...

从实验室到产业:IndexTTS 在六大核心场景的落地实践

一、内容创作&#xff1a;重构数字内容生产范式 在短视频创作领域&#xff0c;IndexTTS 的语音克隆技术彻底改变了配音流程。B 站 UP 主通过 5 秒参考音频即可克隆出郭老师音色&#xff0c;生成的 “各位吴彦祖们大家好” 语音相似度达 97%&#xff0c;单条视频播放量突破百万…...

ZYNQ学习记录FPGA(二)Verilog语言

一、Verilog简介 1.1 HDL&#xff08;Hardware Description language&#xff09; 在解释HDL之前&#xff0c;先来了解一下数字系统设计的流程&#xff1a;逻辑设计 -> 电路实现 -> 系统验证。 逻辑设计又称前端&#xff0c;在这个过程中就需要用到HDL&#xff0c;正文…...

python打卡day49@浙大疏锦行

知识点回顾&#xff1a; 通道注意力模块复习空间注意力模块CBAM的定义 作业&#xff1a;尝试对今天的模型检查参数数目&#xff0c;并用tensorboard查看训练过程 一、通道注意力模块复习 & CBAM实现 import torch import torch.nn as nnclass CBAM(nn.Module):def __init__…...

python读取SQLite表个并生成pdf文件

代码用于创建含50列的SQLite数据库并插入500行随机浮点数据&#xff0c;随后读取数据&#xff0c;通过ReportLab生成横向PDF表格&#xff0c;包含格式化&#xff08;两位小数&#xff09;及表头、网格线等美观样式。 # 导入所需库 import sqlite3 # 用于操作…...

【Linux】使用1Panel 面板让服务器定时自动执行任务

服务器就是一台24小时开机的主机&#xff0c;相比自己家中不定时开关机的主机更适合完成定时任务&#xff0c;例如下载资源、备份上传&#xff0c;或者登录某个网站执行一些操作&#xff0c;只需要编写 脚本&#xff0c;然后让服务器定时来执行这个脚本就可以。 有很多方法实现…...