JeecgBoot的前端Form升级为FormModel用法

开发 前端
本篇发给大家关于前端Form升级为FormModel用法的相关知识,希望对你有所帮助!

[[385150]]

 FormModel 表单 (支持 v-model 绑定)(Ant Design of Vue 版本:1.5.0+)

新版form特性

1、支持双向绑定

2、无需v-decorator这个反人类属性设置

3、form用法跟目前主流框架(element ui iview风格形成统一,不在另类)

4、表单赋值无需手动设置,双向绑定自动赋值

新老form用法对比

标签规则升级

校验规则区别

表单赋值区别

表单提交区别

角色管理老版form代码

  1. ~~~ 
  2. <template> 
  3.   <a-modal 
  4.     :title="title" 
  5.     :width="800" 
  6.     :visible="visible" 
  7.     :confirmLoading="confirmLoading" 
  8.     @ok="handleOk" 
  9.     @cancel="handleCancel" 
  10.     cancelText="关闭" 
  11.     wrapClassName="ant-modal-cust-warp" 
  12.     style="top:5%;height: 85%;overflow-y: hidden"
  13.  
  14.     <a-spin :spinning="confirmLoading"
  15.       <a-form :form="form"
  16.  
  17.         <a-form-item 
  18.           :labelCol="labelCol" 
  19.           :wrapperCol="wrapperCol" 
  20.           label="角色名称"
  21.           <a-input placeholder="请输入角色名称" v-decorator="[ 'roleName', validatorRules.roleName]" /> 
  22.         </a-form-item> 
  23.  
  24.         <a-form-item 
  25.           :labelCol="labelCol" 
  26.           :wrapperCol="wrapperCol" 
  27.           label="角色编码"
  28.           <a-input placeholder="请输入角色编码" :disabled="roleDisabled" v-decorator="[ 'roleCode', validatorRules.roleCode]" /> 
  29.         </a-form-item> 
  30.  
  31.         <a-form-item 
  32.           :labelCol="labelCol" 
  33.           :wrapperCol="wrapperCol" 
  34.           label="描述"
  35.           <a-textarea :rows="5" placeholder="..." v-decorator="[ 'description', validatorRules.description ]" /> 
  36.         </a-form-item> 
  37.  
  38.       </a-form> 
  39.     </a-spin> 
  40.   </a-modal> 
  41. </template> 
  42.  
  43. <script> 
  44.   import pick from 'lodash.pick' 
  45.   import {addRole,editRole,duplicateCheck } from '@/api/api' 
  46.  
  47.   export default { 
  48.     name"RoleModal"
  49.     data () { 
  50.       return { 
  51.         title:"操作"
  52.         visible: false
  53.         roleDisabled: false
  54.         model: {}, 
  55.         labelCol: { 
  56.           xs: { span: 24 }, 
  57.           sm: { span: 5 }, 
  58.         }, 
  59.         wrapperCol: { 
  60.           xs: { span: 24 }, 
  61.           sm: { span: 16 }, 
  62.         }, 
  63.         confirmLoading: false
  64.         form: this.$form.createForm(this), 
  65.         validatorRules:{ 
  66.           roleName:{ 
  67.             rules: [ 
  68.               { required: true, message: '请输入角色名称!' }, 
  69.               { min: 2, max: 30, message: '长度在 2 到 30 个字符'trigger'blur' } 
  70.             ]}, 
  71.           roleCode:{ 
  72.             rules: [ 
  73.               { required: true, message: '请输入角色名称!'}, 
  74.               { min: 0, max: 64, message: '长度不超过 64 个字符'trigger'blur' }, 
  75.               { validator: this.validateRoleCode} 
  76.             ]}, 
  77.           description:{ 
  78.             rules: [ 
  79.               { min: 0, max: 126, message: '长度不超过 126 个字符'trigger'blur' } 
  80.             ]} 
  81.         }, 
  82.       } 
  83.     }, 
  84.     created () { 
  85.     }, 
  86.     methods: { 
  87.       add () { 
  88.         this.edit({}); 
  89.       }, 
  90.       edit (record) { 
  91.         this.form.resetFields(); 
  92.         this.model = Object.assign({}, record); 
  93.         this.visible = true
  94.  
  95.         //编辑页面禁止修改角色编码 
  96.         if(this.model.id){ 
  97.           this.roleDisabled = true
  98.         }else
  99.           this.roleDisabled = false
  100.         } 
  101.         this.$nextTick(() => { 
  102.           this.form.setFieldsValue(pick(this.model,'roleName''description','roleCode')) 
  103.         }); 
  104.  
  105.       }, 
  106.       close () { 
  107.         this.$emit('close'); 
  108.         this.visible = false
  109.       }, 
  110.       handleOk () { 
  111.         const that = this; 
  112.         // 触发表单验证 
  113.         this.form.validateFields((err, values) => { 
  114.           if (!err) { 
  115.             that.confirmLoading = true
  116.             values.roleName = (values.roleName || '').trim() 
  117.             values.roleCode = (values.roleCode || '').trim() 
  118.             let formData = Object.assign(this.model, values); 
  119.             let obj; 
  120.             console.log(formData) 
  121.             if(!this.model.id){ 
  122.               obj=addRole(formData); 
  123.             }else
  124.               obj=editRole(formData); 
  125.             } 
  126.             obj.then((res)=>{ 
  127.               if(res.success){ 
  128.                 that.$message.success(res.message); 
  129.                 that.$emit('ok'); 
  130.               }else
  131.                 that.$message.warning(res.message); 
  132.               } 
  133.             }).finally(() => { 
  134.               that.confirmLoading = false
  135.               that.close(); 
  136.             }) 
  137.           } 
  138.         }) 
  139.       }, 
  140.       handleCancel () { 
  141.         this.close() 
  142.       }, 
  143.       validateRoleCode(rule, value, callback){ 
  144.         if(/[\u4E00-\u9FA5]/g.test(value)){ 
  145.           callback("角色编码不可输入汉字!"); 
  146.         }else
  147.           var params = { 
  148.             tableName: "sys_role"
  149.             fieldName: "role_code"
  150.             fieldVal: value, 
  151.             dataId: this.model.id, 
  152.           }; 
  153.           duplicateCheck(params).then((res)=>{ 
  154.             if(res.success){ 
  155.               callback(); 
  156.             }else
  157.               callback(res.message); 
  158.             } 
  159.           }); 
  160.         } 
  161.       } 
  162.  
  163.     } 
  164.   } 
  165. </script> 
  166.  
  167. <style scoped> 
  168.  
  169. </style> 
  170. ~~~ 

