MyForm.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div>
  3. <el-form
  4. ref="formData"
  5. :label-position="labelPosition"
  6. class="form-box"
  7. :rules="rules"
  8. :inline="inline"
  9. :label-width="labelWidth"
  10. :model="formValue"
  11. >
  12. <template v-for="item in formList">
  13. <el-form-item :label="item.label" :prop="item.prop">
  14. <el-input
  15. v-if="item.type === 'input'"
  16. v-model.trim="formValue[item.prop]"
  17. :style="{ width: `${item.width}` }"
  18. :placeholder="item.placeholder"
  19. :type="item.style"
  20. :show-password="item.style == 'password' ? true : false"
  21. clearable
  22. ></el-input>
  23. <el-select
  24. v-if="item.type === 'select'"
  25. v-model="formValue[item.prop]"
  26. :placeholder="item.placeholder"
  27. >
  28. <el-option
  29. v-for="(item1, index1) in item.child"
  30. :key="index1"
  31. :label="item1.label"
  32. :value="item1.value"
  33. ></el-option>
  34. </el-select>
  35. <!-- 单选 -->
  36. <el-radio-group
  37. v-if="item.type === 'Radio'"
  38. v-model="formValue[item.prop]"
  39. >
  40. <el-radio
  41. v-for="ra in item.radios"
  42. :label="ra.value"
  43. :key="ra.value"
  44. >{{ ra.label }}</el-radio
  45. >
  46. </el-radio-group>
  47. <!-- 单选按钮 -->
  48. <el-radio-group
  49. v-if="item.type === 'RadioButton'"
  50. v-model="formValue[item.prop]"
  51. @change="item.change && item.change(formValue[item.prop])"
  52. >
  53. <el-radio-button
  54. v-for="ra in item.radios"
  55. :label="ra.value"
  56. :key="ra.value"
  57. >{{ ra.label }}</el-radio-button
  58. >
  59. </el-radio-group>
  60. <!-- 复选框 -->
  61. <el-checkbox-group
  62. v-if="item.type === 'Checkbox'"
  63. v-model="formValue[item.prop]"
  64. >
  65. <el-checkbox
  66. v-for="ch in item.checkboxs"
  67. :label="ch.value"
  68. :key="ch.value"
  69. >{{ ch.label }}</el-checkbox
  70. >
  71. </el-checkbox-group>
  72. <span v-if="item.type === 'text'">{{ item.value }}</span>
  73. <quill-editor
  74. v-if="item.type === 'richText'"
  75. id="quill-editor"
  76. v-model="formValue[item.prop]"
  77. >
  78. </quill-editor>
  79. </el-form-item>
  80. </template>
  81. <slot name="handle" :formData="formValue">
  82. <!-- <el-button type="primary" @click="submitForm">提交</el-button> -->
  83. </slot>
  84. </el-form>
  85. </div>
  86. </template>
  87. <script>
  88. export default {
  89. name: "my-form",
  90. props: {
  91. formList: {
  92. type: Array,
  93. default: function () {
  94. return [];
  95. },
  96. },
  97. formValue: {
  98. type: Object,
  99. default: function () {
  100. return {};
  101. },
  102. },
  103. rules: {
  104. type: Object,
  105. default: function () {
  106. return {};
  107. },
  108. },
  109. inline: {
  110. type: Boolean,
  111. default: function () {
  112. return false;
  113. },
  114. },
  115. labelPosition: {
  116. type: String,
  117. default: "right",
  118. },
  119. labelWidth: {
  120. type: String,
  121. default: "",
  122. },
  123. },
  124. data() {
  125. return {
  126. form: {
  127. name: "",
  128. region: "",
  129. date1: "",
  130. date2: "",
  131. delivery: false,
  132. type: [],
  133. resource: "",
  134. desc: "",
  135. },
  136. };
  137. },
  138. methods: {
  139. submitForm() {
  140. this.$refs.formData.validate((valid) => {
  141. if (valid) {
  142. // this.$emit("submitForm", this.formValue);
  143. } else {
  144. return false;
  145. }
  146. });
  147. },
  148. },
  149. mounted() {
  150. //console.log(this.formList, "sss");
  151. },
  152. };
  153. </script>
  154. <style lang="scss" scoped>
  155. // .form-box {
  156. // margin-top: 30px;
  157. // }
  158. .form-box ::v-deep .ql-container {
  159. height: auto;
  160. }
  161. </style>