1
0

DynamicForm.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div class="m-4">
  3. <div class="mb-4">
  4. <a-button @click="changeLabel3" class="mr-2">更改字段3label</a-button>
  5. <a-button @click="changeLabel34" class="mr-2">同时更改字段3,4label</a-button>
  6. <a-button @click="appendField" class="mr-2">往字段3后面插入字段10</a-button>
  7. <a-button @click="deleteField" class="mr-2">删除字段11</a-button>
  8. </div>
  9. <CollapseContainer title="动态表单示例,动态根据表单内其他值改变">
  10. <BasicForm @register="register" />
  11. </CollapseContainer>
  12. </div>
  13. </template>
  14. <script lang="ts">
  15. import { defineComponent } from 'vue';
  16. import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  17. import { CollapseContainer } from '/@/components/Container/index';
  18. const schemas: FormSchema[] = [
  19. {
  20. field: 'field1',
  21. component: 'Input',
  22. label: '字段1',
  23. colProps: {
  24. span: 8,
  25. },
  26. show: ({ values }) => {
  27. return !!values.field5;
  28. },
  29. },
  30. {
  31. field: 'field2',
  32. component: 'Input',
  33. label: '字段2',
  34. colProps: {
  35. span: 8,
  36. },
  37. ifShow: ({ values }) => {
  38. return !!values.field6;
  39. },
  40. },
  41. {
  42. field: 'field3',
  43. component: 'DatePicker',
  44. label: '字段3',
  45. colProps: {
  46. span: 8,
  47. },
  48. dynamicDisabled: ({ values }) => {
  49. return !!values.field7;
  50. },
  51. },
  52. {
  53. field: 'field4',
  54. component: 'Select',
  55. label: '字段4',
  56. colProps: {
  57. span: 8,
  58. },
  59. dynamicRules: ({ values }) => {
  60. return values.field8 ? [{ required: true, message: '字段4必填' }] : [];
  61. },
  62. componentProps: {
  63. options: [
  64. {
  65. label: '选项1',
  66. value: '1',
  67. key: '1',
  68. },
  69. {
  70. label: '选项2',
  71. value: '2',
  72. key: '2',
  73. },
  74. ],
  75. },
  76. },
  77. {
  78. field: 'field11',
  79. component: 'DatePicker',
  80. label: '字段11',
  81. colProps: {
  82. span: 8,
  83. },
  84. },
  85. {
  86. field: 'field5',
  87. component: 'Switch',
  88. label: '是否显示字段1(css控制)',
  89. colProps: {
  90. span: 8,
  91. },
  92. labelWidth: 200,
  93. },
  94. {
  95. field: 'field6',
  96. component: 'Switch',
  97. label: '是否显示字段2(dom控制)',
  98. colProps: {
  99. span: 8,
  100. },
  101. labelWidth: 200,
  102. },
  103. {
  104. field: 'field7',
  105. component: 'Switch',
  106. label: '是否禁用字段3',
  107. colProps: {
  108. span: 8,
  109. },
  110. labelWidth: 200,
  111. },
  112. {
  113. field: 'field8',
  114. component: 'Switch',
  115. label: '字段4是否必填',
  116. colProps: {
  117. span: 8,
  118. },
  119. labelWidth: 200,
  120. },
  121. ];
  122. export default defineComponent({
  123. components: { BasicForm, CollapseContainer },
  124. setup() {
  125. const [
  126. register,
  127. { setProps, updateSchema, appendSchemaByField, removeSchemaByFiled },
  128. ] = useForm({
  129. labelWidth: 120,
  130. schemas,
  131. actionColOptions: {
  132. span: 24,
  133. },
  134. });
  135. function changeLabel3() {
  136. updateSchema({
  137. field: 'field3',
  138. label: '字段3 New',
  139. });
  140. }
  141. function changeLabel34() {
  142. updateSchema([
  143. {
  144. field: 'field3',
  145. label: '字段3 New++',
  146. },
  147. {
  148. field: 'field4',
  149. label: '字段4 New++',
  150. },
  151. ]);
  152. }
  153. function appendField() {
  154. appendSchemaByField(
  155. {
  156. field: 'field10',
  157. label: '字段10',
  158. component: 'Input',
  159. colProps: {
  160. span: 8,
  161. },
  162. },
  163. 'field3'
  164. );
  165. }
  166. function deleteField() {
  167. removeSchemaByFiled('field11');
  168. }
  169. return {
  170. register,
  171. schemas,
  172. setProps,
  173. changeLabel3,
  174. changeLabel34,
  175. appendField,
  176. deleteField,
  177. };
  178. },
  179. });
  180. </script>