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


    目录

    一、表单验证

    思路

    所需表单

    增加和编辑页面:

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

    表单验证

    表单提交时进行校验规则 

    效果​编辑

    二、CRUD

    表单校验通过后,调用后台数据接口完成新增功能(删改同样如此)

    效果展示

    新增:

     修改:

     删除


    一、表单验证

    思路

    1.出现表单组件 el-form
    2.通过点击 新增/编辑将表单对应窗口弹出
    3.给表单设置规则 rules
    4.当表单提交时要校验规则

    所需表单

    1. <el-form :inline="true" :model="formInline" class="user-search">
    2. <el-form-item label="搜索:">
    3. <el-input size="small" v-model="formInline.title" placeholder="输入文章标题">el-input>
    4. el-form-item>
    5. <el-form-item>
    6. <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索el-button>
    7. <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加el-button>
    8. el-form-item>
    9. el-form>
    10. <el-table size="small" :data="listData" highlight-current-row border style="width: 100%;">
    11. <el-table-column align="center" type="selection" width="60">
    12. el-table-column>
    13. <el-table-column sortable prop="id" label="文章ID" width="300">
    14. el-table-column>
    15. <el-table-column sortable prop="title" label="文章标题" width="300">
    16. el-table-column>
    17. <el-table-column sortable prop="body" label="文章内容" width="300">
    18. el-table-column>
    19. <el-table-column align="center" label="操作" min-width="300">
    20. <template slot-scope="scope">
    21. <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑el-button>
    22. <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除el-button>
    23. template>
    24. el-table-column>
    25. el-table>

    增加和编辑页面:

    1. <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
    2. <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
    3. <el-form-item label="文章标题" prop="title">
    4. <el-input size="small" v-model="editForm.title" auto-complete="off" placeholder="请输入部门名称">el-input>
    5. el-form-item>
    6. <el-form-item label="文章内容" prop="body">
    7. <el-input size="small" v-model="editForm.body" auto-complete="off" placeholder="请输入部门代码">el-input>
    8. el-form-item>
    9. el-form>
    10. <div slot="footer" class="dialog-footer">
    11. <el-button size="small" @click="closeDialog">取消el-button>
    12. <el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存el-button>
    13. div>
    14. el-dialog>

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

    判断是弹出新增还是编辑

    1. handleEdit(index, row) {
    2. this.clearData();
    3. this.editFormVisible = true;
    4. if (row) {
    5. this.title = '编辑窗口';
    6. this.editForm.id = row.id;
    7. this.editForm.title = row.title;
    8. this.editForm.body = row.body;
    9. } else {
    10. this.title = '新增窗口';
    11. }
    12. },

    表单验证

     给表单设置规则 都放入Articles.vue

    1. rules: {
    2. title: [{
    3. required: true,
    4. message: '请输入文章标题',
    5. trigger: 'blur'
    6. },
    7. {
    8. min: 5,
    9. max: 10,
    10. message: '长度在 5 到 10 个字符',
    11. trigger: 'blur'
    12. }
    13. ],
    14. body: [{
    15. required: true,
    16. message: '请选择文章内容',
    17. trigger: 'blur'
    18. }]
    19. }

    表单提交时进行校验规则 

    1. submitForm(formName) {
    2. this.$refs[formName].validate((valid) => {
    3. if (valid) {
    4. let url;
    5. if (this.editForm.id == 0) {
    6. //新增
    7. url = this.axios.urls.SYSTEM_ARTICLE_ADD;
    8. } else {
    9. //修改
    10. url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
    11. }
    12. this.axios.post(url, this.editForm).then(r => {
    13. //新增成功后 1.关闭窗体,数据清空 2.重新查询
    14. this.$message({
    15. message: r.data.msg,
    16. type: 'success'
    17. });
    18. this.closeDialog();
    19. this.search();
    20. }).catch(e => {
    21. });
    22. } else {
    23. console.log('error submit!!');
    24. return false;
    25. }
    26. });
    27. }

    效果

    二、CRUD

    保障编辑窗体的正常弹出

    表单校验通过后,调用后台数据接口完成新增功能(删改同样如此)

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

    效果展示

    新增:

     修改:

     删除

    取消删除

     确定删除

  • 相关阅读:
    C++11 之 override
    链表oj3(Leetcode)——相交链表;环形链表
    mybatis04关联关系映射
    ByteX-shrink_r源码解析
    c++基础:指针
    减治法(引例中位数、查找问题:折半&二叉&选择、排序问题:堆排序、组合问题:淘汰赛冠军问题&假币问题)
    线程同步互斥锁
    Linux/VC:进度条的小程序
    【遥控器开发基础教程1】疯壳·开源编队无人机-GPIO(遥控器指示灯控制)
    【华为OD题库-033】经典屏保-java
  • 原文地址:https://blog.csdn.net/weixin_65211978/article/details/126837484