角色管理新版from代码

  1. ~~~ 
  2. <template> 
  3.   <a-modal 
  4.     :title="title" 
  5.     :width="800" 
  6.     :visible="visible" 
  7.     :confirmLoading="confirmLoading" 
  8.     @ok="handleOk" 
  9.     @cancel="handleCancel" 
  10.     cancelText="关闭" 
  11.     wrapClassName="ant-modal-cust-warp" 
  12.     style="top:5%;height: 85%;overflow-y: hidden"
  13.  
  14.     <a-spin :spinning="confirmLoading"
  15.       <a-form-model ref="form"  v-bind="layout"  :model="model" :rules="validatorRules"
  16.         <a-form-model-item label="角色编码" required prop="roleCode"
  17.           <a-input v-model="model.roleCode" :disabled="roleDisabled"  placeholder="请输入角色编码"/> 
  18.         </a-form-model-item> 
  19.         <a-form-model-item label="角色名称" required prop="roleName"
  20.           <a-input v-model="model.roleName" placeholder="请输入角色名称"/> 
  21.         </a-form-model-item> 
  22.         <a-form-model-item label="描述" prop="description"
  23.           <a-textarea :rows="5" v-model="model.description" placeholder="请输入角色描述"/> 
  24.         </a-form-model-item> 
  25.       </a-form-model> 
  26.     </a-spin> 
  27.   </a-modal> 
  28. </template> 
  29.  
  30. <script> 
  31.   import {addRole,editRole,duplicateCheck } from '@/api/api' 
  32.   export default { 
  33.     name"RoleModal"
  34.     data () { 
  35.       return { 
  36.         title:"操作"
  37.         visible: false
  38.         roleDisabled: false
  39.         model: {}, 
  40.         layout: { 
  41.           labelCol: { span: 3 }, 
  42.           wrapperCol: { span: 14 }, 
  43.         }, 
  44.         confirmLoading: false
  45.         validatorRules:{ 
  46.           roleName: [ 
  47.             { required: true, message: '请输入角色名称!' }, 
  48.             { min: 2, max: 30, message: '长度在 2 到 30 个字符'trigger'blur' } 
  49.           ], 
  50.           roleCode: [ 
  51.             { required: true, message: '请输入角色名称!'}, 
  52.             { min: 0, max: 64, message: '长度不超过 64 个字符'trigger'blur' }, 
  53.             { validator: this.validateRoleCode} 
  54.           ], 
  55.           description: [ 
  56.             { min: 0, max: 126, message: '长度不超过 126 个字符'trigger'blur' } 
  57.           ] 
  58.         }, 
  59.       } 
  60.     }, 
  61.     created () { 
  62.     }, 
  63.     methods: { 
  64.       add () { 
  65.         this.edit({}); 
  66.       }, 
  67.       edit (record) { 
  68.         this.model = Object.assign({}, record); 
  69.         this.visible = true
  70.         //编辑页面禁止修改角色编码 
  71.         if(this.model.id){ 
  72.           this.roleDisabled = true
  73.         }else
  74.           this.roleDisabled = false
  75.         } 
  76.       }, 
  77.       close () { 
  78.         this.$emit('close'); 
  79.         this.visible = false
  80.       }, 
  81.       handleOk () { 
  82.         const that = this; 
  83.         // 触发表单验证 
  84.         this.$refs.form.validate(valid => { 
  85.           if (valid) { 
  86.             that.confirmLoading = true
  87.             let obj; 
  88.             if(!this.model.id){ 
  89.               obj=addRole(this.model); 
  90.             }else
  91.               obj=editRole(this.model); 
  92.             } 
  93.             obj.then((res)=>{ 
  94.               if(res.success){ 
  95.                 that.$message.success(res.message); 
  96.                 that.$emit('ok'); 
  97.               }else
  98.                 that.$message.warning(res.message); 
  99.               } 
  100.             }).finally(() => { 
  101.               that.confirmLoading = false
  102.               that.close(); 
  103.             }) 
  104.           }else
  105.             return false
  106.           } 
  107.         }) 
  108.       }, 
  109.       handleCancel () { 
  110.         this.close() 
  111.       }, 
  112.       validateRoleCode(rule, value, callback){ 
  113.         if(/[\u4E00-\u9FA5]/g.test(value)){ 
  114.           callback("角色编码不可输入汉字!"); 
  115.         }else
  116.           let params = { 
  117.             tableName: "sys_role"
  118.             fieldName: "role_code"
  119.             fieldVal: value, 
  120.             dataId: this.model.id, 
  121.           }; 
  122.           duplicateCheck(params).then((res)=>{ 
  123.             if(res.success){ 
  124.               callback(); 
  125.             }else
  126.               callback(res.message); 
  127.             } 
  128.           }); 
  129.         } 
  130.       } 
  131.     } 
  132.   } 
  133. </script> 
  134.  
  135. <style scoped> 
  136.  
  137. </style> 
  138. ~~~ 

 【编辑推荐】

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-10-07 09:03:44

Uniapp封装组件

2021-08-18 11:30:46

TCPWebSocketLinux

2022-07-27 08:27:34

Call前端

2021-05-21 15:56:28

勒索软件攻击数据泄露

2019-06-20 10:23:23

架构代码前端

2011-04-07 13:28:58

AccessSQL Server

2015-07-03 17:31:19

Windows 10微软

2017-10-20 12:18:02

HTML5代码header

2020-12-24 07:40:05

JeecgBoot手机端HBuilder

2023-04-28 07:41:38

Unity前端架构

2019-01-21 15:52:02

前端工程师Flutter代码

2024-09-30 09:25:29

2009-09-17 16:21:57

WSUS服务器

2009-07-01 08:36:31

微软SonyWindows 7

2017-06-30 14:54:10

智慧城市互联网+政务

2017-04-13 12:37:17

智能制造石油和化工智慧云

2021-03-10 09:38:44

JeecgBoot 单表数据sheet实例

2023-05-31 07:29:46

2018-01-23 10:16:36

前端干货Web

2019-10-28 10:43:12

戴尔
点赞
收藏

51CTO技术栈公众号