10.Vue简单项目之crud+表单验证

2023-11-07

1.准备工作,配置action.js

启动前端项目,启动后端项目。
在action.js中添加下列接口

	'BOOK_ADD': '/book/addBook', //书籍新增
	'BOOK_EDIT': '/book/editBook', //书籍编辑
	'BOOK_DEL': '/book/delBook', //书籍删除

2.新增书籍前台页面编写

打开BookList.vue

<template>
  <div style="padding: 20px;">
    <!-- 搜索栏 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="书籍名称">
        <el-input v-model="bookname" placeholder="书籍名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="success" @click="open">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="编号" width="180">
      </el-table-column>
      <el-table-column prop="bookname" label="书籍名称" width="180">
      </el-table-column>
      <el-table-column prop="price" label="价格">
      </el-table-column>
      <el-table-column prop="booktype" label="书籍类别">
      </el-table-column>
    </el-table>
    <!--3. 分页条 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
      :page-sizes="[10, 20, 30, 40]" :page-size="rows" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 4.新增/编辑窗体 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clear()">
      <el-form :model="book">

        <el-form-item label="编号" :label-width="formLabelWidth">
          <el-input v-model="book.id" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="book.bookname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="book.price" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="类别" :label-width="formLabelWidth">
          <el-select v-model="book.booktype" placeholder="请选择书籍类别">
            <el-option :key="'key_'+by.id" v-for="by in booktypes" :label="by.name" :value="by.name"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clear()">取 消</el-button>
        <el-button type="primary" @click="doSub()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        bookname: '',
        tableData: [],
        page: 1,
        rows: 10,
        total: 0,
        //0727
        title: '新增窗体',
        dialogFormVisible: false,
        booktypes: [{
          id: 1,
          name: '言情'
        }, {
          id: 2,
          name: '玄幻'
        }, {
          id: 3,
          name: '历史'
        }, {
          id: 4,
          name: '都市'
        }],
        formLabelWidth: '100px',
        book: {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        }
      }
    },
    methods: {
      doSub() {
        //新增或者编辑提交到后台的方法
        let url = this.axios.urls.BOOK_ADD;
        if (this.title == '编辑窗体') {
          url = this.axios.urls.BOOK_EDIT;
        }
        let params = {
          id: this.book.id,
          bookname: this.book.bookname,
          price: this.book.price,
          booktype: this.book.booktype

        }
        this.axios.post(url, params).then(resp => {
          console.log(resp);
          if (resp.data.success) {
            this.$message({
              message: resp.data.msg,
              type: 'success'
            });
            this.clear();
            this.query({});
          }else{
            this.$message.error(resp.data.msg);
          }
        }).catch(err => {

        })
      },
      clear() {
        //清空表单方法
        this.title = '新增窗体'
        this.dialogFormVisible = false;
        this.book = {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        };

      },
      open() {
        //打开添加/编辑书籍信息的窗体
        this.dialogFormVisible = true;
      },
      query(params) {
        //通用查询方法
        let url = this.axios.urls.BOOK_LIST;
        this.axios.get(url, {
          params: params
        }).then(resp => {
          console.log(resp);
          this.tableData = resp.data.rows;
          this.total = resp.data.total;
        }).catch(err => {

        })
      },
      onSubmit() {
        //搜索方法
        let params = {
          bookname: this.bookname
        }
        this.query(params);

      },
      handleSizeChange(r) {
        console.log("当前展示的记录数为" + r + "条")
        let params = {
          bookname: this.bookname,
          rows: r,
          page: this.page
        }
        this.query(params)

      },
      handleCurrentChange(p) {
        console.log("当前展示的页码是第" + p + "页")
        let params = {
          bookname: this.bookname,
          page: p,
          rows: this.rows
        }
        this.query(params)
      }

    }
  }
</script>

<style>
</style>

在这里插入图片描述

3.修改和删除

