提示文章写完后,目录可以自动生成如何生成参考右边的帮助文档


一、ElementUI增加,删除,修改实现

1.前期的准备工作
1.1引入相应的接口

 'BOOK_ADD': '/book/addBook', //书籍添加
  'BOOK_EDIT': '/book/editBook', //书籍修改
  'BOOK_DEL': '/book/delBook', //书籍删除

1.2按钮添加

<el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        &lt;el-button type="success" @click="open()"&gt;新增</el-button>
      </el-form-item>

1.3新增出框

<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.bookname" :value="by.name" :key="'key_'+by.id"></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="doSubmit">确 定</el-button>
      </div>
    </el-dialog>

1.4在data添加数据

data() {
      return {
        bookname: '',
        tableData: [],
        page: 1,
        rows: 10,
        total: 0,
        title: '新增窗体',
        dialogFormVisible: false,
        booktypes: [{
          id: 1,
          name: '言情'
        }, {
          id: 2,
          name: '玄幻'
        }, {
          id: 3,
          name: '武侠'
        }],
        formLabelWidth: '100px',
        book: {
          id: '',
          bookname: '',
          price: '',
          booktype: ''
        },
        rules: {
          bookname: [{
              required: true,
              message: '请输入书籍名称',
              trigger: 'blur'
            },
            {
              min: 5,
              max: 12,
              message: '长度在 5 到 12 个字符',
              trigger: 'blur'
            }
          ],
          price: [{
            required: true,
            message: '请输入书籍价格',
            trigger: 'blur'
          }],
          booktype: [{
            required: true,
            message: '请输入书籍类型',
            trigger: 'blur'
          }]
        }
      }
    }

1.5在methods中添加方法

doSubmit() {
        //新增或者编辑提交后台方法
        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({
                  message: resp.data.msg,
                  type: 'error'
                });
              }

            }).catch(err => {})


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

      },
      clear() {
        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;
        }
      }

运行效果:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
书籍的修改:
2.1在ElementUI找自己想用的组件

<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="del(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

2.2.methods中添加方法

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;
}
}

 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({
                  message: resp.data.msg,
                  type: 'error'
                });
              }

            }).catch(err => {})

运行结果:
在这里插入图片描述

在这里插入图片描述
3.删除书籍功能

del(idx, row) {

    this.$confirm('您确定删除id为' + row.id + '的书籍吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        let url = this.axios.urls.BOOK_DEL;

        this.axios.post(url, {id: row.id}).then(resp => {
            if (resp.data.success) {
                this.$message({
                    message: resp.data.msg,
                    type: 'success'
                });
                this.clear();
                let params = {
                    bookname: this.bookname
                }
                this.query(params);
            } else {
                this.$message({
                    message: resp.data.msg,
                    type: 'error'
                })
            }
        })
    }).catch(() => {
        this.$message({
            type: 'info',
            message: '已取消删除'
        });
    });
},

运行结果:
在这里插入图片描述
在这里插入图片描述

二、表单的验证

1.修改弹出

<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.bookname" :value="by.name" :key="'key_'+by.id"></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="doSubmit">确 定</el-button>
      </div>
    </el-dialog>

2.在data修改对应参数,根据ElementUI官方文档

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

运行结果:
在这里插入图片描述

总结

提示这里文章进行总结
以上就是今天要讲的内容本文仅仅简单介绍了ElementUI的增删改的使用,希望对你有帮助

原文地址:https://blog.csdn.net/Lwcxz1006/article/details/131961795

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_40584.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注