基于element-plus和IndexedDB数据库的基础表单

本文介绍了基于Vue 3和Element Plus的表单项目配置页面实现。页面包含搜索栏、操作按钮、数据表格和分页组件,使用IndexedDB进行本地数据存储。主要功能包括:1) 通过模糊查询搜索项目;2) 分页显示项目数据;3) 添加/编辑/删除项目操作。核心代码展示了如何通过IndexedDB API实现数据查询、分页和模糊搜索,并采用响应式设计计算表格高度。技术栈包含Vue 3组合式API、Element Plus组件库和IndexedDB数据库操作。
在这里插入图片描述
依赖安装:

npm i idb
npm install element-plus --save
<template><div class="config-page"><!-- 面包屑 --><div class="breadcrumb-header">表单项目配置</div><!-- 搜索栏 --><div ref="searchRef" class="search-container"><el-form :model="searchForm" label-width="auto" class="search-form"><el-form-item label="项目名称"><el-input v-model="searchForm.projectName" placeholder="请输入项目名称" /></el-form-item><el-form-item class="search-actions"><el-button type="primary" @click="handleSearch">查 询</el-button><el-button @click="handleReset">重 置</el-button></el-form-item></el-form></div><!-- 操作栏 --><div class="operation-btns"><el-button @click="handleAdd">添 加</el-button></div><!-- table表格 --><div class="main" :style="{ height: tableHeight }"><el-table :data="tableData" stripe v-loading="loading" style="width: 100%;height: 100%"><el-table-column prop="projectName" label="项目名称" /><el-table-column label="操作" width="120" fixed="right"><template #default="scope"><el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button><el-button link type="danger" @click="handleDelete(scope.row)">删除</el-button></template></el-table-column></el-table></div><!-- 分页 --><div class="footer"><el-pagination v-model:current-page="searchForm.currentPage" v-model:page-size="searchForm.pageSize":page-sizes="[10, 20, 50, 100, 200]" :total="total" layout="total, sizes, prev, pager, next, jumper"@current-change="handleCurrentChange" @size-change="handleSizeChange" /></div><el-dialog v-model="dialogVisible" :title="dialogTitle" width="500"><el-form ref="ruleFormRef" :model="form" label-width="auto" :rules="rules"><el-form-item label="项目名称" prop="projectName"><el-input v-model="form.projectName" autocomplete="off" /></el-form-item></el-form><template #footer><div class="dialog-footer"><el-button @click="dialogVisible = false">取 消</el-button><el-button type="primary" @click="handleConfirm">确 定</el-button></div></template></el-dialog></div>
</template><script setup>
import { ref, useTemplateRef, onMounted, onBeforeUnmount } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { debounce } from 'lodash-es'
import { openDB } from 'idb'// 表格容器高度
const tableHeight = ref('auto')// 计算表格高度
const calculateTableHeight = () => {if (!searchRef.value) return// 面包屑高度const headerHeight = 41// 搜索栏高度const searchHeight = searchRef.value.offsetHeight// 操作按钮栏高度const operationHeight = 56// 分页高度const footerHeight = 56tableHeight.value = `calc(100% - ${headerHeight + searchHeight + operationHeight + footerHeight}px)`
}// 防抖的resize处理
const debouncedResize = debounce(() => {calculateTableHeight()
}, 200)// 查询条件
const searchForm = ref({projectName: '',currentPage: 1,pageSize: 10
})const loading = ref(false)
const tableData = ref([])
const total = ref(0)// 获取表格数据方法
const queryProjects = async (conditions = {}, pagination = {}) => {try {if (!db) await initDB()const { currentPage = 1, pageSize = 10 } = paginationconst { projectName = '' } = conditions// 开启一个只读事务const tx = db.transaction(STORE_NAME, 'readonly')// 并获取指定对象存储的引用const store = tx.objectStore(STORE_NAME)// 获取总数和分页查询数据const skip = (currentPage - 1) * pageSizelet data = []let totalCount = 0if (projectName) {/*** 在IndexedDB数据库中打开一个游标,用于遍历或操作存储对象中的数据。* 支持查询、更新或删除数据等操作*/let cursor = await store.openCursor()let matchedCount = 0while (cursor && data.length < pageSize) {const record = cursor.value// 模糊匹配项目名(不区分大小写)if (record.projectName && record.projectName.toLowerCase().includes(projectName.toLowerCase())) {if (totalCount >= skip) {data.push(record)matchedCount++}totalCount++ // 统计总匹配数}cursor = await cursor.continue()}// 继续遍历剩余记录以计算准确的总数while (cursor) {const record = cursor.valueif (record.projectName && record.projectName.toLowerCase().includes(projectName.toLowerCase())) {totalCount++}cursor = await cursor.continue()}} else {// 无项目名条件时,获取总数并进行分页查询totalCount = await store.count()/*** 在IndexedDB数据库中打开一个游标,用于遍历或操作存储对象中的数据。* 支持查询、更新或删除数据等操作*/let cursor = await store.openCursor()let count = 0while (cursor && data.length < pageSize) {if (count >= skip) {data.push(cursor.value)}count++cursor = await cursor.continue()}}return { data, totalCount }} catch (error) {console.error('查询失败:', error)throw error}
}// 获取表格数据
const fetchTableData = async () => {try {loading.value = trueconst { data, totalCount } = await queryProjects({ projectName: searchForm.value.projectName },{currentPage: Math.max(1, searchForm.value.currentPage),pageSize: searchForm.value.pageSize})tableData.value = datatotal.value = totalCount} catch (error) {console.error('获取数据失败:', error)ElMessage.error('获取数据失败')} finally {loading.value = false}
}const searchRef = useTemplateRef('searchRef')
// 搜索
const handleSearch = () => {searchForm.value.currentPage = 1fetchTableData()
}// 重置
const handleReset = () => {searchForm.value = {projectName: '',currentPage: 1,pageSize: 10}fetchTableData()
}// current-page 改变时触发
const handleCurrentChange = (val) => {searchForm.value.currentPage = valfetchTableData()
}// 分页大小变化
const handleSizeChange = (size) => {searchForm.value.pageSize = sizefetchTableData()
}const dialogVisible = ref(false)
const dialogTitle = ref('添加')const ruleFormRef = useTemplateRef('ruleFormRef')
const form = ref({projectName: '',
})
const rules = {projectName: [{ required: true, message: '请输入项目名称', trigger: 'blur' }]
}const isAdd = ref(true)// 添加
const handleAdd = () => {form.value = {projectName: '',}isAdd.value = truedialogTitle.value = '添加'dialogVisible.value = true
}// 编辑
const handleEdit = (val) => {form.value = { ...val }isAdd.value = falsedialogTitle.value = '编辑'dialogVisible.value = true
}// 确定按钮点击事件
const handleConfirm = () => {ruleFormRef.value.validate(async (valid, fields) => {if (valid) {try {if (isAdd.value) {// 添加数据await db.add(STORE_NAME, {...form.value})ElMessage.success('添加成功')} else {// 编辑数据await db.put(STORE_NAME, { ...form.value })ElMessage.success('编辑成功')}dialogVisible.value = false// 重新获取数据fetchTableData()} catch (error) {console.error('添加数据失败:', error)if (error.name === 'ConstraintError') {ElMessage.error('该项目已存在')} else {ElMessage.error('添加失败')}}} else {console.log('error submit!', fields)}})
}// 删除
const handleDelete = ({ id }) => {console.log(id)ElMessageBox.confirm('是否确定删除?',{confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',}).then(async () => {try {await db.delete(STORE_NAME, id)// 重新获取数据fetchTableData()ElMessage({type: 'success',message: '删除成功',})} catch (error) {ElMessage.error('删除失败')console.error('删除失败:', error)}}).catch(() => {ElMessage({type: 'info',message: '已取消',})})
}// 数据库名称
const DB_NAME = 'LowCodeAppDatabase'
// 对象存储(数据库表名称)
const STORE_NAME = 'projects'
let db = null
// 初始化数据库
const initDB = async () => {if (db) return dbtry {/*** DB_NAME:数据库名称* 1:数据库版本号* 第三个参数是升级回调函数,在数据库首次创建或版本升级时执行*/db = await openDB(DB_NAME, 1, {upgrade(db) {// 创建一个名为STORE_NAME的对象存储, 设置id为主键,且自动递增const store = db.createObjectStore(STORE_NAME, { keyPath: 'id', autoIncrement: true })// 在projectName字段上创建唯一索引, 以便查询store.createIndex('projectName', 'projectName', { unique: true })}})console.log('数据库初始化成功')return db} catch (err) {ElMessage.error('数据库初始化失败,请刷新页面重试')console.error('数据库初始化失败:', err)}
}onMounted(async () => {try {// 初始化数据库await initDB()// 初始化数据await fetchTableData()// 计算表格高度calculateTableHeight()window.addEventListener('resize', debouncedResize)} catch (error) {console.error('初始化失败:', error)}
})onBeforeUnmount(() => {window.removeEventListener('resize', debouncedResize)
})</script><style scoped>
.config-page {width: 100%;height: 100%;display: flex;flex-direction: column;overflow: hidden;
}.breadcrumb-header {width: 100%;height: 41px;display: flex;align-items: center;border-bottom: 1px solid #e0e0e0;padding: 0 10px;color: #606266;
}.search-container {width: 100%;height: auto;border-bottom: 1px solid #e0e0e0;display: flex;flex-wrap: wrap;align-items: center;
}.main {padding: 0 10px 10px 10px;
}.operation-btns {width: 100%;height: 56px;display: flex;align-items: center;padding: 0 10px;
}.footer {width: 100%;height: 56px;display: flex;justify-content: flex-end;align-items: center;padding: 0 10px;border-top: 1px solid #e0e0e0;
}.search-form {width: 100%;display: flex;flex-wrap: wrap;padding-top: 10px;
}.el-form-item {margin: 0 10px 10px 10px;
}.search-actions {margin-left: auto;
}:deep(.el-table--fit .el-table__inner-wrapper:before) {display: none;
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/93207.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/93207.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

paimon实时数据湖教程-主键表更新机制

在上一章&#xff0c;我们学习了 Paimon 如何保证每一次写入的原子性和一致性。但数据仓库的核心需求不仅是写入&#xff0c;更重要的是更新。想象一个场景&#xff1a;我们需要实时更新用户的最新信息&#xff0c;或者实时累加计算用户的消费总额。传统的 Hive 数据湖对此无能…

第十六届蓝桥杯青少组C++省赛[2025.8.9]第二部分编程题(4、矩阵圈层交错旋转)

参考程序&#xff1a;#include <bits/stdc.h> using namespace std;const int MAXN 105; int a[MAXN][MAXN];int main() {int n;if (!(cin >> n)) return 0;for (int i 0; i < n; i)for (int j 0; j < n; j)cin >> a[i][j];int layers n / 2; // 每…

【FastGTP✨】[01] 使用 FastGPT 搭建简易 AI 应用

简易应用&#xff1a;英语单词解释 例句 1. 前言 FastGPT 是一个低代码 AI 应用构建平台&#xff0c;可以通过简单配置快速创建自己的 AI 应用。 本文将带你用 FastGPT 搭建一个 英语单词解释 例句 的 AI 工具&#xff0c;输入英文单词后&#xff0c;输出&#xff1a; 单词…

【Mysql语句练习】

MysqlMysql语句练习一、建库建表二、插入数据三、查询Mysql语句练习 一、建库建表 1、创建数据库mydb11_stu&#xff0c;并使用数据库 # 创建数据库mydb11_stu mysql> create database mydb11_stu; Query OK, 1 row affected (0.00 sec) # 使用数据库 mysql> use mydb1…

用Python Scrapy征服网络爬虫(反爬技术深入剖析)

目录 第1章:Scrapy是个啥?为什么它是你爬虫路上的最佳拍档? 1.1 Scrapy的核心亮点 1.2 啥时候用Scrapy? 1.3 安装Scrapy 第2章:动手写你的第一个Scrapy爬虫 2.1 创建Scrapy项目 2.2 定义数据结构(Items) 2.3 编写爬虫逻辑 2.4 运行爬虫 2.5 小技巧:调试爬虫 …

解决Electron透明窗口点击不影响其他应用

遇到的问题&#xff1a;在electron透明窗口点击&#xff0c;影响窗口下的应用接受不到点击事件解决方案&#xff1a;CSSIgnoreMouseEvents实现原理&#xff1a;主进程默认设置禁用目标窗口鼠标事件&#xff08;禁用之后能检测到mousemove&#xff09;&#xff0c;UI进程检测页面…

C# 泛型(Generics)详解

泛型是 C# 2.0 引入的核心特性&#xff0c;它允许在定义类、接口、方法、委托等时使用未指定的类型参数&#xff0c;在使用时再指定具体类型。这种机制可以显著提高代码的复用性、类型安全性和性能。一、泛型的核心概念类型参数化泛型允许将类型作为 "参数" 传递给类…

Spring中存在两个相同的Bean是否会报错?

第一种情况&#xff1a;使用XML的方式设置Bean&#xff0c;这种情况在Spring启动时就会报错&#xff0c;因为ID在Spring中是Bean的唯一标识&#xff0c;Spring容器在启动时会校验唯一性&#xff0c;一旦发现重复就会报错。但是如果是在两个不同的XML文件中定义两个相同的Bean&a…

【新手入门】Android基础知识(一):系统架构

目 录 Android 系统架构图 1. 应用 2. JAVA API 框架 3. 原生 C/C 库 4. Android 运行时&#xff08;Android Runtime&#xff09; 5. 硬件抽象层 (HAL) 6. Linux 内核 参考资料 Android 系统架构图 Android底层内核空间以Linux Kernel作为基石&#xff0c;上层用户空…

晶振电路的负载电容、电阻参数设计

系列文章目录 文章目录系列文章目录前言一、晶振主要参数二、有源与无源区别三、无源晶振四、有源晶振总结前言 在硬件电路的设计中&#xff0c;晶振电路是必不可少的&#xff0c;它充当了整个电路心脏的作用。在这个晶振电路的设计中负载电容、电阻参数的选型是很重要的&…

电脑上练打字用什么软件最好:10款打字软件评测

现在孩子们在电脑上练打字&#xff0c;软件一搜一大把&#xff0c;可好多家长和老师都犯愁&#xff1a;到底哪个管用&#xff1f;我带200多个小学生练过字&#xff0c;前前后后试了十款软件&#xff0c;今天就掏心窝子说说——有的看着花哨其实没用&#xff0c;有的专业是专业但…

第五天~提取Arxml的模板信息

🌟 ARXML模板信息提取:解锁汽车软件的乐高魔法 在汽车电子的世界里,AUTOSAR(汽车开放系统架构)如同无形的神经系统,而ARXML文件正是承载这套神经系统蓝图的数字载体。当工程师们需要批量创建或修改ECU(电子控制单元)配置时,模板信息提取便成为了一项至关重要的核心技…

react+antd+vite自动引入组件、图标等

前言&#xff1a;react在使用antd的时候&#xff0c;也是需要每个组件都在界面上按需引入的&#xff0c;那能不能自动生成&#xff0c;按需使用呢&#xff1f;我们这里说一说这个。安装插件&#xff0c;组件按需引入unplugin-antd-resolverunplugin-auto-importnpm install unp…

深度学习与遥感入门(六)|轻量化 MobileNetV2 高光谱分类

系列回顾&#xff1a; &#xff08;一&#xff09;CNN 基础&#xff1a;高光谱图像分类可视化全流程 &#xff08;二&#xff09;HybridNet&#xff08;CNNTransformer&#xff09;&#xff1a;提升全局感受野 &#xff08;三&#xff09;GCN 入门实战&#xff1a;基于光谱 KNN…

第4节 神经网络从公式简化到卷积神经网络(CNN)的进化之路

🧙 深度学习的"玄学进化史" 从CNN用卷积层池化层处理图片,循环网络RNN如何利用上下文处理序列数据,到注意力机制让Transformer横空出世,现在的大语言模型已经能写能画能决策!每个新技巧都让人惊呼"还能这么玩",难怪说深度学习像玄学——但这玄学,…

最新去水印小程序系统 前端+后端全套源码 多套模版 免授权(源码下载)

最新去水印小程序系统 前端后端全套源码 多套模版 免授权 源码下载&#xff1a;https://download.csdn.net/download/m0_66047725/91669468 更多资源下载&#xff1a;关注我

TCP Socket 编程实战:实现简易英译汉服务

前言&#xff1a;TCP&#xff08;传输控制协议&#xff09;是一种面向连接、可靠的流式传输协议&#xff0c;与 UDP 的无连接特性不同&#xff0c;它通过三次握手建立连接、四次挥手断开连接&#xff0c;提供数据确认、重传机制&#xff0c;保证数据有序且完整传输。本文将基于…

CF566C Logistical Questions Solution

Description 给定一棵 nnn 个点的树 TTT&#xff0c;点有点权 aia_iai​&#xff0c;边有边权 www. 定义 dist⁡(u,v)\operatorname{dist}(u,v)dist(u,v) 为 u→vu\to vu→v 的简单路径上的边权和. 找到一个节点 uuu&#xff0c;使得 W∑i1ndist⁡(u,i)32aiW\sum\limits_{i1}^n…

聊天室全栈开发-保姆级教程(Node.js+Websocket+Redis+HTML+CSS)

前言 最近在学习websocket全双工通信&#xff0c;想要做一个联机小游戏&#xff0c;做游戏之前先做一个聊天室练练手。 跟着本篇博客&#xff0c;可以从0搭建一个属于你自己的聊天室。 准备阶段 什么人适合学习本篇文章&#xff1f; 答&#xff1a;前端开发者&#xff0c;有一…

后台管理系统-2-vue3之路由配置和Main组件的初步搭建布局

文章目录1 路由搭建1.1 路由创建(router/index.js)1.2 路由组件(views/Main.vue)1.3 路由引入并注册(main.js)1.4 路由渲染(App.vue)2 element-plus的应用2.1 完整引入并注册(main.js)2.2 示例应用(App.vue)3 ElementPlusIconsVue的应用3.1 图标引入并注册(main.js)3.2 示例应用…