• SPA项目开发之CRUD+表单验证


    目录

    一、表单校验

    1、出现表单组件 el-form

    2、通过点击新增/编辑将表单对应窗口弹出

    3、给表单设置规则 rules

    4、当表单提交时要校验规则

    二、增删改

    1、保障编辑窗体能够正常弹出

    2、表单校验通过后,调用后台数据接口完成新增功能


    一、表单校验

    1、出现表单组件 el-form

    2、通过点击新增/编辑将表单对应窗口弹出

    3、给表单设置规则 rules

    4、当表单提交时要校验规则

    表单校验

     

    校验代码:

    Articles.vue:

    1. <template>
    2. <div>
    3. <el-form :inline="true" :model="formInline" class="user-search">
    4. <el-form-item label="搜索:">
    5. <el-input size="small" v-model="formInline.title" placeholder="输入文章标题">el-input>
    6. el-form-item>
    7. <el-form-item>
    8. <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索el-button>
    9. <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加el-button>
    10. el-form-item>
    11. el-form>
    12. <el-table size="small" :data="listData" highlight-current-row style="width: 100%;">
    13. <el-table-column align="center" type="selection" min-width="60">
    14. el-table-column>
    15. <el-table-column sortable prop="id" label="文章ID" min-width="300">
    16. el-table-column>
    17. <el-table-column sortable prop="title" label="文章标题" min-width="300">
    18. el-table-column>
    19. <el-table-column sortable prop="body" label="文章内容" min-width="300">
    20. el-table-column>
    21. <el-table-column align="center" label="操作" min-width="300">
    22. <template slot-scope="scope">
    23. <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑el-button>
    24. <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除el-button>
    25. template>
    26. el-table-column>
    27. el-table>
    28. <el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
    29. :current-page="formInline.page" :page-sizes="[10, 20, 30, 50]" :page-size="100"
    30. layout="total, sizes, prev, pager, next, jumper"
    31. :total="formInline.total">
    32. el-pagination>
    33. <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog">
    34. <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
    35. <el-form-item label="文章标题" prop="title">
    36. <el-input size="small" v-model="editForm.title" auto-complete="off" placeholder="请输入文章标题">el-input>
    37. el-form-item>
    38. <el-form-item label="文章内容" prop="body">
    39. <el-input size="small" v-model="editForm.body" auto-complete="off" placeholder="请输入文章内容">el-input>
    40. el-form-item>
    41. el-form>
    42. <div slot="footer" class="dialog-footer">
    43. <el-button size="small" @click="closeDialog">取消el-button>
    44. <el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存el-button>
    45. div>
    46. el-dialog>
    47. div>
    48. template>
    49. <script>
    50. export default {
    51. name: 'Articles',
    52. data() {
    53. return {
    54. title: '',
    55. editFormVisible: false,
    56. editForm: {
    57. title: '',
    58. body: ''
    59. },
    60. rules: {
    61. title: [
    62. {required: true, message: '请输入文章标题', trigger: 'blur'},
    63. {min: 5, max: 10, message: '长度须在 5 到 10 个字符之间', trigger: 'blur'}
    64. ],
    65. body: [
    66. {required: true, message: '请输入文章内容', trigger: 'blur'}
    67. ],
    68. },
    69. listData: [],
    70. formInline: {
    71. page: 1,
    72. total: 10,
    73. title: ''
    74. }
    75. }
    76. },
    77. methods: {
    78. handleSizeChange(rows) {
    79. // console.log("页面发生改变")
    80. this.formInline.page = 1;
    81. this.formInline.rows = rows;
    82. this.search();
    83. },
    84. handleCurrentChange(page) {
    85. // console.log("当前页码发生改变")
    86. this.formInline.page = page;
    87. this.search();
    88. },
    89. //是为了代码复用
    90. doSearch(param) {
    91. // 获取树形节点的数据
    92. let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
    93. //this指的是vue实例
    94. this.axios.post(url, param)
    95. .then(resp => {//代表成功 箭头函数 jdk8的语法
    96. console.log(resp);
    97. this.listData = resp.data.result;
    98. this.formInline = resp.data.pageBean;
    99. }).catch(function () {//代表失败
    100. });
    101. },
    102. search() {
    103. // 按照条件进行查询
    104. this.doSearch(this.formInline);
    105. },
    106. closeDialog() {
    107. // 关闭窗体
    108. },
    109. handleEdit() {
    110. // 展示新增文章的表单
    111. this.editFormVisible = true;
    112. },
    113. submitForm(formName) {
    114. this.$refs[formName].validate((valid) => {
    115. if (valid) {
    116. alert('submit!');
    117. } else {
    118. console.log('error submit!!');
    119. return false;
    120. }
    121. });
    122. }
    123. },
    124. created() {
    125. this.doSearch({});
    126. }
    127. }
    128. script>
    129. <style scoped>
    130. style>

    演示效果:

    二、增删改

    1、保障编辑窗体能够正常弹出

    2、表单校验通过后,调用后台数据接口完成新增功能

    Articles.vue

    1. <script>
    2. export default {
    3. name: 'Articles',
    4. data() {
    5. return {
    6. title: '',
    7. editFormVisible: false,
    8. editForm: {
    9. title: '',
    10. body: '',
    11. id:0
    12. },
    13. rules: {
    14. title: [
    15. {required: true, message: '请输入文章标题', trigger: 'blur'},
    16. {min: 5, max: 10, message: '长度须在 5 到 10 个字符之间', trigger: 'blur'}
    17. ],
    18. body: [
    19. {required: true, message: '请输入文章内容', trigger: 'blur'}
    20. ],
    21. },
    22. listData: [],
    23. formInline: {
    24. page: 1,
    25. total: 10,
    26. title: ''
    27. }
    28. }
    29. },
    30. methods: {
    31. handleSizeChange(rows) {
    32. // console.log("页面发生改变")
    33. this.formInline.page = 1;
    34. this.formInline.rows = rows;
    35. this.search();
    36. },
    37. handleCurrentChange(page) {
    38. // console.log("当前页码发生改变")
    39. this.formInline.page = page;
    40. this.search();
    41. },
    42. //是为了代码复用
    43. doSearch(param) {
    44. // 获取树形节点的数据
    45. let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
    46. //this指的是vue实例
    47. this.axios.post(url, param)
    48. .then(resp => {//代表成功 箭头函数 jdk8的语法
    49. console.log(resp);
    50. this.listData = resp.data.result;
    51. this.formInline.total = resp.data.pageBean.total;
    52. }).catch(function () {//代表失败
    53. });
    54. },
    55. search() {
    56. // 按照条件进行查询
    57. this.doSearch(this.formInline);
    58. },
    59. closeDialog() {
    60. // 关闭窗体
    61. this.clearData();
    62. },
    63. clearData(){
    64. // 清楚编辑窗体的缓存数据
    65. this.editForm.title='';
    66. this.editForm.id=0;
    67. this.editForm.body='';
    68. this.title='';
    69. this.editFormVisible=false;
    70. },
    71. handleEdit(index,row) {
    72. this.clearData();
    73. // 展示新增文章的表单
    74. this.editFormVisible = true;
    75. if(row){
    76. // 编辑
    77. this.title='编辑窗体';
    78. this.editForm.id = row.id;
    79. this.editForm.title = row.title;
    80. this.editForm.body=row.body
    81. }else{
    82. //新增
    83. this.title='新增窗体';
    84. }
    85. },
    86. deleteUser(index,row){
    87. this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
    88. confirmButtonText: '确定',
    89. cancelButtonText: '取消',
    90. type: 'warning'
    91. }).then(() => {
    92. let url =this.axios.urls.SYSTEM_ARTICLE_DEL;
    93. this.axios.post(url, {id:row.id}).then(r=>{
    94. // 新增成功之后,1、关闭窗体 ,清空数据 2、重新查询
    95. this.$message({
    96. message: r.data.msg,
    97. type: 'success'
    98. });
    99. this.closeDialog();
    100. this.search();
    101. }).catch(e=>{
    102. })
    103. }).catch(() => {
    104. this.$message({
    105. type: 'info',
    106. message: '已取消删除'
    107. });
    108. });
    109. },
    110. submitForm(formName) {
    111. this.$refs[formName].validate((valid) => {
    112. if (valid) {
    113. let url ;
    114. if(this.editForm.id==0){
    115. //新增
    116. url=this.axios.urls.SYSTEM_ARTICLE_ADD;
    117. }else {
    118. url=this.axios.urls.SYSTEM_ARTICLE_EDIT;
    119. }
    120. this.axios.post(url,this.editForm).then(r=>{
    121. // 新增成功之后,1、关闭窗体 ,清空数据 2、重新查询
    122. this.$message({
    123. message: r.data.msg,
    124. type: 'success'
    125. });
    126. this.closeDialog();
    127. this.search();
    128. }).catch(e=>{
    129. })
    130. } else {
    131. console.log('error submit!!');
    132. return false;
    133. }
    134. });
    135. }
    136. },
    137. created() {
    138. this.doSearch({});
    139. }
    140. }
    141. script>
    142. <style scoped>
    143. style>

     删除代码可从官网上去找:

     

    运行新增:

     

     

    编辑:

     

     

    删除:

     

     

    今日分享完毕,再见!

  • 相关阅读:
    Windows7 - 永恒之蓝 - 测试
    Maven
    PYTHON 120道题目详解(115-117)
    iOS基础介绍(二)
    APP攻防之博弈历程
    c 声明、定义、初始化的差别
    MySQL数据的ONLINE DDL操作测试
    体系认证服务认证产品认证的相同点与不同点
    sklearn决策树(Decision Trees)模型
    使用OfficeTool免费安装Office
  • 原文地址:https://blog.csdn.net/weixin_65808248/article/details/126834607