dialog布局
表单验证
Form组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,
并将Form-Item的prop属性设置为需校验的字段名即可
<el-form-item label="活动名称" prop="name">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm"
注:有多个表单,怎么在提交进行区分?
我们在rules这里写了对表单的验证规则,但是我们如何在methods里进行指定的表单进行认证所以我们一开始就在el-form里写了ref="ruleForm"
,我们在methods里就可以用
清空表单验证信息
this.$refs[formName].resetFields();
CUD
- 新增
- 删除/修改
- 在上使用特殊的slot-scope 特性,可以接收传递给插槽的prop
<template>
<div style="padding: 20px;">
<!-- <h1>{{ ts }}</h1> -->
<!-- 面包屑导航 -->
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>文章管理</el-breadcrumb-item>
</el-breadcrumb>
<!-- 搜索筛选 -->
<el-form :inline="true" class="user-search">
<el-form-item label="搜索:">
<el-input size="small" v-model="title" 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="handleAdd()">添加</el-button>
</el-form-item>
</el-form>
<!--列表-->
<el-table size="small" :data="listData" style="width: 100%;">
<el-table-column align="center" type="selection" width="60">
</el-table-column>
<el-table-column sortable prop="id" label="文章ID" min-width="1">
</el-table-column>
<el-table-column sortable prop="title" label="文章内容" min-width="2">
</el-table-column>
<el-table-column sortable prop="body" label="文章内容" min-width="5">
</el-table-column>
<el-table-column label="操作" min-width="2">
<template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
<el-button size="mini" type="danger" @click="handleDelete(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="currentPage" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
<!-- 新增修改弹出框 -->
<el-dialog :title="articleTitle" :visible="articleDialogFormVisible" @close="doCancel">
<el-form :model="articleForm" :rules="articleRules" ref="articleForm">
<el-form-item label="文章标题" :label-width="articleFormLabelWidth" prop="title">
<el-input v-model="articleForm.title" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="文章内容" :label-width="articleFormLabelWidth" prop="body">
<el-input type="textarea" v-model="articleForm.body" rows="6"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="doCancel">取 消</el-button>
<el-button type="primary" @click="doSubmit">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
name: 'Articles',
data: function() {
return {
title: null,
listData: [],
currentPage: 1,
rows: 10,
total: 0,
articleTitle: '新增文章',
articleDialogFormVisible: false,
articleFormLabelWidth: '120px',
articleForm: {
id: null,
title: null,
body: null
},
articleRules: {
title: [{
required: true,
message: '请输入文章标题',
trigger: 'blur'
},
{
min: 3,
max: 10,
message: '长度在 3 到 10 个字符',
trigger: 'blur'
}
],
body: [{
required: true,
message: '文章内容不能为空',
trigger: 'change'
}],
}
};
},
methods: {
search: function() {
var url = this.axios.urls.SYSTEM_ARTICLE_LIST;
console.log(url);
var formData = {
title: this.title,
page: this.currentPage,
rows: this.rows
}
// 我们发现封装后的axios不需要再用qs.stringify(formData);做处理了
this.axios.post(url, formData).then(response => {
console.log(url);
console.log(response)
this.listData = response.data.result
this.total = response.data.pageBean.total;
}).catch(function(error) {
console.log(error);
});
},
handleAdd: function() {
this.articleDialogFormVisible = true;
},
doCancel: function() {
this.doClearForm();
this.articleDialogFormVisible = false;
},
doSubmit: function() {
this.$refs['articleForm'].validate((valid) => {
if (valid) {
var url = null;
if (null == this.articleForm.id) {
url = this.axios.urls.SYSTEM_ARTICLE_ADD;
} else {
url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
}
console.log(url);
this.axios.post(url, this.articleForm).then(response => {
if (response.data.code == 0) {
this.$message({
message: response.data.msg,
type: 'warning'
});
} else {
if (null == this.articleForm.id) {
this.doClearForm();
}else{
this.doClearForm();
this.articleDialogFormVisible=false;
this.search();
}
// this.search();
this.$message({
message: response.data.msg,
type: 'success'
});
}
}).catch(function(error) {
console.log(error);
});
} else {
console.log('error submit!!');
return false;
}
});
},
doClearForm: function() {
this.articleForm.id = null;
this.articleForm.title = null;
this.articleForm.body = null;
// this.articleDialogFormVisible = false;
},
handleEdit: function(id, row) {
this.articleForm.id = row.id;
this.articleForm.title = row.title;
this.articleForm.body = row.body;
this.articleDialogFormVisible = true;
},
handleDelete: function(id, row) {
this.articleForm.id = row.id;
var url = this.axios.urls.SYSTEM_ARTICLE_DEL;
this.axios.post(url, this.articleForm).then(response => {
if (response.data.code == 0) {
this.$message({
message: response.data.msg,
type: 'warning'
});
} else {
// this.search();
this.$message({
message: response.data.msg,
type: 'success'
});
this.search();
}
}).catch(function(error) {
console.log(error);
});
},
handleSizeChange: function(rows) {
this.currentPage = 1;
this.rows = rows;
this.search();
},
handleCurrentChange: function(page) {
this.currentPage = page;
this.search();
}
}
}
</script>