华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
6.5 KiB

2 weeks ago
  1. <template>
  2. <div>
  3. <div v-for="(item, index) in formConfig" :key="index">
  4. <template v-if="item.type === 'cardItem'">
  5. <div class="grid-container">
  6. <div v-for="(sItem, sIndex) in item.config" class="form-item" :class="sItem.span == 1 ? 'full-row' : ''"
  7. :key="sIndex">
  8. <template v-if="sItem.type === 'input'">
  9. <div class="form-title">{{ sItem.label }}</div>
  10. <el-input :maxlength="sItem.maxlength || 30" :disabled="sItem.disabled"
  11. v-model="formFields[sIndex]" :class="sItem.borderType | getBorderType"
  12. :placeholder="sItem.placeholder ? sItem.placeholder : ('请输入' + sItem.label)" />
  13. </template>
  14. </div>
  15. </div>
  16. </template>
  17. <template v-else-if="item.type === 'conditionItem'">
  18. <div class="form-item ">
  19. <div class="form-title fs-16">{{ item.label }}</div>
  20. <div v-for="(sItem, sIndex) in item.config" class="flex">
  21. <div class="flex1 mr-24">
  22. <div class="form-title">{{ sItem.label }}</div>
  23. <div class="flex">
  24. <el-select class="flex1" v-model="formFields[sIndex]" multiple placeholder="请选择">
  25. <el-option
  26. v-for="op in sItem.options"
  27. :key="op.value"
  28. :label="op.label"
  29. :value="op.value">
  30. </el-option>
  31. </el-select>
  32. </div>
  33. </div>
  34. <div class="flex1" v-show = "isShowOther(formFields[sIndex])">
  35. <div class="form-title">其他</div>
  36. <div class="flex">
  37. <el-input v-model="formFields[sItem.otherCode]" :class="sItem.borderType | getBorderType"></el-input>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. </template>
  43. </div>
  44. </div>
  45. </template>
  46. <script>
  47. export default {
  48. components: {
  49. },
  50. props: {
  51. formConfig: {
  52. type: Array,
  53. value: () => [],
  54. },
  55. formData: {
  56. type: Object,
  57. value: () => ({})
  58. }
  59. },
  60. data() {
  61. return {
  62. formFields: {},//表单双休绑定字段
  63. allFieldsConfig: {},//包含config的所有字段,主要用于校验表单是否填写
  64. };
  65. },
  66. watch: {
  67. immediate: true,
  68. formData: {
  69. handler(v) {
  70. this.handleFormField();
  71. }
  72. }
  73. },
  74. mounted() {
  75. this.handleFormField();
  76. },
  77. filters: {
  78. getBorderType(type) {
  79. const typeObj = {
  80. orange: "orange-border",
  81. green: "green-border",
  82. blue: "blue-border",
  83. }
  84. return typeObj[type] || ""
  85. },
  86. },
  87. methods: {
  88. isShowOther(v = []){
  89. return v.includes(-1);
  90. },
  91. //根据formConifg回填form表单数据
  92. handleFormField() {
  93. const result = {};
  94. let config = {};
  95. const { formConfig, formData } = this;
  96. formConfig.forEach((item) => {
  97. if (item.config) {
  98. config = { ...config, ...item.config }
  99. Object.keys(item.config).forEach(key => {
  100. const currentConfig = item.config[key];
  101. result[key] = formData[key];
  102. if(currentConfig.otherCode){//如果有其它的字段需要赋值给formData
  103. const {otherCode} = currentConfig;
  104. result[otherCode] = formData[otherCode]
  105. config[otherCode] = {label:"其他",type:"input"}
  106. }
  107. });
  108. console.log(item.config,"config")
  109. if(item.config?.otherCode){
  110. config[item.config?.otherCode] = item.config?.otherCode;
  111. }
  112. }
  113. })
  114. this.formFields = result;
  115. this.allFieldsConfig = config;
  116. console.log(config, "allFieldsConfig")
  117. },
  118. getFormData() {
  119. const { formFields, allFieldsConfig } = this;
  120. for (const key in formFields) {
  121. if (!formFields[key]) {
  122. const o = allFieldsConfig[key];
  123. let prefix = "";
  124. if (o.type === "input") {
  125. prefix = "填写"
  126. } else {
  127. prefix = "选择"
  128. }
  129. this.$message.error(`${o.label}还未${prefix}${prefix}后再提交`);
  130. return;
  131. }
  132. }
  133. return formFields;
  134. }
  135. },
  136. }
  137. </script>
  138. <style lang="scss" >
  139. .grid-container {
  140. display: grid;
  141. grid-template-columns: repeat(2, 1fr);
  142. /* 默认2列 */
  143. gap: 0 24px;
  144. }
  145. .form-item {
  146. background: #fff;
  147. padding: 20px;
  148. border-radius: 8px;
  149. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  150. margin-top: 24px;
  151. padding: 24px;
  152. border-radius: 5px 5px;
  153. }
  154. /* 或者使用 span 语法 */
  155. .full-row {
  156. grid-column: span 2;
  157. }
  158. .form-title {
  159. margin-bottom: 12px;
  160. font-size: 14px;
  161. font-weight: normal;
  162. color:#606266;
  163. }
  164. .orange-border {
  165. input {
  166. border-color: #f9c588;
  167. &:focus {
  168. border-color: #f9c588;
  169. }
  170. &:hover {
  171. border-color: #f9c588;
  172. }
  173. }
  174. }
  175. .green-border {
  176. input {
  177. border-color: green;
  178. &:focus {
  179. border-color: green;
  180. }
  181. &:hover {
  182. border-color: green;
  183. }
  184. }
  185. }
  186. .blue-border {
  187. input {
  188. border-color: #4ea2ff;
  189. &:focus {
  190. border-color: #4ea2ff;
  191. }
  192. &:hover {
  193. border-color: #4ea2ff;
  194. }
  195. }
  196. }
  197. .fs-16{
  198. font-size: 0.96rem;
  199. font-weight: bold;
  200. color:#464647
  201. }
  202. .flex1{
  203. flex:1;
  204. }
  205. .flex{
  206. display: flex;
  207. }
  208. .mr-24{
  209. margin-right: 24px;
  210. }
  211. </style>