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

文章目录

一、ElementUI增加,删除,修改的实现二、表单的验证1.修改弹出框2.在data修改对应的参数,根据ElementUI官方文档

总结

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

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

'BOOK_ADD': '/book/addBook', //书籍添加

'BOOK_EDIT': '/book/editBook', //书籍修改

'BOOK_DEL': '/book/delBook', //书籍删除

1.2按钮的添加

查询

新增

1.3新增弹出框

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找自己想用的组件

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.修改弹出框

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的增删改的使用,希望对你有帮助

相关阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: