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

vue入门(增查改!)

<template><div><!-- 搜索栏 --><el-card id="search"><el-row><el-col :span="20"><el-input v-model="searchModel.name" placeholder="根据名字查询"></el-input><el-input v-model="searchModel.email" placeholder="根据邮件查询"></el-input><el-button @click="searchList" type="primary" round icon="el-icon-search">搜索</el-button></el-col><el-col :span="4"><el-button @click="openEditUI(null)" type="primary" icon="el-icon-plus" circle></el-button></el-col></el-row></el-card><!-- 结果列表 --><el-table :data="userList" style="width: 100%"><el-table-column type="index" label="#" width="180"><template slot-scope="scope">{{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}</template></el-table-column><el-table-column prop="id" label="用户id" width="180"></el-table-column><el-table-column prop="name" label="姓名"></el-table-column><el-table-column prop="age" label="年龄"></el-table-column><el-table-column prop="email" label="电子邮件"></el-table-column><el-table-column label="操作"><template slot-scope="scope"><el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" circle></el-button><el-button type="danger" icon="el-icon-delete" circle></el-button></template></el-table-column></el-table><!--  分页功能 --><el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange":current-page="searchModel.pageNo" :page-sizes="[1, 5, 10, 15]" :page-size="searchModel.pageSize"layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination><!-- 对话框 --><!-- Form --><el-dialog :title="title" :visible.sync="dialogFormVisible"><el-form :model="userForm" :rules="rules"><el-form-item label="名字" :label-width="formLabelWidth"><el-input v-model="userForm.name" autocomplete="off"></el-input></el-form-item><el-form-item label="age" :label-width="formLabelWidth"><el-input v-model="userForm.age" autocomplete="off"></el-input></el-form-item><el-form-item label="email" :label-width="formLabelWidth"><el-input v-model="userForm.email" autocomplete="off"></el-input></el-form-item></el-form><div slot="footer" class="dialog-footer"><el-button @click="dialogFormVisible = false">取 消</el-button><el-button type="primary" @click="performAction">确 定</el-button></div></el-dialog></div>
</template><script>
import axios from 'axios';
export default {data() {return {title: '添加',userForm: {name: '',age: '',email: ''},dialogFormVisible: false,total: 0,searchModel: {pageNo: 1,pageSize: 5,name: '',email: '',},userList: [],formLabelWidth: '50px', // 设置标签的宽度rules: {name: [{ required: true, message: '请输入用户名', trigger: 'blur' },{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],},}},methods: {openEditUI(id) {if (id == null) {this.title = "新增用户";this.userForm = {name: '',age: '',email: ''};} else {this.title = "修改用户";// 根据id查询用户数据axios.get(`/api/sys/demoTable/` + id).then(response => {this.userForm = response.data.data; // 填充编辑数据this.dialogFormVisible = true;}).catch(error => {console.error('获取编辑数据失败:', error);});}this.dialogFormVisible = true;},performAction() {if (this.title === '新增用户') {const newData = {name: this.userForm.name,age: this.userForm.age,email: this.userForm.email};axios.post('/api/sys/demoTable/create', newData).then(response => {if (response.data.code == 1) {this.$message({message: '添加成功',type: 'success'});// 关闭对话框this.dialogFormVisible = false;// 可以更新列表,重新加载数据等操作// 重新加载数据this.getUserList();} else {console.error('数据添加失败');}}).catch(error => {console.error('添加数据失败:', error);});} else if (this.title === '修改用户') {axios.put(`/api/sys/demoTable/update`, this.userForm).then(response => {if (response.data.code === 1) {this.$message({message: '更新成功',type: 'success'});this.dialogFormVisible = false;this.getUserList();} else {console.error('数据更新失败');}}).catch(error => {console.error('更新数据失败:', error);});}},// 分页大小变化handleSizeChange(pageSize) {this.searchModel.pageSize = pageSize;this.getUserList();},// 当前页码变化handleCurrentChange(pageNo) {this.searchModel.pageNo = pageNo;this.getUserList();},// 获取用户列表getUserList() {axios.get('/api/sys/demoTable/list', { params: this.searchModel }).then(response => {console.log(response);const data = response.data.data;this.userList = data.rows;this.total = data.total;}).catch(error => {console.error('获取用户列表失败:', error);});},searchList() {this.searchModel.pageNo = 1; // 重置页码为1,以获取新的查询结果// 构建查询参数const queryParams = {name: this.searchModel.name,email: this.searchModel.email,pageNo: this.searchModel.pageNo,pageSize: this.searchModel.pageSize};// 发送 GET 请求axios.get('/api/sys/demoTable/list', { params: queryParams }).then(response => {const data = response.data.data;this.userList = data.rows;this.total = data.total;}).catch(error => {console.error('获取用户列表失败:', error);});}},mounted() {// 初始化加载用户列表this.getUserList();}}
</script><style>
#search .el-input {width: 300px;margin-right: 20px;
}
</style>

思路

关于 查询和分页的结合分析

1.查询也就是在页面上显示数据,所以用到了分页。

2. 用Mybatis-plus , 进行分页用Map返回数据,注意关键点pageNo和pageSize,这是前端请求头请求的数据,后端要返回一个查询到的结果集和总页数

    @GetMapping("/list")public R<Map<String, Object>> getAllList(@RequestParam(value = "name", required = false) String name,@RequestParam(value = "email", required = false) String email,@RequestParam(value = "pageNo") Long pageNo,@RequestParam(value = "pageSize") Long pageSize) {

3. 关于 添加和修改的功能结合分析

    *  添加按钮和修改按钮,共用一个对话框,用data:title,  的值区分 两个请求。

   * 添加按钮:id为null 或 undifined,  

      而修改按钮id :肯定是有值的。

   *从而分别为title赋值 ,添加 | 修改,

在起一个方法,if ( 添加==添加 ){

添加的请求

} else{

  修改的请求

}

http://www.lryc.cn/news/126819.html

相关文章:

  • 移动端身份证识别技术的应用,告别手动录入证件信息
  • 网络通信原理TCP字段解析(第四十七课)
  • uniapp微信小程序消息订阅快速上手
  • MySQL 根据多字段查询重复数据
  • Markdown编辑器 Mac版Typora功能介绍
  • el-form自定义校验规则
  • xml对象与字符串互换
  • 单例模式和多例模式和工厂模式
  • 【网络架构】华为hw交换机网络高可用网络架构拓扑图以及配置
  • 信也科技一面凉经
  • AI商业化如何落地?看设计师如何利用AI细化工作流
  • 论文阅读 - Understanding Diffusion Models: A Unified Perspective
  • [Python进阶] 定制类:模拟篇
  • HTML5 游戏开发实战 | 五子棋
  • rust学习-json的序列化和反序列化
  • 基于MapReduce的Hive数据倾斜场景以及调优方案
  • mysql 02 数据库的约束
  • Quivr 基于GPT和开源LLMs构建本地知识库 (更新篇)
  • Unity如何制作声音控制条(控制音量大小)
  • 非计算机科班如何顺利转行计算机领域?
  • Android音视频剪辑器自定义View实战!
  • stm32_ADC电源、通道、工作模式
  • Vue编程式路由导航
  • LVS-DR模式
  • 详细介绍生成对抗网络 (GAN) 的原理和基于Pytorch源码的实现
  • 高性能数据处理选型
  • 【深入理解C语言】-- 关键字2
  • Java进阶(3)——手动实现ArrayList 源码的初步理解分析 数组插入数据和删除数据的问题
  • 若依前端npm run dev启动时报错
  • 实战项目:基于主从Reactor模型实现高并发服务器