华西海圻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.

297 lines
9.1 KiB

2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
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, key) in item.config" class="form-item" :class="sItem.span == 1 ? 'full-row' : ''"
  7. :key="key">
  8. <template v-if="sItem.type === 'input'">
  9. <div class="form-title">{{ sItem.label }}</div>
  10. <HandleFormItem :item="sItem" :value="formFields[key]" @input="onInput(key, $event)"
  11. @copy="onCopy(sItem, key)" />
  12. </template>
  13. </div>
  14. </div>
  15. </template>
  16. <template v-else-if="item.type === 'conditionItem'">
  17. <div class="form-item ">
  18. <div class="form-title fs-16" v-if="item.label">{{ item.label }}</div>
  19. <div v-for="(sItem, key) in item.config" class="c-Item grid-container">
  20. <div class="p-r-20">
  21. <div class="form-title">{{ sItem.label }}</div>
  22. <div class="flex ">
  23. <HandleFormItem type="select" :item="sItem" :value="formFields[key]"
  24. @copy="onCopy(sItem, key)" @change="onSelectChange(key, $event)" />
  25. </div>
  26. </div>
  27. <div class="p-r-20">
  28. <div v-show="isShowOther(formFields[key])">
  29. <div class="form-title">其他</div>
  30. <div class="flex">
  31. <el-input v-model="formFields[sItem.otherCode]"
  32. :class="sItem.borderType | getBorderType"></el-input>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </template>
  39. <template v-else-if="item.type === 'cellItem'">
  40. <div class="form-item ">
  41. <div class="form-title fs-16" v-if="item.label">{{ item.label }}</div>
  42. <div class="grid-container">
  43. <div v-for="(sItem, key) in item.config" class="c-Item " :class="sItem.span == 1 ? 'full-row' : ''"
  44. :key="key">
  45. <div class="form-title" v-if="sItem.label">{{ sItem.label }}</div>
  46. <div v-if="sItem.type === 'dateTime'">
  47. <el-date-picker v-model="formFields[key]" type="datetime" class="w-100"
  48. format="yyyy/MM/DD HH:mm:ss" value-format="yyyy/MM/DD HH:mm:ss"
  49. :placeholder="sItem.placeholder ? sItem.placeholder : ('请选择' + sItem.label)">
  50. </el-date-picker>
  51. </div>
  52. <div v-else-if="sItem.type === 'select'">
  53. <el-select class="w-100" :class="sItem.borderType | getBorderType" v-model="formFields[key]"
  54. multiple :placeholder="sItem.placeholder ? sItem.placeholder : ('请选择' + sItem.label)">
  55. <el-option v-for="op in sItem.options" :key="op.value" :label="op.label"
  56. :value="op.value">
  57. </el-option>
  58. </el-select>
  59. </div>
  60. <div v-else-if="sItem.type === 'input'">
  61. <el-input class="w-100" :class="sItem.borderType | getBorderType" v-model="formFields[key]"
  62. :placeholder="sItem.placeholder ? sItem.placeholder : ('请输入' + sItem.label)"></el-input>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </template>
  68. </div>
  69. </div>
  70. </template>
  71. <script>
  72. import HandleFormItem from "./HandleFormItem.vue"
  73. export default {
  74. components: {
  75. HandleFormItem
  76. },
  77. props: {
  78. formConfig: {
  79. type: Array,
  80. value: () => [],
  81. },
  82. formData: {
  83. type: Object,
  84. value: () => ({})
  85. }
  86. },
  87. data() {
  88. return {
  89. formFields: {},//表单双休绑定字段
  90. allFieldsConfig: {},//包含config的所有字段,主要用于校验表单是否填写
  91. };
  92. },
  93. watch: {
  94. immediate: true,
  95. formData: {
  96. handler(v) {
  97. this.handleFormField();
  98. }
  99. }
  100. },
  101. mounted() {
  102. this.handleFormField();
  103. },
  104. filters: {
  105. getBorderType(type) {
  106. const typeObj = {
  107. orange: "orange-border",
  108. green: "green-border",
  109. blue: "blue-border",
  110. }
  111. return typeObj[type] || ""
  112. },
  113. },
  114. methods: {
  115. isShowOther(v = []) {
  116. console.log(v, "vvv")
  117. return v.includes("-1");
  118. },
  119. //根据formConifg回填form表单数据
  120. handleFormField() {
  121. const result = {};
  122. let config = {};
  123. const { formConfig, formData } = this;
  124. formConfig.forEach((item) => {
  125. if (item.config) {
  126. config = { ...config, ...item.config }
  127. Object.keys(item.config).forEach(key => {
  128. const currentConfig = item.config[key];
  129. result[key] = formData[key];
  130. if (currentConfig.otherCode) {//如果有其它的字段需要赋值给formData
  131. const { otherCode } = currentConfig;
  132. result[otherCode] = formData[otherCode]
  133. config[otherCode] = { label: "其他", type: "input" }
  134. }
  135. });
  136. console.log(item.config, "config")
  137. if (item.config?.otherCode) {
  138. config[item.config?.otherCode] = item.config?.otherCode;
  139. }
  140. }
  141. })
  142. this.formFields = result;
  143. this.allFieldsConfig = config;
  144. console.log(config, "allFieldsConfig")
  145. },
  146. getFormData() {
  147. const { formFields, allFieldsConfig } = this;
  148. return new Promise((resolve,reject)=>{
  149. for (const key in formFields) {
  150. if (!formFields[key]) {
  151. const o = allFieldsConfig[key];
  152. let prefix = "";
  153. if (o.type === "input") {
  154. prefix = "填写"
  155. } else {
  156. prefix = "选择"
  157. }
  158. this.$message.error(`${o.label}还未${prefix}${prefix}后再提交`);
  159. reject("还未填写完");
  160. return;
  161. }
  162. }
  163. resolve(formFields)
  164. })
  165. },
  166. onInput(key, val) {
  167. this.formFields[key] = val;
  168. this.$emit("input", { key, value: val });
  169. },
  170. onSelectChange(key, val) {
  171. this.formFields[key] = val;
  172. this.$emit("select", { key, value: val });
  173. },
  174. //复制
  175. onCopy(config, key) {
  176. const { formFields } = this;
  177. if (config.copyFrom) {
  178. formFields[key] = formFields[config.copyFrom]
  179. }
  180. },
  181. },
  182. }
  183. </script>
  184. <style lang="scss">
  185. .grid-container {
  186. display: grid;
  187. grid-template-columns: repeat(2, 1fr);
  188. /* 默认2列 */
  189. gap: 0 24px;
  190. }
  191. .w-100 {
  192. width: 100% !important;
  193. }
  194. .form-item {
  195. background: #fff;
  196. padding: 20px;
  197. border-radius: 8px;
  198. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  199. margin-top: 24px;
  200. padding: 24px;
  201. border-radius: 5px 5px;
  202. }
  203. /* 或者使用 span 语法 */
  204. .full-row {
  205. grid-column: span 2;
  206. }
  207. .c-Item {
  208. &:not(:last-child) {
  209. margin-bottom: 16px;
  210. }
  211. }
  212. .form-title {
  213. margin-bottom: 12px;
  214. font-size: 14px;
  215. font-weight: normal;
  216. color: #606266;
  217. }
  218. .p-r-20{
  219. padding-right: 20px;
  220. }
  221. .orange-border {
  222. input {
  223. border-color: #f9c588;
  224. &:focus {
  225. border-color: #f9c588;
  226. }
  227. &:hover {
  228. border-color: #f9c588;
  229. }
  230. }
  231. }
  232. .green-border {
  233. input {
  234. border-color: green;
  235. &:focus {
  236. border-color: green;
  237. }
  238. &:hover {
  239. border-color: green;
  240. }
  241. }
  242. }
  243. .blue-border {
  244. input {
  245. border-color: #4ea2ff;
  246. &:focus {
  247. border-color: #4ea2ff;
  248. }
  249. &:hover {
  250. border-color: #4ea2ff;
  251. }
  252. }
  253. }
  254. .fs-16 {
  255. font-size: 0.96rem;
  256. font-weight: bold;
  257. color: #464647
  258. }
  259. .flex1 {
  260. flex: 1;
  261. }
  262. .flex {
  263. display: flex;
  264. }
  265. .mr-24 {
  266. margin-right: 24px;
  267. }
  268. </style>