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


    目录

    一、表单验证

    1.出现表单组件el-form 

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

     3.给表单设置规则  rules

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

     二、增删改功能实现

    1.保障编辑窗体的正常弹出 

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


    一、表单验证

    1.出现表单组件 el-form

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

    3.给表单设置规则  rules

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

    1.出现表单组件el-form 

    编辑界面

    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>

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

     

    测试一下:

     3.给表单设置规则  rules

    最终改成这样: 

     

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

     

    最终运行结果测试如图所示:

    只要这样才是可以的如图所示: 

     二、增删改功能实现

    1.保障编辑窗体的正常弹出

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

    1.保障编辑窗体的正常弹出 

    测试一下如图所示:

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

    去elementUI中去找一个提示款,要不然新增或者编辑都不知道成功没有成功

    删除的提示框:

    Article.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. id:0
    60. },
    61. rules: {
    62. title: [
    63. {required: true, message: '请输入文章标题', trigger: 'blur'},
    64. {min: 5, max: 10, message: '长度须在 5 到 10 个字符之间', trigger: 'blur'}
    65. ],
    66. body: [
    67. {required: true, message: '请输入文章内容', trigger: 'blur'}
    68. ],
    69. },
    70. listData: [],
    71. formInline: {
    72. page: 1,
    73. total: 10,
    74. title: ''
    75. }
    76. }
    77. },
    78. methods: {
    79. handleSizeChange(rows) {
    80. // console.log("页面发生改变")
    81. this.formInline.page = 1;
    82. this.formInline.rows = rows;
    83. this.search();
    84. },
    85. handleCurrentChange(page) {
    86. // console.log("当前页码发生改变")
    87. this.formInline.page = page;
    88. this.search();
    89. },
    90. //是为了代码复用
    91. doSearch(param) {
    92. // 获取树形节点的数据
    93. let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
    94. //this指的是vue实例
    95. this.axios.post(url, param)
    96. .then(resp => {//代表成功 箭头函数 jdk8的语法
    97. console.log(resp);
    98. this.listData = resp.data.result;
    99. this.formInline.total = resp.data.pageBean.total;
    100. }).catch(function () {//代表失败
    101. });
    102. },
    103. search() {
    104. // 按照条件进行查询
    105. this.doSearch(this.formInline);
    106. },
    107. closeDialog() {
    108. // 关闭窗体
    109. this.clearData();
    110. },
    111. clearData(){
    112. // 清楚编辑窗体的缓存数据
    113. this.editForm.title='';
    114. this.editForm.id=0;
    115. this.editForm.body='';
    116. this.title='';
    117. this.editFormVisible=false;
    118. },
    119. handleEdit(index,row) {
    120. this.clearData();
    121. // 展示新增文章的表单
    122. this.editFormVisible = true;
    123. if(row){
    124. // 编辑
    125. this.title='编辑窗体';
    126. this.editForm.id = row.id;
    127. this.editForm.title = row.title;
    128. this.editForm.body=row.body
    129. }else{
    130. //新增
    131. this.title='新增窗体';
    132. }
    133. },
    134. deleteUser(index,row){
    135. this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
    136. confirmButtonText: '确定',
    137. cancelButtonText: '取消',
    138. type: 'warning'
    139. }).then(() => {
    140. let url =this.axios.urls.SYSTEM_ARTICLE_DEL;
    141. this.axios.post(url, {id:row.id}).then(r=>{
    142. // 新增成功之后,1、关闭窗体 ,清空数据 2、重新查询
    143. this.$message({
    144. message: r.data.msg,
    145. type: 'success'
    146. });
    147. this.closeDialog();
    148. this.search();
    149. }).catch(e=>{
    150. })
    151. }).catch(() => {
    152. this.$message({
    153. type: 'info',
    154. message: '已取消删除'
    155. });
    156. });
    157. },
    158. submitForm(formName) {
    159. this.$refs[formName].validate((valid) => {
    160. if (valid) {
    161. let url ;
    162. if(this.editForm.id==0){
    163. //新增
    164. url=this.axios.urls.SYSTEM_ARTICLE_ADD;
    165. }else {
    166. url=this.axios.urls.SYSTEM_ARTICLE_EDIT;
    167. }
    168. this.axios.post(url,this.editForm).then(r=>{
    169. // 新增成功之后,1、关闭窗体 ,清空数据 2、重新查询
    170. this.$message({
    171. message: r.data.msg,
    172. type: 'success'
    173. });
    174. this.closeDialog();
    175. this.search();
    176. }).catch(e=>{
    177. })
    178. } else {
    179. console.log('error submit!!');
    180. return false;
    181. }
    182. });
    183. }
    184. },
    185. created() {
    186. this.doSearch({});
    187. }
    188. }
    189. script>
    190. <style scoped>
    191. style>

     最终运行的CRUD的方法如图所示:

    新增效果: 

    编辑效果:

    删除效果:



  • 相关阅读:
    日语等级J.TEST的自测卷
    后置处理 Bean
    vue上传文件
    Python递归的几个经典案例
    冒泡排序详解
    ACWING/4262. 空调
    【融合ChatGPT等AI模型】Python-GEE遥感云大数据分析、管理与可视化及多领域案例实践应用
    使用boost::geometry::partition的示例程序(C/C++)
    myj的尘歌壶
    Logstash对接 SNMP v2和 v3
  • 原文地址:https://blog.csdn.net/weixin_67465673/article/details/126855791