batch2.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!--
  2. * @Description: 动态表格 用于批量填入数据
  3. * @Author: kcz
  4. * @Date: 2020-03-27 18:36:56
  5. * @LastEditors: kcz
  6. * @LastEditTime: 2021-05-14 14:04:14
  7. -->
  8. <template>
  9. <a-form-model
  10. ref="dynamicValidateForm"
  11. layout="inline"
  12. :model="dynamicValidateForm"
  13. >
  14. <a-card v-for="(d,index) in dynamicValidateForm.domains" :key="d.key">
  15. <a-row :gutter="24">
  16. <a-col :md="12" :sm="12">
  17. <div v-for="c in columns" :key="c.title">{{c.title}}</div>
  18. </a-col>
  19. <a-col :md="12" :sm="12">
  20. <!-- <div v-for="(d,index) in dynamicValidateForm.domains" :key="d.key"> -->
  21. <div v-for="item in d.recordList" :key="item.key">
  22. <KFormModelItem
  23. :key="item.key + '1'"
  24. :record="item"
  25. :config="config"
  26. :parentDisabled="disabled"
  27. :index="index"
  28. :domains="dynamicValidateForm.domains"
  29. :dynamicData="dynamicData"
  30. v-model="d[item.model]"
  31. @input="handleInput"
  32. />
  33. </div>
  34. <!-- </div> -->
  35. </a-col>
  36. </a-row>
  37. </a-card>
  38. <a-table
  39. class="batch-table"
  40. :pagination="false"
  41. :rowKey="record => record.key"
  42. :columns="columns"
  43. :dataSource="dynamicValidateForm.domains"
  44. bordered
  45. :scroll="{
  46. x: listLength * 190 + 80 + (!record.options.hideSequence ? 60 : 0),
  47. y: record.options.scrollY
  48. }"
  49. >
  50. <template
  51. v-for="item in record.list"
  52. :slot="item.key"
  53. slot-scope="text, record, index"
  54. >
  55. <KFormModelItem
  56. :key="item.key + '1'"
  57. :record="item"
  58. :config="config"
  59. :parentDisabled="disabled"
  60. :index="index"
  61. :domains="dynamicValidateForm.domains"
  62. :dynamicData="dynamicData"
  63. v-model="record[item.model]"
  64. @input="handleInput"
  65. />
  66. </template>
  67. <template slot="dynamic-opr-button" slot-scope="text, record">
  68. <a-icon
  69. title="删除改行"
  70. v-if="!disabled"
  71. class="dynamic-opr-button"
  72. type="minus-circle-o"
  73. @click="removeDomain(record)"
  74. />
  75. <a-icon
  76. title="复制添加"
  77. v-if="!disabled"
  78. type="copy-o"
  79. class="dynamic-opr-button"
  80. @click="copyDomain(record)"
  81. />
  82. </template>
  83. </a-table>
  84. <a-button type="dashed" :disabled="disabled" @click="addDomain">
  85. <a-icon type="plus" />增加
  86. </a-button>
  87. </a-form-model>
  88. </template>
  89. <script>
  90. import KFormModelItem from "./module/KFormModelItem";
  91. export default {
  92. name: "KBatch",
  93. props: ["record", "value", "dynamicData", "config", "parentDisabled"],
  94. components: {
  95. KFormModelItem
  96. },
  97. watch: {
  98. value: {
  99. // value 需要深度监听及默认先执行handler函数
  100. handler(val) {
  101. this.dynamicValidateForm.domains = val || [];
  102. },
  103. immediate: true,
  104. deep: true
  105. }
  106. },
  107. data() {
  108. return {
  109. dynamicValidateForm: {
  110. domains: []
  111. }
  112. };
  113. },
  114. created(){
  115. },
  116. computed: {
  117. listLength() {
  118. return this.record.list.filter(item => !item.options.hidden).length;
  119. },
  120. columns() {
  121. const columns = [];
  122. if (!this.record.options.hideSequence) {
  123. columns.push({
  124. title: "序号",
  125. dataIndex: "sequence_index_number",
  126. width: "60px",
  127. align: "center",
  128. customRender: (text, record, index) => {
  129. return index + 1;
  130. }
  131. });
  132. }
  133. columns.push(
  134. ...this.record.list
  135. .filter(item => !item.options.hidden)
  136. .map((item, index) => {
  137. return {
  138. title: item.label,
  139. dataIndex: item.key,
  140. width: index === this.record.list.length - 1 ? "" : "190px",
  141. scopedSlots: { customRender: item.key }
  142. };
  143. })
  144. );
  145. columns.push({
  146. title: "操作",
  147. dataIndex: "dynamic-opr-button",
  148. fixed: "right",
  149. width: "80px",
  150. align: "center",
  151. scopedSlots: { customRender: "dynamic-opr-button" }
  152. });
  153. return columns;
  154. },
  155. disabled() {
  156. return this.record.options.disabled || this.parentDisabled;
  157. }
  158. },
  159. methods: {
  160. validationSubform() {
  161. let verification;
  162. this.$refs.dynamicValidateForm.validate(valid => {
  163. verification = valid;
  164. });
  165. return verification;
  166. },
  167. resetForm() {
  168. this.$refs.dynamicValidateForm.resetFields();
  169. },
  170. removeDomain(item) {
  171. const index = this.dynamicValidateForm.domains.indexOf(item);
  172. if (index !== -1) {
  173. this.dynamicValidateForm.domains.splice(index, 1);
  174. }
  175. },
  176. copyDomain(record) {
  177. const data = {};
  178. this.record.list.forEach(item => {
  179. data[item.model] = record[item.model];
  180. });
  181. this.dynamicValidateForm.domains.push({
  182. ...data,
  183. key: Date.now()
  184. });
  185. this.handleInput();
  186. },
  187. addDomain() {
  188. const data = {};
  189. this.record.list.forEach(item => {
  190. data[item.model] = item.options.defaultValue;
  191. });
  192. this.dynamicValidateForm.domains.push({
  193. ...data,
  194. key: Date.now()
  195. });
  196. //循环字段数据复制字段形成唯一
  197. this.dynamicValidateForm.domains.forEach((element,index) => {
  198. //组件数组
  199. const recordList=[]
  200. //循环原始组件获取字段生成流水字段
  201. this.record.list.forEach(item => {
  202. data[item.model] = item.options.defaultValue;
  203. let m=item.model;
  204. m=m+index;
  205. if(!element[m]){
  206. element[m]=""
  207. }
  208. //根据流水字段形成对应组件
  209. const r=JSON.parse(JSON.stringify(item));
  210. r.model=m;
  211. recordList.push(r)
  212. });
  213. if(!element.recordList){
  214. element.recordList=recordList;
  215. }
  216. });
  217. this.handleInput();
  218. },
  219. handleInput() {
  220. this.dynamicValidateForm.domains.forEach((element,index) => {
  221. console.log("element",element)
  222. this.record.list.forEach(item => {
  223. let m=item.model;
  224. m=m+index;
  225. element[item.model]=element[m];
  226. });
  227. console.log("element",element)
  228. });
  229. this.$emit("change", this.dynamicValidateForm.domains);
  230. }
  231. }
  232. };
  233. </script>
  234. <style scoped>
  235. .dynamic-opr-button:last {
  236. margin-left: 0px;
  237. }
  238. .dynamic-opr-button {
  239. cursor: pointer;
  240. position: relative;
  241. top: 4px;
  242. font-size: 16px;
  243. color: #999;
  244. transition: all 0.3s;
  245. margin-left: 6px;
  246. }
  247. .dynamic-opr-button:hover {
  248. color: #e89;
  249. }
  250. .dynamic-opr-button[disabled] {
  251. cursor: not-allowed;
  252. opacity: 0.5;
  253. }
  254. </style>