1.增加书籍功能编写
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="'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="doSub">确定</el-button>
</div>
</el-dialog>
1.2定义变量
title: '新增窗体',
dialogFormVisible: false,
booktypes: [{
id: 1,
name: '言情'
},
{
id: 2,
name: '玄幻'
},
{
id: 3,
name: '武侠'
}
],
formLabelWidth: '100px',
book: {
id: '',
bookname: '',
price: '',
booktype: ''
}
1.3定义方法
doSub
doSub() {
// 新增/编辑提交到后台的数据
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
clear() {
this.title = '新增窗体';
this.dialogFormVisible = false;
this.book = {
id: '',
bookname: '',
price: '',
booktype: ''
};
}
open
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;
}
query
query(params) {
// 通用查询方法
let url = this.axios.urls.BOOK_LIST;
this.axios.get(url, {
params: params
}).then(resp => {
console.log(resp);
this.tableData = resp.data.rows;
this.total = resp.data.total;
}).catch(error => {
})
}
1.4效果展示
2.修改书籍功能编写
2.1样式添加
<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定义变量
title: '新增窗体',
dialogFormVisible: false,
booktypes: [{
id: 1,
name: '言情'
},
{
id: 2,
name: '玄幻'
},
{
id: 3,
name: '武侠'
}
],
formLabelWidth: '100px',
book: {
id: '',
bookname: '',
price: '',
booktype: ''
}
2.3定义方法
doSub
doSub() {
// 新增/编辑提交到后台的数据
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
clear() {
this.title = '新增窗体';
this.dialogFormVisible = false;
this.book = {
id: '',
bookname: '',
price: '',
booktype: ''
};
}
open
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;
}
query
query(params) {
// 通用查询方法
let url = this.axios.urls.BOOK_LIST;
this.axios.get(url, {
params: params
}).then(resp => {
console.log(resp);
this.tableData = resp.data.rows;
this.total = resp.data.total;
}).catch(error => {
})
}
2.4效果展示
3.删除书籍功能编写
3.1样式添加
<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>
3.2定义变量
3.3定义方法
del
del(idx, row) {
this.$confirm('你确定要删除id为' + row.id + '的书籍吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(resp => {
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.clear();
this.query({});
} else {
this.$message({
message: resp.data.msg,
type: 'error'
});
}
}).catch(err => {
})
}).catch(err => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
}
query
query(params) {
// 通用查询方法
let url = this.axios.urls.BOOK_LIST;
this.axios.get(url, {
params: params
}).then(resp => {
console.log(resp);
this.tableData = resp.data.rows;
this.total = resp.data.total;
}).catch(error => {
})
}
3.4效果展示
表单验证
4.1样式添加
<el-form :model="book" :rules="rules" ref="book">
<el-form-item label="名称" :label-width="formLabelWidth" prop="bookname">
<el-form-item label="价格" :label-width="formLabelWidth" prop="price">
<el-form-item label="类别" :label-width="formLabelWidth" prop="booktype">
4.2定义变量
rules: {
bookname: [{
required: true,
message: '请输入书籍名称',
trigger: 'blur'
},
{
min: 4,
max: 10,
message: '长度在3到5个字符',
trigger: 'blur'
}
],
price: [{
required: true,
message: '请输入书籍价格',
trigger: 'blur'
}],
booktype: [{
required: true,
message: '请输入书籍类型',
trigger: 'blur'
}]
}