<template>
  <div style="padding: 20px;">
    <!-- 搜索栏 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="书籍名称">
        <el-input v-model="bookname" placeholder="书籍名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="success" @click="open(null)">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="编号" width="180">
      </el-table-column>
      <el-table-column prop="bookname" label="书籍名称" width="180">
      </el-table-column>
      <el-table-column prop="price" label="价格">
      </el-table-column>
      <el-table-column prop="booktype" label="书籍类别">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- {{scope.row}} -->
          <el-button size="mini" @click="open(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--3. 分页条 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
      :page-sizes="[10, 20, 30, 40]" :page-size="rows" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 4.新增/编辑窗体 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clear()">
      <el-form :model="book">

        <el-form-item label="编号" :label-width="formLabelWidth">
          <el-input v-model="book.id" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="book.bookname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="book.price" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="类别" :label-width="formLabelWidth">
          <el-select v-model="book.booktype" placeholder="请选择书籍类别">
            <el-option :key="'key_'+by.id" v-for="by in booktypes" :label="by.name" :value="by.name"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clear()">取 消</el-button>
        <el-button type="primary" @click="doSub()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        bookname: '',
        tableData: [],
        page: 1,
        rows: 10,
        total: 0,
        //0727
        title: '新增窗体',
        dialogFormVisible: false,
        booktypes: [{
          id: 1,
          name: '言情'
        }, {
          id: 2,
          name: '玄幻'
        }, {
          id: 3,
          name: '历史'
        }, {
          id: 4,
          name: '都市'
        }],
        formLabelWidth: '100px',
        book: {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        }
      }
    },
    methods: {
      del(idx, row) {
        this.$confirm('确定要删除id为' + row.id + '的书籍信息吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let url = this.axios.urls.BOOK_DEL;
          this.axios.get(url,{
            params:{
              id: row.id
            }
          }).then(resp => {
            if (resp.data.success) {
              this.$message({
                message: resp.data.msg,
                type: 'success'
              });
              this.query({});
            }else{
              this.$message({
                message:resp.data.msg,
                type:'error'
              });
            }
          }).catch(err=>{

          });
        })

      },
      doSub() {
        //新增或者编辑提交到后台的方法
        let url = this.axios.urls.BOOK_ADD;
        if (this.title == '编辑窗体') {
          url = this.axios.urls.BOOK_EDIT;
        }
        let params = {
          id: this.book.id,
          bookname: this.book.bookname,
          price: this.book.price,
          booktype: this.book.booktype

        }
        this.axios.post(url, params).then(resp => {
          console.log(resp);
          if (resp.data.success) {
            this.$message({
              message: resp.data.msg,
              type: 'success'
            });
            this.clear();
            this.query({});
          } else {
            this.$message.error(resp.data.msg);
          }
        }).catch(err => {

        })
      },
      clear() {
        //清空表单方法
        this.title = '新增窗体'
        this.dialogFormVisible = false;
        this.book = {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        };

      },
      open(idx, row) {
        //打开添加/编辑书籍信息的窗体
        this.dialogFormVisible = true;
        if (row) {
          this.title = '编辑窗体',
            this.book.id = row.id;
          this.book.bookname = row.bookname;
          this.book.price = row.price;
          this.book.booktype = row.booktype;
        }
      },
      query(params) {
        //通用查询方法
        let url = this.axios.urls.BOOK_LIST;
        this.axios.get(url, {
          params: params
        }).then(resp => {
          console.log(resp);
          this.tableData = resp.data.rows;
          this.total = resp.data.total;
        }).catch(err => {

        })
      },
      onSubmit() {
        //搜索方法
        let params = {
          bookname: this.bookname
        }
        this.query(params);

      },
      handleSizeChange(r) {
        console.log("当前展示的记录数为" + r + "条")
        let params = {
          bookname: this.bookname,
          rows: r,
          page: this.page
        }
        this.query(params)

      },
      handleCurrentChange(p) {
        console.log("当前展示的页码是第" + p + "页")
        let params = {
          bookname: this.bookname,
          page: p,
          rows: this.rows
        }
        this.query(params)
      }

    }
  }
</script>

<style>
</style>

在这里插入图片描述

4.表单验证

