Index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <div class="test-form">
  3. <parser :form-conf="formConf" @submit="sumbitForm1" />
  4. <parser :key="key2" :form-conf="formConf" @submit="sumbitForm2" />
  5. <el-button @click="change">
  6. change
  7. </el-button>
  8. </div>
  9. </template>
  10. <script>
  11. import Parser from '../Parser'
  12. // 若parser是通过安装npm方式集成到项目中的,使用此行引入
  13. // import Parser from 'form-gen-parser'
  14. export default {
  15. components: {
  16. Parser
  17. },
  18. props: {},
  19. data() {
  20. return {
  21. key2: +new Date(),
  22. formConf: {
  23. fields: [
  24. {
  25. __config__: {
  26. label: '单行文本',
  27. labelWidth: null,
  28. showLabel: true,
  29. changeTag: true,
  30. tag: 'el-input',
  31. tagIcon: 'input',
  32. required: true,
  33. layout: 'colFormItem',
  34. span: 24,
  35. document: 'https://element.eleme.cn/#/zh-CN/component/input',
  36. regList: [
  37. {
  38. pattern: '/^1(3|4|5|7|8|9)\\d{9}$/',
  39. message: '手机号格式错误'
  40. }
  41. ]
  42. },
  43. __slot__: {
  44. prepend: '',
  45. append: ''
  46. },
  47. __vModel__: 'mobile',
  48. placeholder: '请输入手机号',
  49. style: {
  50. width: '100%'
  51. },
  52. clearable: true,
  53. 'prefix-icon': 'el-icon-mobile',
  54. 'suffix-icon': '',
  55. maxlength: 11,
  56. 'show-word-limit': true,
  57. readonly: false,
  58. disabled: false
  59. },
  60. {
  61. __config__: {
  62. label: '日期范围',
  63. tag: 'el-date-picker',
  64. tagIcon: 'date-range',
  65. defaultValue: null,
  66. span: 24,
  67. showLabel: true,
  68. labelWidth: null,
  69. required: true,
  70. layout: 'colFormItem',
  71. regList: [],
  72. changeTag: true,
  73. document:
  74. 'https://element.eleme.cn/#/zh-CN/component/date-picker',
  75. formId: 101,
  76. renderKey: 1585980082729
  77. },
  78. style: {
  79. width: '100%'
  80. },
  81. type: 'daterange',
  82. 'range-separator': '至',
  83. 'start-placeholder': '开始日期',
  84. 'end-placeholder': '结束日期',
  85. disabled: false,
  86. clearable: true,
  87. format: 'yyyy-MM-dd',
  88. 'value-format': 'yyyy-MM-dd',
  89. readonly: false,
  90. __vModel__: 'field101'
  91. },
  92. {
  93. __config__: {
  94. layout: 'rowFormItem',
  95. tagIcon: 'row',
  96. label: '行容器',
  97. layoutTree: true,
  98. children: [
  99. {
  100. __config__: {
  101. label: '评分',
  102. tag: 'el-rate',
  103. tagIcon: 'rate',
  104. defaultValue: 0,
  105. span: 24,
  106. showLabel: true,
  107. labelWidth: null,
  108. layout: 'colFormItem',
  109. required: true,
  110. regList: [],
  111. changeTag: true,
  112. document: 'https://element.eleme.cn/#/zh-CN/component/rate',
  113. formId: 102,
  114. renderKey: 1586839671259
  115. },
  116. style: {},
  117. max: 5,
  118. 'allow-half': false,
  119. 'show-text': false,
  120. 'show-score': false,
  121. disabled: false,
  122. __vModel__: 'field102'
  123. }
  124. ],
  125. document: 'https://element.eleme.cn/#/zh-CN/component/layout',
  126. formId: 101,
  127. span: 24,
  128. renderKey: 1586839668999,
  129. componentName: 'row101',
  130. gutter: 15
  131. },
  132. type: 'default',
  133. justify: 'start',
  134. align: 'top'
  135. },
  136. {
  137. __config__: {
  138. label: '按钮',
  139. showLabel: true,
  140. changeTag: true,
  141. labelWidth: null,
  142. tag: 'el-button',
  143. tagIcon: 'button',
  144. span: 24,
  145. layout: 'colFormItem',
  146. document: 'https://element.eleme.cn/#/zh-CN/component/button',
  147. renderKey: 1594288459289
  148. },
  149. __slot__: {
  150. default: '测试按钮1'
  151. },
  152. type: 'primary',
  153. icon: 'el-icon-search',
  154. round: false,
  155. size: 'medium',
  156. plain: false,
  157. circle: false,
  158. disabled: false,
  159. on: {
  160. click: 'clickTestButton1'
  161. }
  162. }
  163. ],
  164. __methods__: {
  165. clickTestButton1() {
  166. console.log(
  167. `%c【测试按钮1】点击事件里可以访问当前表单:
  168. 1) formModel='formData', 所以this.formData可以拿到当前表单的model
  169. 2) formRef='elForm', 所以this.$refs.elForm可以拿到当前表单的ref(vue组件)
  170. `,
  171. 'color:#409EFF;font-size: 15px'
  172. )
  173. console.log('表单的Model:', this.formData)
  174. console.log('表单的ref:', this.$refs.elForm)
  175. }
  176. },
  177. formRef: 'elForm',
  178. formModel: 'formData',
  179. size: 'small',
  180. labelPosition: 'right',
  181. labelWidth: 100,
  182. formRules: 'rules',
  183. gutter: 15,
  184. disabled: false,
  185. span: 24,
  186. formBtns: true,
  187. unFocusedComponentBorder: false
  188. },
  189. formConf2: {
  190. fields: [
  191. {
  192. __config__: {
  193. label: '单行文本',
  194. labelWidth: null,
  195. showLabel: true,
  196. changeTag: true,
  197. tag: 'el-input',
  198. tagIcon: 'input',
  199. required: true,
  200. layout: 'colFormItem',
  201. span: 24,
  202. document: 'https://element.eleme.cn/#/zh-CN/component/input',
  203. regList: [
  204. {
  205. pattern: '/^1(3|4|5|7|8|9)\\d{9}$/',
  206. message: '手机号格式错误'
  207. }
  208. ]
  209. },
  210. __slot__: {
  211. prepend: '',
  212. append: ''
  213. },
  214. __vModel__: 'mobile',
  215. placeholder: '请输入手机号',
  216. style: {
  217. width: '100%'
  218. },
  219. clearable: true,
  220. 'prefix-icon': 'el-icon-mobile',
  221. 'suffix-icon': '',
  222. maxlength: 11,
  223. 'show-word-limit': true,
  224. readonly: false,
  225. disabled: false
  226. },
  227. {
  228. __config__: {
  229. label: '日期范围',
  230. tag: 'el-date-picker',
  231. tagIcon: 'date-range',
  232. defaultValue: null,
  233. span: 24,
  234. showLabel: true,
  235. labelWidth: null,
  236. required: true,
  237. layout: 'colFormItem',
  238. regList: [],
  239. changeTag: true,
  240. document:
  241. 'https://element.eleme.cn/#/zh-CN/component/date-picker',
  242. formId: 101,
  243. renderKey: 1585980082729
  244. },
  245. style: {
  246. width: '100%'
  247. },
  248. type: 'daterange',
  249. 'range-separator': '至',
  250. 'start-placeholder': '开始日期',
  251. 'end-placeholder': '结束日期',
  252. disabled: false,
  253. clearable: true,
  254. format: 'yyyy-MM-dd',
  255. 'value-format': 'yyyy-MM-dd',
  256. readonly: false,
  257. __vModel__: 'field101'
  258. }
  259. ],
  260. formRef: 'elForm',
  261. formModel: 'formData',
  262. size: 'small',
  263. labelPosition: 'right',
  264. labelWidth: 100,
  265. formRules: 'rules',
  266. gutter: 15,
  267. disabled: false,
  268. span: 24,
  269. formBtns: true,
  270. unFocusedComponentBorder: false
  271. }
  272. }
  273. },
  274. computed: {},
  275. watch: {},
  276. created() {},
  277. mounted() {
  278. // 表单数据回填,模拟异步请求场景
  279. setTimeout(() => {
  280. // 请求回来的表单数据
  281. const data = {
  282. mobile: '18836662555'
  283. }
  284. // 回填数据
  285. this.fillFormData(this.formConf, data)
  286. // 更新表单
  287. this.key2 = +new Date()
  288. }, 2000)
  289. },
  290. methods: {
  291. fillFormData(form, data) {
  292. form.fields.forEach(item => {
  293. const val = data[item.__vModel__]
  294. if (val) {
  295. item.__config__.defaultValue = val
  296. }
  297. })
  298. },
  299. change() {
  300. this.key2 = +new Date()
  301. const t = this.formConf
  302. this.formConf = this.formConf2
  303. this.formConf2 = t
  304. },
  305. sumbitForm1(data) {
  306. console.log('sumbitForm1提交数据:', data)
  307. },
  308. sumbitForm2(data) {
  309. console.log('sumbitForm2提交数据:', data)
  310. }
  311. }
  312. }
  313. </script>
  314. <style lang="scss" scoped>
  315. .test-form {
  316. margin: 15px auto;
  317. width: 800px;
  318. padding: 15px;
  319. }
  320. </style>