batch.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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-table
  15. class="batch-table"
  16. :pagination="false"
  17. :rowKey="record => record.key"
  18. :columns="columns"
  19. :dataSource="dynamicValidateForm.domains"
  20. bordered
  21. :scroll="{
  22. x: listLength * 190 + 80 + (!record.options.hideSequence ? 60 : 0),
  23. y: record.options.scrollY
  24. }"
  25. >
  26. <template
  27. v-for="item in record.list"
  28. :slot="item.key"
  29. slot-scope="text, record, index"
  30. >
  31. <KFormModelItem
  32. :key="item.key + '1'"
  33. :record="item"
  34. :config="config"
  35. :parentDisabled="disabled"
  36. :index="index"
  37. :domains="dynamicValidateForm.domains"
  38. :dynamicData="dynamicData"
  39. v-model="record[item.model]"
  40. @input="handleInput"
  41. />
  42. </template>
  43. <template slot="dynamic-opr-button" slot-scope="text, record">
  44. <a-icon
  45. title="删除改行"
  46. v-if="!disabled"
  47. class="dynamic-opr-button"
  48. type="minus-circle-o"
  49. @click="removeDomain(record)"
  50. />
  51. <a-icon
  52. title="复制添加"
  53. v-if="!disabled"
  54. type="copy-o"
  55. class="dynamic-opr-button"
  56. @click="copyDomain(record)"
  57. />
  58. </template>
  59. </a-table>
  60. <a-button type="dashed" :disabled="disabled" @click="addDomain">
  61. <a-icon type="plus" />增加
  62. </a-button>
  63. </a-form-model>
  64. </template>
  65. <script>
  66. import KFormModelItem from "./module/KFormModelItem";
  67. export default {
  68. name: "KBatch",
  69. props: ["record", "value", "dynamicData", "config", "parentDisabled"],
  70. components: {
  71. KFormModelItem
  72. },
  73. watch: {
  74. value: {
  75. // value 需要深度监听及默认先执行handler函数
  76. handler(val) {
  77. this.dynamicValidateForm.domains = val || [];
  78. },
  79. immediate: true,
  80. deep: true
  81. }
  82. },
  83. data() {
  84. return {
  85. dynamicValidateForm: {
  86. domains: []
  87. }
  88. };
  89. },
  90. computed: {
  91. listLength() {
  92. return this.record.list.filter(item => !item.options.hidden).length;
  93. },
  94. columns() {
  95. const columns = [];
  96. if (!this.record.options.hideSequence) {
  97. columns.push({
  98. title: "序号",
  99. dataIndex: "sequence_index_number",
  100. width: "60px",
  101. align: "center",
  102. customRender: (text, record, index) => {
  103. return index + 1;
  104. }
  105. });
  106. }
  107. columns.push(
  108. ...this.record.list
  109. .filter(item => !item.options.hidden)
  110. .map((item, index) => {
  111. return {
  112. title: item.label,
  113. dataIndex: item.key,
  114. width: index === this.record.list.length - 1 ? "" : "190px",
  115. scopedSlots: { customRender: item.key }
  116. };
  117. })
  118. );
  119. columns.push({
  120. title: "操作",
  121. dataIndex: "dynamic-opr-button",
  122. fixed: "right",
  123. width: "80px",
  124. align: "center",
  125. scopedSlots: { customRender: "dynamic-opr-button" }
  126. });
  127. return columns;
  128. },
  129. disabled() {
  130. return this.record.options.disabled || this.parentDisabled;
  131. }
  132. },
  133. methods: {
  134. validationSubform() {
  135. let verification;
  136. this.$refs.dynamicValidateForm.validate(valid => {
  137. verification = valid;
  138. });
  139. return verification;
  140. },
  141. resetForm() {
  142. this.$refs.dynamicValidateForm.resetFields();
  143. },
  144. removeDomain(item) {
  145. const index = this.dynamicValidateForm.domains.indexOf(item);
  146. if (index !== -1) {
  147. this.dynamicValidateForm.domains.splice(index, 1);
  148. }
  149. },
  150. copyDomain(record) {
  151. const data = {};
  152. this.record.list.forEach(item => {
  153. data[item.model] = record[item.model];
  154. });
  155. this.dynamicValidateForm.domains.push({
  156. ...data,
  157. key: Date.now()
  158. });
  159. this.handleInput();
  160. },
  161. addDomain() {
  162. const data = {};
  163. this.record.list.forEach(item => {
  164. data[item.model] = item.options.defaultValue;
  165. });
  166. this.dynamicValidateForm.domains.push({
  167. ...data,
  168. key: Date.now()
  169. });
  170. this.handleInput();
  171. },
  172. handleInput() {
  173. this.$emit("change", this.dynamicValidateForm.domains);
  174. }
  175. }
  176. };
  177. </script>
  178. <style scoped>
  179. .dynamic-opr-button:last {
  180. margin-left: 0px;
  181. }
  182. .dynamic-opr-button {
  183. cursor: pointer;
  184. position: relative;
  185. top: 4px;
  186. font-size: 16px;
  187. color: #999;
  188. transition: all 0.3s;
  189. margin-left: 6px;
  190. }
  191. .dynamic-opr-button:hover {
  192. color: #e89;
  193. }
  194. .dynamic-opr-button[disabled] {
  195. cursor: not-allowed;
  196. opacity: 0.5;
  197. }
  198. </style>