<template>
  <div style="padding: 20px;">
    <!-- 搜索栏 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="书籍名称">
        <el-input v-model="bookname" placeholder="书籍名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="success" @click="open(null)">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="编号" width="180">
      </el-table-column>
      <el-table-column prop="bookname" label="书籍名称" width="180">
      </el-table-column>
      <el-table-column prop="price" label="价格">
      </el-table-column>
      <el-table-column prop="booktype" label="书籍类别">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- {{scope.row}} -->
          <el-button size="mini" @click="open(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="del(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--3. 分页条 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
      :page-sizes="[10, 20, 30, 40]" :page-size="rows" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 4.新增/编辑窗体 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clear()">
      <el-form :model="book" :rules="rules" ref="book">

        <el-form-item label="编号" :label-width="formLabelWidth">
          <el-input v-model="book.id" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="名称" :label-width="formLabelWidth" prop="bookname">
          <el-input v-model="book.bookname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="价格" :label-width="formLabelWidth" prop="price">
          <el-input v-model="book.price" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="类别" :label-width="formLabelWidth" prop="booktype">
          <el-select v-model="book.booktype" placeholder="请选择书籍类别">
            <el-option :key="'key_'+by.id" v-for="by in booktypes" :label="by.name" :value="by.name"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clear()">取 消</el-button>
        <el-button type="primary" @click="doSub()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        bookname: '',
        tableData: [],
        page: 1,
        rows: 10,
        total: 0,
        //0727
        title: '新增窗体',
        dialogFormVisible: false,
        booktypes: [{
          id: 1,
          name: '言情'
        }, {
          id: 2,
          name: '玄幻'
        }, {
          id: 3,
          name: '历史'
        }, {
          id: 4,
          name: '都市'
        }],
        formLabelWidth: '100px',
        book: {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        },
        rules: {
          bookname: [{
            required: true,
            message: '请输入书籍名称',
            trigger: 'blur'
          }],
          price: [{
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          }],
          booktype: [{
            required: true,
            message: '请选择类别',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      del(idx, row) {
        this.$confirm('确定要删除id为' + row.id + '的书籍信息吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let url = this.axios.urls.BOOK_DEL;
          this.axios.get(url, {
            params: {
              id: row.id
            }
          }).then(resp => {
            if (resp.data.success) {
              this.$message({
                message: resp.data.msg,
                type: 'success'
              });
              this.query({});
            } else {
              this.$message({
                message: resp.data.msg,
                type: 'error'
              });
            }
          }).catch(err => {

          });
        })

      },
      doSub() {
        //新增或者编辑提交到后台的方法
        let url = this.axios.urls.BOOK_ADD;
        if (this.title == '编辑窗体') {
          url = this.axios.urls.BOOK_EDIT;
        }
        let params = {
          id: this.book.id,
          bookname: this.book.bookname,
          price: this.book.price,
          booktype: this.book.booktype

        }

        this.$refs['book'].validate((valid) => {
          if (valid) {//校验
            //验证通过,发送请求
            this.axios.post(url, params).then(resp => {
              console.log(resp);
              if (resp.data.success) {
                this.$message({
                  message: resp.data.msg,
                  type: 'success'
                });
                this.clear();
                this.query({});
              } else {
                this.$message.error(resp.data.msg);
              }
            }).catch(err => {})

          } else {

            console.log('error submit!!');
            return false;

          }
        });


      },
      clear() {
        //清空表单方法
        this.title = '新增窗体'
        this.dialogFormVisible = false;
        this.book = {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        };

      },
      open(idx, row) {
        //打开添加/编辑书籍信息的窗体
        this.dialogFormVisible = true;
        if (row) {
          this.title = '编辑窗体',
            this.book.id = row.id;
          this.book.bookname = row.bookname;
          this.book.price = row.price;
          this.book.booktype = row.booktype;
        }
      },
      query(params) {
        //通用查询方法
        let url = this.axios.urls.BOOK_LIST;
        this.axios.get(url, {
          params: params
        }).then(resp => {
          console.log(resp);
          this.tableData = resp.data.rows;
          this.total = resp.data.total;
        }).catch(err => {

        })
      },
      onSubmit() {
        //搜索方法
        let params = {
          bookname: this.bookname
        }
        this.query(params);

      },
      handleSizeChange(r) {
        console.log("当前展示的记录数为" + r + "条")
        let params = {
          bookname: this.bookname,
          rows: r,
          page: this.page
        }
        this.query(params)

      },
      handleCurrentChange(p) {
        console.log("当前展示的页码是第" + p + "页")
        let params = {
          bookname: this.bookname,
          page: p,
          rows: this.rows
        }
        this.query(params)
      }

    }
  }
</script>

<style>
</style>

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

10.Vue简单项目之crud+表单验证 的相关文章

随机推荐

  • 区块链中的密码学应用

    区块链中应用到的密码学主要包括以下几方面 数字摘要 区块链本质上是一种分布式数据存储方式 每一个数据区块之间靠数字摘要建立起联系 比如比特币中每一个区块都包含了它前一个区块的摘要值 因此数字摘要是区块链中应用最广泛的密码学技术 也是区块链的
  • SQL删除语句

    1 delete 可指定删除一行或多行 不改变表结构 语句 delete from table where 2 drop 完全删除 包括表结构 语句 drop table 表名称 drop database 数据库名 drop index
  • matlab给定状态方程用欧拉法_基于SPH法的水滴入钢板仿真

    光滑粒子流体动力学 SPH Smoothedparticle hydrodynamics 方法是近30年发展起来的一种新的纯拉格朗日无网格粒子法 它最初是用于解决三维开放空间的天体物理学问题 现已被广泛地研究和扩展 并被应用于具有材料强度的
  • 注解@Qualifier的使用

    近期在整理一些spring的知识点 遇到关于 Qualifier这个注解的使用 记录一下 先说这个注解的作用 和autowired对比 Autowired默认是根据类型进行注入的 因此如果有多个类型一样的Bean候选者 则需要限定其中一个候
  • LeetCode第283题解法与思考

    第283题的要求如下 给定一个数组 nums 编写一个函数将所有 0 移动到数组的末尾 同时保持非零元素的相对顺序 示例 输入 0 1 0 3 12 输出 1 3 12 0 0 说明 必须在原数组上操作 不能拷贝额外的数组 尽量减少操作次数
  • python——时间绑定

    一个Tkinter主要跑在mainloop进程里 Events可能来自多个地方 比如按键 鼠标 或是系统事件 Tkinter提供了丰富的方法来处理这些事件 对于每一个控件Widget 你都可以为其绑定方法function widget bi
  • stm32——定时器之中断

    TIM 定时器 是STM32中比较重要的外设 定时器分为三种 分为为高级定时器 通用定时器 和基本定时器 本节就以通用定时器讲解 定时器中断 就是指每隔一段确定的时间 cpu就自动进入中断 执行响应的事件 那它是怎样实现这个功能的呢 首先它
  • 数据结构(C语言版 第2版)课后习题答案 严蔚敏 编著

    转自 https blog csdn net Bamboo shui article details 72433523 原文没第八章答案 数据结构 C语言版 第2版 课后习题答案 严蔚敏 等 编著 仅供参考 还是自己认真做了再看 第1章 绪
  • 油猴(Tampermonkey)安装教程

    油猴简介 Tampermonkey 油猴 是一款免费的浏览器扩展和最为流行的用户脚本管理器 它适用于 Chrome Microsoft Edge Safari Opera Next 和 Firefox 虽然有些受支持的浏览器拥有原生的用户脚
  • Informer讲解PPT介绍【超详细】--AAAI 2021最佳论文:比Transformer更有效的长时间序列预测

    文章目录 Abstract 一 informer重温讲解PPT简洁 超详细 1 1 title 1 2 Background 1 3 LSTF 问题的提出 1 4 Transformer in LSTF problem 1 5 问题阐述 1
  • 常见的内存数据库有哪些

    Redis 键值存储数据库 常用于缓存 消息代理和应用程序数据处理 Memcached 分布式内存对象缓存系统 用于缓存Web应用程序数据 VoltDB 高速内存数据库 用于实时数据处理和实时决策 Aerospike 高性能的键值存储和文档
  • 基于Ribbon自定义负载均衡策略

    微服务间通过FeignClient互相调用默认使用的是ribbon的轮询负载均衡策略 而实际场景中我们可能需要自定义一些规则或者约束来实现特定的负载均衡策略 背景 微服务开发 多租户 API接口隔离 这些在现在开发过程中会经常遇到的问题 服
  • 第2章 R语言编程基础(超详细)

    目录 2 1 数据管理 2 1 1 变量重命名 2 1 2 缺失 异常与重复值的清洗 2 1 3 数据转换 排序 抽样与概率统计 2 1 4 案例分析 工程管理 2 1 5 字符串处理 2 2 控制语句与函数编写 2 2 1 控制语句 分值
  • Python3 数据类型转换

    目录 Python3 数据类型转换 隐式类型转换 显式类型转换 Python3 数据类型转换 有时候 我们需要对数据内置的类型进行转换 数据类型的转换 一般情况下你只需要将数据类型作为函数名即可 Python 数据类型转换可以分为两种 隐式
  • js中int与string之间的转换

  • 关系型数据库和非关系型数据库

    数据库总结 关系型数据库和非关系型数据库 NOSQL 关系型数据库和非关系型数据库 NOSQL NoSQL 指的是非关系型的数据库 NoSQL有时也称作Not Only SQL的缩写 是对不同于传统的关系型数据库的数据库管理系统的统称 No
  • 护网HVV(蓝队)小白必知必会

    前言 在HVV期间 蓝队主要就是通过安全设备看告警信息 后续进行分析研判得出结论及处置建议 在此期间要注意以下内容 内网攻击告警需格外谨慎 可能是进行内网渗透 1 攻击IP是内网IP 攻击行为不定 主要包括 扫描探测行为 爆破行为 命令执行
  • 小金的2019年终总结

    文章目录 一 各平台年报 简书 中国移动 微信读书 支付宝 建设银行 二 值得说一说的事 我的舒适区 刷海贼王 致敬特斯拉惊叹达芬奇 小柠檬降世 当舅舅 换mac电脑 总结 一 各平台年报 简书 研究区块链 gt gt 投资简书 gt gt
  • 万字长文解析!复现和使用GPT-3/ChatGPT,你所应该知道的

    关于作者 英文原版作者 杨靖锋 现任亚马逊科学家 本科毕业于北大 硕士毕业于佐治亚理工学院 师从 Stanford 杨笛一教授 杨昊桐 译 王骁 修订 感谢靳弘业对第一版稿件的建议 感谢陈三星 符尧的讨论和建议 英文原版 https jin
  • 10.Vue简单项目之crud+表单验证

    Vue简单项目之crud 表单验证 1 准备工作 配置action js 2 新增书籍前台页面编写 3 修改和删除 4 表单验证 1 准备工作 配置action js 启动前端项目 启动后端项目 在action js中添加下列接口 BOOK