index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div class="form-com">
  3. <el-form ref="form" :rules="rules" :model="form" :label-width="configs.labelWidth||'80px'" :label-position="configs.labelPosition||'right'">
  4. <el-collapse v-if="isGroup" v-model="activeNames">
  5. <el-collapse-item v-for="(item, i) in configs.fields" :key="i" :title="item.groupTitle" :name="i">
  6. <Form :form.sync="form" :fields.sync="item.children" :columnNum="configs.columnNum">
  7. <template v-for="(item, i) in $scopedSlots" :slot="i" slot-scope="scope">
  8. <!-- <template v-if="hasSlot(item.children, i)" > -->
  9. <slot :name="i" :scope="scope"/>
  10. <!-- </template> -->
  11. </template>
  12. </Form>
  13. </el-collapse-item>
  14. </el-collapse>
  15. <template v-else>
  16. <Form :form.sync="form" :fields.sync="configs.fields" :columnNum="configs.columnNum">
  17. <template v-for="(item, i) in $scopedSlots" :slot="i" slot-scope="scope">
  18. <!-- <template v-if="hasSlot(item.children, i)" > -->
  19. <slot :name="i" :scope="scope"/>
  20. <!-- </template> -->
  21. </template>
  22. </Form>
  23. </template>
  24. </el-form>
  25. <div class="pt50 tc">
  26. <slot name="footer"></slot>
  27. </div>
  28. <!-- <div class="tc pt50">
  29. <el-button >取消</el-button>
  30. <el-button type="primary" @click="submit()">保存</el-button>
  31. </div> -->
  32. </div>
  33. </template>
  34. <script>
  35. import Form from './form'
  36. import {isIdcard} from '../../utils/validate'
  37. import validateForm from '../../utils/validateForm'
  38. var validateMobile = (rule, value, callback) => {
  39. if(!(/^1[3456789]\d{9}$/.test(value))){
  40. return callback(new Error('号码有误,请重填'));
  41. }
  42. return callback()
  43. };
  44. var validateIdCard = (rule, value, callback) => {
  45. if(!value){
  46. return callback();
  47. }
  48. var idcard = value
  49. var idcardLen = idcard.length,
  50. xchar = idcard.charAt(idcardLen-1);
  51. if (xchar === 'x') {
  52. idcard = idcard.substr(0, idcardLen-1) + 'X';
  53. }
  54. if(!isIdcard(idcard)){
  55. return callback(new Error('号码有误,请重填'));
  56. }
  57. return callback()
  58. };
  59. export default {
  60. name: 'CustomForm',
  61. props: {
  62. 'configs':{},
  63. },
  64. components:{
  65. Form
  66. },
  67. computed:{
  68. isGroup(){
  69. return !!this.configs.fields[0].groupTitle
  70. }
  71. },
  72. watch:{
  73. form:{
  74. handler(n){
  75. this.$emit('input', this.form)
  76. },
  77. deep: true
  78. }
  79. },
  80. data(){
  81. return {
  82. activeNames: [],
  83. form: "",
  84. rules: ""
  85. }
  86. },
  87. created(){
  88. if(this.isGroup){
  89. var activeNames = []
  90. this.configs.fields.forEach((v, i) => {
  91. activeNames.push(i)
  92. });
  93. this.activeNames = activeNames
  94. }
  95. var form = {}
  96. var rules = {}
  97. this.configs.fields.forEach((v, i) => {
  98. if(v.groupTitle){
  99. v.children.forEach((m, n)=>{
  100. form[m.id] = m.value||m.value===0||m.value===false? m.value : m.type=='checkbox'? [] :""
  101. rules[m.id] = this.setRule(m)
  102. })
  103. } else {
  104. form[v.id] = v.value||v.value===0||v.value===false? v.value : v.type=='checkbox'? [] :""
  105. rules[v.id] = this.setRule(v)
  106. }
  107. });
  108. this.form = form
  109. this.rules = rules
  110. console.log(this.rules)
  111. },
  112. mounted() {
  113. },
  114. methods: {
  115. resetRule(item){
  116. this.rules[item.id] = this.setRule(item)
  117. },
  118. resetRules(){
  119. var rules = {}
  120. this.configs.fields.forEach((v, i) => {
  121. if(v.groupTitle){
  122. v.children.forEach((m, n)=>{
  123. rules[m.id] = this.setRule(m)
  124. })
  125. } else {
  126. rules[v.id] = this.setRule(v)
  127. }
  128. });
  129. this.rules = rules
  130. },
  131. setRule(item){
  132. if(item.rules){
  133. return item.rules
  134. }
  135. var r = []
  136. if(item.required){
  137. r.push({
  138. required: true,
  139. message: "该项必填",
  140. trigger: "blur"
  141. })
  142. }
  143. if(item.regTel){
  144. //手机号码
  145. r.push({
  146. validator: validateMobile,
  147. trigger: 'blur'
  148. })
  149. }
  150. if(item.regIdcard){
  151. //身份证
  152. r.push({
  153. validator: validateIdCard,
  154. trigger: 'blur'
  155. })
  156. }
  157. if(item.type == 'digit'){
  158. r.push({
  159. validator: validateForm['digit'],
  160. trigger: 'blur'
  161. })
  162. }
  163. if(item.regularType && validateForm[item.regularType]){
  164. r.push({
  165. validator: validateForm[item.regularType],
  166. trigger: 'blur'
  167. })
  168. }
  169. return r
  170. },
  171. submit() {
  172. var vm = this;
  173. return new Promise((resolve, reject)=>{
  174. this.$refs['form'].validate(valid => {
  175. if(valid){
  176. resolve(this.form)
  177. } else {
  178. resolve(valid)
  179. }
  180. });
  181. })
  182. },
  183. setValues(model){
  184. for(var k in model){
  185. this.form[k] = model[k]
  186. }
  187. },
  188. setValue(id, value){
  189. this.form[id] = value
  190. },
  191. hasSlot(list, key){
  192. for(var i=0; i<list.length; i++){
  193. if(list[i].id == key){
  194. return true
  195. }
  196. }
  197. return false
  198. },
  199. setOptions(configs, key, options){
  200. configs.fields.forEach(v=>{
  201. if(v.children){
  202. v.children.forEach(m=>{
  203. if(m.id == key){
  204. m.optionList = options
  205. }
  206. })
  207. } else {
  208. if(v.id == key){
  209. v.optionList = options
  210. }
  211. }
  212. })
  213. }
  214. }
  215. }
  216. </script>
  217. <style lang="scss">
  218. .form-com{
  219. .el-collapse-item__header{
  220. font-size: 16px;
  221. }
  222. .el-collapse-item__content{
  223. padding-bottom: 0;
  224. padding-top: 10px;
  225. }
  226. }
  227. </style>