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>
- 定义打开添加窗体的函数
该函数可以根据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;
}
},
- 定义提交按钮绑定的函数
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;
}
});
},
- 提交后表单要清空表单内容,定义清空函数
clear() {
//还原窗体类型
this.title = '新增窗体';
this.dialogFormVisible = false;
this.book = {
id: '',
bookname: '',
price: '',
booktype: ''
}
},
- 定义删除函数
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
添加rules
到data
的return
中定义验证规则,并在想要验证的属性定义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;
}
});