ElementUI实现增删改功能以及表单验证

简介: ElementUI实现增删改功能以及表单验证

前言

本篇还是在之前的基础上,继续完善功能。上一篇完成了数据表格的查询,这一篇完善增删改,以及表单验证。

BookList.vue

<template>
  <div class="books" 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="primary" @click="open">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 数据表格 -->
    <template>
      <el-table :data="tableData" border 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">
            <el-button size="mini" @click="open(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <div class="block">
      <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>
    </div>
    <!-- 编辑窗口 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clear()">
      <el-form :model="book" :rules="rules" ref="book">
        <el-form-item label="书籍编号" :label-width="formLabelWidth" :disabled="true">
          <el-input :value="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 v-for="t in types" :label="t.name" :value="t.name" :key="'key_'+t.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</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,
        title: '新增窗口',
        dialogFormVisible: false,
        formLabelWidth: '100px',
        types: [],
        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: {
      // 删除
      handleDelete(idx, row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let url = this.axios.urls.BOOK_DEL;
          this.axios.post(url, {
            id: row.id
          }).then(r => {
            console.log(r);
            this.query({});
          }).catch(e => {
          });
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 确认新增
      dosub() {
        this.$refs['book'].validate((valid) => {
          if (valid) {
            let url = this.axios.urls.BOOK_ADD;
            if (this.title == '编辑窗口') {
              url = this.axios.urls.BOOK_UPD;
            };
            let params = {
              id: this.book.id,
              bookname: this.book.bookname,
              price: this.book.price,
              booktype: this.book.booktype
            };
            this.axios.post(url, params).then(r => {
              console.log(r);
              this.clear();
              this.query({});
            }).catch(e => {
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      // 初始化窗口
      clear() {
        this.dialogFormVisible = false;
        this.title = '新增窗口',
          this.book = {
            id: '',
            bookname: '',
            price: '',
            booktype: ''
          }
      },
      // 打开窗口的方法
      open(idx, row) {
        this.dialogFormVisible = true;
        // console.log(idx);
        // console.log(row)
        if (row) {
          this.title = '编辑窗口';
          this.book.id = row.id;
          this.book.bookname = row.bookname;
          this.book.price = row.price;
          this.book.booktype = row.booktype;
        }
      },
      // 当前页大小
      handleSizeChange(r) {
        let params = {
          bookname: this.bookname,
          rows: r,
          page: this.page
        }
        this.query(params);
        // 当前页码
      },
      handleCurrentChange(p) {
        let params = {
          bookname: this.bookname,
          rows: this.rows,
          page: p
        }
        this.query(params);
      },
      query(params) {
        let url = this.axios.urls.BOOK_LIST;
        this.axios.get(url, {
          params: params
        }).then(r => {
          console.log(r);
          this.tableData = r.data.rows;
          this.total = r.data.total;
        }).catch(e => {
        });
      },
      onSubmit() {
        let params = {
          bookname: this.bookname
        }
        this.query(params);
      }
    },
    created() {
      this.query({});
      this.types = [{
        id: 1,
        name: '玄幻'
      }, {
        id: 2,
        name: '武侠'
      }, {
        id: 3,
        name: '言情'
      }, {
        id: 4,
        name: '推理'
      }, {
        id: 5,
        name: '恐怖'
      }];
    }
  }
</script>
<style>
</style>

action.js

/**
 * 对后台请求的地址的封装,URL格式如下:
 * 模块名_实体名_操作
 */
export default {
  'SERVER': 'http://localhost:8080', //服务器
  'SYSTEM_USER_DOLOGIN': '/user/userLogin', //登陆
  'SYSTEM_USER_DOREG': '/user/userRegister', //注册
  'SYSTEM_MENUS': '/module/queryRootNode', //左侧菜单树
  'BOOK_LIST': '/book/queryBookPager', //数据表格
  'BOOK_ADD': '/book/addBook', //数据增加
  'BOOK_UPD': '/book/editBook', //数据修改
  'BOOK_DEL': '/book/delBook', //数据删除
  'getFullPath': k => { //获得请求的完整地址,用于mockjs测试时使用
    return this.SERVER + this[k];
  }
}

展示效果

 

相关文章
Ansible-playbook 并发运行async、poll(学习笔记二十二)
ansible默认只会创建5个进程,所以一次任务只能同时控制5台机器执行.那如果你有大量的机器需要控制,或者你希望减少进程数,那你可以采取异步执行.ansible的模块可以把task放进后台,然后轮询它.
5203 0
|
Dart
[Flutter]足够入门的Dart语言系列之函数:函数定义、调用、5种参数类型和main函数
函数(Function)也被称为方法(Method)。其最直观的理解就是数据中的函数,比如y=f(x),在编程中,f对输入x进行处理,返回结果y,就是一个函数......
1533 0
[Flutter]足够入门的Dart语言系列之函数:函数定义、调用、5种参数类型和main函数
|
9月前
|
分布式计算 数据可视化 大数据
大数据+GIS:别光想着看地图,人家早就开始“算”地图了!
大数据+GIS:别光想着看地图,人家早就开始“算”地图了!
308 17
|
7月前
|
文字识别 Python
文字识别自动点击器, 脚本识别文字然后点击软件,按键精灵识别文字点击
该实现包含完整的OCR识别和自动化点击功能,支持多种配置选项和文本匹配模式。使用时需
|
Ubuntu 开发工具 git
Ubuntu编译ffmpeg解决错误:ERROR: avisynth/avisynth_c.h not found
通过本文的详细指导,您可以顺利地在Ubuntu系统上配置和编译FFmpeg,并解决Avisynth头文件缺失的问题。
586 27
|
11月前
|
人工智能 数据可视化 定位技术
AI 小技巧 | PPT 也能用数据地图?
AI 小技巧 | PPT 也能用数据地图?
574 4
|
人工智能 文字识别 自然语言处理
Nougat:一种用于科学文档OCR的Transformer 模型
随着人工智能领域的不断进步,其子领域,包括自然语言处理,自然语言生成,计算机视觉等,由于其广泛的用例而迅速获得了大量的普及。光学字符识别(OCR)是计算机视觉中一个成熟且被广泛研究的领域。它有许多用途,如文档数字化、手写识别和场景文本识别。数学表达式的识别是OCR在学术研究中受到广泛关注的一个领域。
619 0
|
存储 C++
c++的指针完整教程
本文提供了一个全面的C++指针教程,包括指针的声明与初始化、访问指针指向的值、指针运算、指针与函数的关系、动态内存分配,以及不同类型指针(如一级指针、二级指针、整型指针、字符指针、数组指针、函数指针、成员指针、void指针)的介绍,还提到了不同位数机器上指针大小的差异。
581 1
|
运维 监控 大数据
高效运维管理:提升系统稳定性的策略与实践
在当今信息技术飞速发展的时代,运维管理作为保障系统稳定运行的关键环节,其重要性不言而喻。本文将深入探讨如何通过优化运维流程、引入自动化工具和建立完善的监控体系等策略,来有效提升系统的稳定性。同时,结合具体实践案例,分析这些策略在实际工作中的应用效果,为运维人员提供有益的参考和启示。
699 6
|
JSON 数据格式 Python
Flask实现内部接口----pycharm安装及新建,location代表着文件路径,下面是Python的环境,Flask是由Python开发的框架,Python文件接口ython通过GET发送
Flask实现内部接口----pycharm安装及新建,location代表着文件路径,下面是Python的环境,Flask是由Python开发的框架,Python文件接口ython通过GET发送