vue之表格增删改操作和表单验证

这里写目录标题

1.表格的增删改

  1. 添加增加按钮和点击后打开的窗体

<!-- 添加按钮 -->
 <el-button type="success" @click="open()">新增</el-button>

<!-- 弹窗组件点击新增后打开这个窗口-->
 <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 v-for="by in booktypes" :label="by.name" :value="by.name" :key="'id_'+by.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>
  1. 定义打开添加窗体的函数

该函数可以根据row是否有值来让改变窗体类型,当row有值时窗体变成编辑状态根据选择的行列来给弹出窗体填值

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;
        }
      },
  1. 定义提交按钮绑定的函数
dosub() {
        let url = this.axios.urls.BOOK_ADD;
        if (this.title == '编辑窗体') {
          url = this.axios.urls.BOOK_EDIT;
        }
        let params = {
          bookname: this.book.bookname,
          id: this.book.id,
          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 {
                his.$message({
                  message: resp.data.msg,
                  type: 'error'
                });
              }
            }).catch(err => {
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
  1. 提交后表单要清空表单内容,定义清空函数
 clear() {
        //还原窗体类型
        this.title = '新增窗体';
        this.dialogFormVisible = false;
        this.book = {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        }
      },
  1. 定义删除函数
  del(idx, row) {
        this.$confirm('你确定要删除编号为' + row.id + '书名为' + row.bookname + '的书籍信息吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          let url = this.axios.urls.BOOK_DEL;
          this.axios.post(url, {
            id: row.id
          }).then(resp => {
            console.log(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 => {})

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

其中

        this.$confirm('你确定要删除编号为' + row.id + '书名为' + row.bookname + '的书籍信息吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });

是从elementUI文档中拷过来的要学会看文档在文档中找,至此表格的增删改完成

2.表单验证

表单验证要将:rules="rules" ref="ruleForm"添加到el-form中,其中ruleForm是更具表单中model的值来改变

elementUI 原文说明:

Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop
属性设置为需校验的字段名即可。校验规则参见 async-validator

添加rulesdatareturn中定义验证规则,并在想要验证的属性定义prop进行绑定

data() {
      return {
        bookname: '',
        tableData: [],
        rows: 10,
        total: 0,
        page: 1,
        dialogFormVisible: false,
        title: '新增窗体',
        formLabelWidth: '100px',
        booktypes: [{
            id: 1,
            name: '言情'
          }, {
            id: 2,
            name: '玄幻'
          },
          {
            id: 3,
            name: '毛片'
          }
        ],

        book: {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        },
        rules: {
          bookname: [{
              required: true,
              message: '请输入书籍名称',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 10,
              message: '长度在 2 到 5 个字符',
              trigger: 'blur'
            }
          ],
          price: [{
            required: true,
            message: '请输入书籍价格',
            trigger: 'blur'
          }],
          booktype: [{
            required: true,
            message: '请输入书籍类别 ',
            trigger: 'blur'
          }],
        }
      }
    },

同时还要再确认按钮绑定的函数添加

this.$refs[formName].validate((valid) => {
          if (valid) {
           //在这里定义所有验证通过要执行的函数
          } else {
            console.log('error submit!!');
            return false;
          }
        });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值