功能:
功能:
<!--点击事件add()弹出增加文章的对话框 -->
<el-button size="small" type="primary" icon="el-icon-plus" @click="add()">增加</el-button>
<!-- 编辑界面 -->
<el-dialog title="title" :visible.sync="dialogVisible" width="30%">
<el-form :model="editFrom" :rules="rules" ref="editFrom.title">
<el-form-item label="文章标题" prop="tilte">
<el-input v-model="editFrom.title" auto-complete="off" placeholder="请输入文章标题"></el-input>
</el-form-item>
<el-form-item label="文章内容" prop="body">
<el-input v-model="editFrom.body" auto-complete="off" placeholder="请输入文章内容"></el-input>
</el-form-item>
</el-from>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitForm(editFrom)">确 定</el-button>
</span>
</el-dialog>
<script>
export default {
data() {
return {
dialogVisible: false,
title: '',
editFrom: {
title: '',
body: '',
id: 0
},
rules: {
title: [{
required: true,
message: '请输入文章标题',
trigger: 'blur'
},
{
min: 3,
max: 20,
message: '长度在 3 到 20 个字符',
trigger: 'blur'
}
],
body: [{
required: true,
message: '请输入文章内容',
trigger: 'blur'
},
{
min: 3,
max: 500,
message: '长度在 3 到 500 个字符',
trigger: 'blur'
}
]
}
};
},
methods: {
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
this.dialogVisible = false;
this.clearFrom();
})
.catch(_ => {});
},
add() {
this.title = '新增文章'
this.dialogVisible = true;
this.clearFrom();
}
}
};
</script>
并将Form-Item的prop属性设置为需校验的字段名即可 注1:有多个表单,怎么在提交进行区分?
<el-form :model="editFrom" :rules="rules" ref="editFrom.title">
<el-form-item label="文章标题" prop="tilte">
<el-input v-model="editFrom.title" auto-complete="off" placeholder="请输入文章标题"></el-input>
</el-form-item>
<el-form-item label="文章内容" prop="body">
<el-input v-model="editFrom.body" auto-complete="off" placeholder="请输入文章内容"></el-input>
</el-form-item>
</el-from>
<script>
export default {
data() {
return {
dialogVisible: false,
title: '',
editFrom: {
title: '',
body: '',
id: 0
},
rules: {
title: [{
required: true,
message: '请输入文章标题',
trigger: 'blur'
},
{
min: 3,
max: 20,
message: '长度在 3 到 20 个字符',
trigger: 'blur'
}
],
body: [{
required: true,
message: '请输入文章内容',
trigger: 'blur'
},
{
min: 3,
max: 500,
message: '长度在 3 到 500 个字符',
trigger: 'blur'
}
]
}
};
</script>
我们在rules这里写了对表单的验证规则,但是我们如何在methods里进行指定的表单进行认证,
所以我们一开始就在el-form里写了 ref="ruleForm",我们在methods里就可以用
<template>
<div>
<el-form :inline="true" :model="formInline" class="user-search">
<el-form-item label="搜索:">
<el-input size="small" v-model="formInline.deptName" placeholder="输入文章标题"></el-input>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
<el-button size="small" type="primary" icon="el-icon-plus" @click="add()">增加</el-button>
</el-form-item>
</el-form>
<el-table :data="Data" border style="width: 100%">
<el-table-column prop="id" label="id" width="180">
</el-table-column>
<el-table-column prop="tile" label="文章标题" width="180">
</el-table-column>
<el-table-column prop="body" label="文章内容">
</el-table-column>
</el-table-column>
</el-table>
<!-- 分页条 -->
<el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper"
:total="pageBean.total">
</el-pagination>
<!-- 编辑界面 -->
<el-dialog title="title" :visible.sync="dialogVisible" width="30%">
<el-form :model="editFrom" :rules="rules" ref="editFrom.title">
<el-form-item label="文章标题" prop="tilte">
<el-input v-model="editFrom.title" auto-complete="off" placeholder="请输入文章标题"></el-input>
</el-form-item>
<el-form-item label="文章内容" prop="body">
<el-input v-model="editFrom.body" auto-complete="off" placeholder="请输入文章内容"></el-input>
</el-form-item>
</el-from>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitForm(editFrom)">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
Data: [],
pageBean: {
total: 0,
},
formInline: {
title: '',
page: 1,
rows: 10,
},
dialogVisible: false,
title: '',
editFrom: {
title: '',
body: '',
id: 0
},
rules: {
title: [{
required: true,
message: '请输入文章标题',
trigger: 'blur'
},
{
min: 3,
max: 20,
message: '长度在 3 到 20 个字符',
trigger: 'blur'
}
],
body: [{
required: true,
message: '请输入文章内容',
trigger: 'blur'
},
{
min: 3,
max: 500,
message: '长度在 3 到 500 个字符',
trigger: 'blur'
}
]
}
};
},
methods: {
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
this.dialogVisible = false;
this.clearFrom();
})
.catch(_ => {});
},
submitForm(editFrom) {
//用来提交新增、修改的表单数据的,提交之前需要通过vue实例中定义的表单填写规则
this.$refs[formName].validate((valid) => {
if (valid) {
let url;
if(this.editFrom.id ==0){
url = this.axios.urls.SYSTEM_ARTICLE_ADD;
}else{
url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
}
this.axios.post(url, this.editFrom ).then((response) => {
//这是请求成功的回掉
this.clearFrom()
this.doSearch(this.formInline);
}).catch((response) => {
//这是请求异常执行的回掉
console.log(response);
});
} else {
//清空文本框内容
this.clearFrom();
console.log('error submit!!');
return false;
}
});
},
clearFrom() {
//请空文本框内容
this.editFrom.id = 0;
this.editFrom.title = '';
this.editFrom.body = '';
this.formInline.page =1;
this.formInline.rows =10;
//关闭编辑窗口
this.dialogVisible =false;
},
add() {
//需要添加对话框到进行输入值,并且还要调用保存的方法
//还需要用到表单验证,文字是否符合
this.title = '新增文章'
//打开对话框
this.dialogVisible = true;
//重新查询是刷新的意思
this.clearFrom();
},
search() {
this.doSearch(this.formInline);
},
doSearch(params) {
let url = this.axios.urls.SYSTEM_USER_DOLOGIN;
this.axios.post(url, params).then((response) => {
//这是请求成功的回掉
this.menus = response.data.result;
this.pageBean = response.data.pageBean;
}).catch((response) => {
//这是请求异常执行的回掉
console.log(response);
});
},
handleSizeChange(rows) { //页码大小发生改变的时候触发
this.formInline.page = 1;
this.formInline.rows = rows;
this.search();
},
handleCurrentChange(page) { //当前页码发生改变的时候触发
this.formInline.page = page;
this.search();
},
created() {
this.doSearch({});
}
}
};
</script>
<template>
<div>
<el-form :inline="true" :model="formInline" class="user-search">
<el-form-item label="搜索:">
<el-input size="small" v-model="formInline.deptName" placeholder="输入文章标题"></el-input>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
</el-form-item>
</el-form>
<el-table :data="Data" border style="width: 100%">
<el-table-column prop="id" label="id" width="180">
</el-table-column>
<el-table-column prop="tile" label="文章标题" width="180">
</el-table-column>
<el-table-column prop="body" label="文章内容">
</el-table-column>
<el-table-column align="center" label="操作">
<template>
<el-button size="mini" @click="edit(scope.$index,scope.row)">编辑</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页条 -->
<el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper"
:total="pageBean.total">
</el-pagination>
<!-- 编辑界面 -->
<el-dialog title="title" :visible.sync="dialogVisible" width="30%">
<el-form :model="editFrom" :rules="rules" ref="editFrom.title">
<el-form-item label="文章标题" prop="tilte">
<el-input v-model="editFrom.title" auto-complete="off" placeholder="请输入文章标题"></el-input>
</el-form-item>
<el-form-item label="文章内容" prop="body">
<el-input v-model="editFrom.body" auto-complete="off" placeholder="请输入文章内容"></el-input>
</el-form-item>
</el-from>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitForm(editFrom)">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
Data: [],
pageBean: {
total: 0,
},
formInline: {
title: '',
page: 1,
rows: 10,
}
dialogVisible: false,
title: '',
editFrom: {
title: '',
body: '',
id: 0
},
rules: {
title: [{
required: true,
message: '请输入文章标题',
trigger: 'blur'
},
{
min: 3,
max: 20,
message: '长度在 3 到 20 个字符',
trigger: 'blur'
}
],
body: [{
required: true,
message: '请输入文章内容',
trigger: 'blur'
},
{
min: 3,
max: 500,
message: '长度在 3 到 500 个字符',
trigger: 'blur'
}
]
}
};
},
methods: {
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
this.dialogVisible = false;
this.clearFrom();
})
.catch(_ => {});
},
submitForm(editFrom) {
//用来提交新增、修改的表单数据的,提交之前需要通过vue实例中定义的表单填写规则
this.$refs[formName].validate((valid) => {
if (valid) {
let url;
if(this.editFrom.id ==0){
url = this.axios.urls.SYSTEM_ARTICLE_ADD;
}else{
url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
}
this.axios.post(url, this.editFrom ).then((response) => {
//这是请求成功的回掉
this.clearFrom()
this.doSearch(this.formInline);
}).catch((response) => {
//这是请求异常执行的回掉
console.log(response);
});
} else {
//清空文本框内容
this.clearFrom();
console.log('error submit!!');
return false;
}
});
},
clearFrom() {
this.editFrom.id = 0;
this.editFrom.title = '';
this.editFrom.body = '';
this.formInline.page =1;
this.formInline.rows =10;
//关闭编辑窗口
this.dialogVisible =false;
},
edit(index, row) {
//index代表的是操作数据在当前界面的行号
//row代表操作的当前数据,那也就意味着可以冲row中或所有数据库列段名
//跟增加方法共同用到保存的方法,还需要同过id传值赋值所有的其他内容
this.title = '编辑文章'
this.editFrom.id = row.id;
this.editFrom.title = row.title;
this.editFrom.body = row.body;
//打开文本对话框
this.dialogVisible = true;
},
search() {
this.doSearch(this.formInline);
},
doSearch(params) {
let url = this.axios.urls.SYSTEM_USER_DOLOGIN;
this.axios.post(url, params).then((response) => {
//这是请求成功的回掉
this.menus = response.data.result;
this.pageBean = response.data.pageBean;
}).catch((response) => {
//这是请求异常执行的回掉
console.log(response);
});
},
handleSizeChange(rows) { //页码大小发生改变的时候触发
this.formInline.page = 1;
this.formInline.rows = rows;
this.search();
},
handleCurrentChange(page) { //当前页码发生改变的时候触发
this.formInline.page = page;
this.search();
},
created() {
this.doSearch({});
}
}
</script>
<template>
<div>
<el-form :inline="true" :model="formInline" class="user-search">
<el-form-item label="搜索:">
<el-input size="small" v-model="formInline.deptName" placeholder="输入文章标题"></el-input>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
</el-form-item>
</el-form>
<el-table :data="Data" border style="width: 100%">
<el-table-column prop="id" label="id" width="180">
</el-table-column>
<el-table-column prop="tile" label="文章标题" width="180">
</el-table-column>
<el-table-column prop="body" label="文章内容">
</el-table-column>
<el-table-column align="center" label="操作">
<template>
<el-button size="mini" type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页条 -->
<el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper"
:total="pageBean.total">
</el-pagination>
</div>
</template>
<script>
export default {
data() {
return {
Data: [],
pageBean: {
total: 0,
},
formInline: {
title: '',
page: 1,
rows: 10,
}
dialogVisible: false,
title: '',
editFrom: {
title: '',
body: '',
id: 0
},
rules: {
title: [{
required: true,
message: '请输入文章标题',
trigger: 'blur'
},
{
min: 3,
max: 20,
message: '长度在 3 到 20 个字符',
trigger: 'blur'
}
],
body: [{
required: true,
message: '请输入文章内容',
trigger: 'blur'
},
{
min: 3,
max: 500,
message: '长度在 3 到 500 个字符',
trigger: 'blur'
}
]
}
};
},
methods: {
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done();
this.dialogVisible = false;
this.clearFrom();
})
.catch(_ => {});
},
submitForm(editFrom) {
//用来提交新增、修改的表单数据的,提交之前需要通过vue实例中定义的表单填写规则
this.$refs[formName].validate((valid) => {
if (valid) {
let url;
if(this.editFrom.id ==0){
url = this.axios.urls.SYSTEM_ARTICLE_ADD;
}else{
url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
}
this.axios.post(url, this.editFrom ).then((response) => {
//这是请求成功的回掉
this.clearFrom()
this.doSearch(this.formInline);
}).catch((response) => {
//这是请求异常执行的回掉
console.log(response);
});
} else {
//清空文本框内容
this.clearFrom();
console.log('error submit!!');
return false;
}
});
},
clearFrom() {
this.editFrom.id = 0;
this.editFrom.title = '';
this.editFrom.body = '';
this.formInline.page =1;
this.formInline.rows =10;
//关闭编辑窗口
this.dialogVisible =false;
},
del(index, row) {
//index代表的是操作数据在当前界面的行号
// 删除方法的话就比较简单不需要表单验证跟对话框了
// 直接传id执行点击事件请求后台地址路径this.axios.urls.SYSTEM_ARTICLE_DEL直接进行删除
let url = this.axios.urls.SYSTEM_ARTICLE_DEL;
this.axios.post(url, {id :row.id}).then((response) => {//获取到row。id的值
//这是请求成功的回掉
this.clearFrom()
this.doSearch({});
}).catch((response) => {
//这是请求异常执行的回掉
console.log(response);
});
},
search() {
this.doSearch(this.formInline);
},
doSearch(params) {
let url = this.axios.urls.SYSTEM_USER_DOLOGIN;
this.axios.post(url, params).then((response) => {
//这是请求成功的回掉
this.menus = response.data.result;
this.pageBean = response.data.pageBean;
}).catch((response) => {
//这是请求异常执行的回掉
console.log(response);
});
},
handleSizeChange(rows) { //页码大小发生改变的时候触发
this.formInline.page = 1;
this.formInline.rows = rows;
this.search();
},
handleCurrentChange(page) { //当前页码发生改变的时候触发
this.formInline.page = page;
this.search();
},
created() {
this.doSearch({});
}
}