• SPA项目开发(CRUD&表单验证)


    目录

    一、表单验证

    二、增删改


    一、表单验证

    步骤:

     ①、将以下拷入Articles.vue中分页条下面

    1. <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="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>

    拷入后会报错,将title和editFormVisible、editForm赋值

    1. title: '',
    2. editFormVisible: false,
    3. editForm: {
    4. title: '',
    5. body: '',
    6. id: 0
    7. },

     然后先加入一个方法closeDialog

    1. closeDialog() {
    2. // 关闭窗体
    3. }

    ②、 写一个方法 handleEdit

    1. handleEdit(index, row) {
    2. // 展示新增文章的表单窗体
    3. this.editFormVisible = true;
    4. }

    效果:

     ③、将以下代码拷入

    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. alert('submit!');
    5. } else {
    6. console.log('error submit!!');
    7. return false;
    8. }
    9. });
    10. },

     效果:

     步骤总结:

    1、拷入表单组件 el-from

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

    3、给表单设置规则 rules

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

    二、增删改

    写个方法:clearData

    1. clearData() {
    2. // 清除编辑窗体的缓存数据
    3. this.editForm.title = '';
    4. this.editForm.id = 0;
    5. this.editForm.body = '';
    6. this.title = '';
    7. this.editFormVisible = false;
    8. }

    在关闭窗体方法中加入以下代码

    this.clearData();

    handleEdit方法中加入以下代码

    1. this.clearData();
    2. // 展示新增文章的表单窗体
    3. this.editFormVisible = true;
    4. if (row) {
    5. // 编辑
    6. this.title = '编辑窗体';
    7. this.editForm.id = row.id;
    8. this.editForm.title = row.title;
    9. this.editForm.body = row.body;
    10. } else {
    11. // 新增
    12. this.title = '新增窗体';
    13. }

    写入以下代码

    1. clearData() {
    2. // 清除编辑窗体的缓存数据
    3. this.editForm.title = '';
    4. this.editForm.id = 0;
    5. this.editForm.body = '';
    6. this.title = '';
    7. this.editFormVisible = false;
    8. }

    关闭窗体方法和handleEdit中调用以上该方法

    this.clearData();

    对submitForm方法进行优化

    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. url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
    10. }
    11. this.axios.post(url, this.editForm).then(r => {
    12. // 新增成功 1.关闭窗体-清空数据 2.重新查询
    13. this.$message({
    14. message: r.data.msg,
    15. type: 'success'
    16. });
    17. this.clearData();
    18. this.search();
    19. }).catch(e => {
    20. });
    21. } else {
    22. console.log('error submit!!');
    23. return false;
    24. }
    25. });
    26. }

    然后更改doSearch中的以下代码,改成第二个代码

    this.formInline = resp.data.pageBean;
    this.formInline.total = resp.data.pageBean.total;

     最后写个删除方法

    1. deleteUser(index, row) {
    2. this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    3. confirmButtonText: '确定',
    4. cancelButtonText: '取消',
    5. type: 'warning'
    6. }).then(() => {
    7. let url = this.axios.urls.SYSTEM_ARTICLE_DEL;
    8. this.axios.post(url, {id:row.id}).then(r => {
    9. // 新增成功 1.关闭窗体-清空数据 2.重新查询
    10. this.$message({
    11. message: r.data.msg,
    12. type: 'success'
    13. });
    14. this.clearData();
    15. this.search();
    16. }).catch(e => {
    17. });
    18. }).catch(() => {
    19. this.$message({
    20. type: 'info',
    21. message: '已取消删除'
    22. });
    23. });
    24. }

    整个页面代码

    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" width="60">
    14. el-table-column>
    15. <el-table-column sortable prop="id" label="文章ID" width="300">
    16. el-table-column>
    17. <el-table-column sortable prop="title" label="文章标题" width="300">
    18. el-table-column>
    19. <el-table-column sortable prop="body" label="文章内容" 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" :total="formInline.total">
    31. el-pagination>
    32. <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog">
    33. <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
    34. <el-form-item label="文章标题" prop="title">
    35. <el-input size="small" v-model="editForm.title" auto-complete="off" placeholder="请输入文章标题">el-input>
    36. el-form-item>
    37. <el-form-item label="文章内容" prop="body">
    38. <el-input size="small" v-model="editForm.body" auto-complete="off" placeholder="请输入文章内容">el-input>
    39. el-form-item>
    40. el-form>
    41. <div slot="footer" class="dialog-footer">
    42. <el-button size="small" @click="closeDialog">取消el-button>
    43. <el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存el-button>
    44. div>
    45. el-dialog>
    46. div>
    47. template>
    48. <script>
    49. export default {
    50. name: 'Articles',
    51. data() {
    52. return {
    53. title: '',
    54. editFormVisible: false,
    55. editForm: {
    56. title: '',
    57. body: '',
    58. id: 0
    59. },
    60. rules: {
    61. title: [{
    62. required: true,
    63. message: '请输入请输入文章标题',
    64. trigger: 'blur'
    65. },
    66. {
    67. min: 5,
    68. max: 10,
    69. message: '长度在 5 到 10 个字符',
    70. trigger: 'blur'
    71. }
    72. ],
    73. body: [{
    74. required: true,
    75. message: '请输入文章内容',
    76. trigger: 'blur'
    77. }],
    78. },
    79. listData: [],
    80. formInline: {
    81. page: 1,
    82. total: 10,
    83. title: ''
    84. }
    85. }
    86. },
    87. methods: {
    88. handleSizeChange(rows) {
    89. this.formInline.page = 1;
    90. this.formInline.rows = rows;
    91. this.search();
    92. },
    93. handleCurrentChange(page) {
    94. this.formInline.page = page;
    95. this.search();
    96. },
    97. // 为了代码复用
    98. doSearch(parm) {
    99. // 获取树形节点的数据
    100. let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
    101. // alert(url);
    102. // {uname:'zs',pwd:'123'}
    103. this.axios.post(url, parm).then((resp) => {
    104. console.log(resp);
    105. this.listData = resp.data.result;
    106. this.formInline.total = resp.data.pageBean.total;
    107. }).catch(function() {
    108. });
    109. },
    110. search() {
    111. // 按照条件查询
    112. this.doSearch(this.formInline);
    113. },
    114. closeDialog() {
    115. // 关闭窗体
    116. this.clearData();
    117. },
    118. clearData() {
    119. // 清除编辑窗体的缓存数据
    120. this.editForm.title = '';
    121. this.editForm.id = 0;
    122. this.editForm.body = '';
    123. this.title = '';
    124. this.editFormVisible = false;
    125. },
    126. handleEdit(index, row) {
    127. this.clearData();
    128. // 展示新增文章的表单窗体
    129. this.editFormVisible = true;
    130. if (row) {
    131. // 编辑
    132. this.title = '编辑窗体';
    133. this.editForm.id = row.id;
    134. this.editForm.title = row.title;
    135. this.editForm.body = row.body;
    136. } else {
    137. // 新增
    138. this.title = '新增窗体';
    139. }
    140. },
    141. deleteUser(index, row) {
    142. this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    143. confirmButtonText: '确定',
    144. cancelButtonText: '取消',
    145. type: 'warning'
    146. }).then(() => {
    147. let url = this.axios.urls.SYSTEM_ARTICLE_DEL;
    148. this.axios.post(url, {id:row.id}).then(r => {
    149. // 新增成功 1.关闭窗体-清空数据 2.重新查询
    150. this.$message({
    151. message: r.data.msg,
    152. type: 'success'
    153. });
    154. this.clearData();
    155. this.search();
    156. }).catch(e => {
    157. });
    158. }).catch(() => {
    159. this.$message({
    160. type: 'info',
    161. message: '已取消删除'
    162. });
    163. });
    164. },
    165. submitForm(formName) {
    166. this.$refs[formName].validate((valid) => {
    167. if (valid) {
    168. let url;
    169. if (this.editForm.id == 0) {
    170. // 新增
    171. url = this.axios.urls.SYSTEM_ARTICLE_ADD;
    172. } else {
    173. url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
    174. }
    175. this.axios.post(url, this.editForm).then(r => {
    176. // 新增成功 1.关闭窗体-清空数据 2.重新查询
    177. this.$message({
    178. message: r.data.msg,
    179. type: 'success'
    180. });
    181. this.clearData();
    182. this.search();
    183. }).catch(e => {
    184. });
    185. } else {
    186. console.log('error submit!!');
    187. return false;
    188. }
    189. });
    190. }
    191. },
    192. created() {
    193. this.doSearch({});
    194. }
    195. }
    196. script>
    197. <style>
    198. style>

    效果:

  • 相关阅读:
    【微服务】springboot 整合 dubbo3.0
    【MATLAB】利用MATLAB奏乐
    基于py的网络路由实验
    【YAML】【YAML的实践】【YAML的使用学习记录】
    PyQt(学习笔记)
    SpringCloud进阶-Eureka基础知识与搭建单机Eureka
    短视频ai剪辑分发账号矩阵系统(招商oem)----源头技术开发
    最大公约数——Hankson的趣味题(线筛法求质数+gcd+质因数组合搜索约数)
    计及新能源出力不确定性的电气设备综合能源系统协同优化(Matlab代码实现)
    智能手术机器人技术与原理(二)
  • 原文地址:https://blog.csdn.net/m0_62604616/article/details/126839748