xm-formchildtable-render.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <div>
  3. <div :class="$style.wrapper">
  4. <div :class="$style.btnselect">
  5. <a-button type="link" :class="$style.batchselect" @click="addNewform">
  6. <a-icon type="plus-circle" :theme="'filled'" />
  7. 添加
  8. </a-button>
  9. <a-button type="link" :class="$style.batchselect" @click="deleteRow">
  10. <a-icon type="minus-circle" :theme="'filled'" />
  11. 删除
  12. </a-button>
  13. </div>
  14. </div>
  15. <xm-child-table
  16. ref="xmformchildtable"
  17. v-bind="$attrs"
  18. :label="labelCompute"
  19. :columns="columns"
  20. :addbuttonvisiable="false"
  21. :deletebuttonvisiable="false"
  22. v-on="$listeners"
  23. >
  24. <template v-slot:form>
  25. <span />
  26. <component
  27. :is="builder"
  28. :form-data="{
  29. attrFD: $attrs.childAttrFD || { columns: 1 },
  30. pageFieldInfos: $attrs.fields,
  31. }"
  32. />
  33. </template>
  34. <template
  35. v-for="field in items.filter((item) => item.componentForDisplay)"
  36. v-slot:[field.name]="{ text }"
  37. >
  38. <component
  39. :is="field.componentForDisplay"
  40. :key="field.name"
  41. :value="text"
  42. :read-only="true"
  43. />
  44. </template>
  45. </xm-child-table>
  46. </div>
  47. </template>
  48. <script>
  49. import axios from '@/common/services/axios-instance'
  50. import asyncComponent from '@/common/services/async-component'
  51. import crossWindowWatcher from '@/common/services/cross-window-watcher'
  52. import { message } from 'ant-design-vue'
  53. import runtimeUtil from '../../../src/form-designer/runtime-util'
  54. import XmChildTable from '../../../src_product/iam/components/xm-child-table.vue'
  55. import components from './_import-components/xm-formchildtable-render-import'
  56. const XmFormchildtableRender = {
  57. name: 'XmFormchildtableRender',
  58. components: {
  59. ...components,
  60. // 自动导入的是当前目录的child-table
  61. [XmChildTable.name]: XmChildTable,
  62. },
  63. inheritAttrs: false,
  64. model: {
  65. prop: 'value',
  66. event: 'change',
  67. },
  68. props: {
  69. label: {
  70. type: String,
  71. default: '',
  72. },
  73. hideCaption: {
  74. type: Boolean,
  75. default: false,
  76. },
  77. },
  78. data() {
  79. return {
  80. builder: asyncComponent(() =>
  81. import(
  82. /* webpackChunkName: "sd-form-by-builder" */ '@/common/components/sd-form-by-builder.vue'
  83. )
  84. ),
  85. }
  86. },
  87. computed: {
  88. items() {
  89. if (this.$attrs.fields.length === 0) return []
  90. const results = runtimeUtil
  91. .getTableInfo(this.$attrs.fields, { columns: 1 })
  92. .map((tr) => tr[0])
  93. return results
  94. },
  95. columns() {
  96. return this.$attrs.fields
  97. .filter((item) => item.attrFD.hidden)
  98. .map((item) => {
  99. return {
  100. dataIndex: item.name,
  101. sdHidden: true,
  102. }
  103. })
  104. .concat(this.$attrs.columns || [])
  105. },
  106. labelCompute() {
  107. return this.hideCaption ? null : this.label
  108. },
  109. },
  110. inject: {
  111. SdFormContext: { default: () => {} },
  112. },
  113. methods: {
  114. // 添加行
  115. addNewform() {
  116. const url = '/sd-webform?pageId=' + this.$attrs.pageId + '&' + this.$attrs.urlQuery // 新建表单链接
  117. crossWindowWatcher.waitForChanged(url).then((res) => {
  118. if (res && res.pageFormData?.pageFieldInfos) {
  119. const tempJson = {}
  120. const id = res.pageFormData.pageFieldInfos.find((f) => {
  121. return f.name === 'id'
  122. })
  123. tempJson.id = id.value
  124. // 子表字段赋值
  125. this.$attrs.columns?.forEach((item) => {
  126. const tempc = res.pageFormData.pageFieldInfos.find((f) => {
  127. return f.name === item.dataIndex
  128. })
  129. tempJson[item.dataIndex] = tempc.value
  130. })
  131. // 子表追加值
  132. let tempArr = []
  133. if (this.SdFormContext.model[this.$attrs.fieldName]) {
  134. tempArr = [...this.SdFormContext.model[this.$attrs.fieldName]]
  135. }
  136. tempArr.push(tempJson)
  137. this.SdFormContext.model[this.$attrs.fieldName] = [...tempArr]
  138. }
  139. })
  140. },
  141. // 删除行
  142. deleteRow() {
  143. const keys = this.$refs.xmformchildtable.selectedRowKeys
  144. let ids = ''
  145. keys.forEach((i) => {
  146. if (ids === '') {
  147. ids = this.$attrs.value[i].id
  148. } else {
  149. ids = ids + ',' + this.$attrs.value[i].id
  150. }
  151. })
  152. if (ids !== '') {
  153. // 调用接口删除配置的formid对应库表数据
  154. const url = `api/framework/v1/page/${this.$attrs.formId}?ids=${ids}`
  155. this.delFormData(url)
  156. .then((res) => {
  157. if (res.status === 200) {
  158. // 用的序号作key,从大往小删,防止串了
  159. const removeKeys = keys.sort((a, b) => b - a)
  160. removeKeys.forEach((key) => {
  161. this.$attrs.value.splice(key, 1)
  162. })
  163. this.$refs.xmformchildtable.selectedRowKeys = []
  164. // 更新子表数据
  165. this.$refs.xmformchildtable.fnupdate(this.$attrs.value)
  166. }
  167. })
  168. .catch((e) => {
  169. message.error('删除失败,请联系管理员')
  170. })
  171. }
  172. },
  173. delFormData(url) {
  174. return axios.delete(url).then((res) => res)
  175. },
  176. },
  177. }
  178. // 默认导出:编辑模式的组件
  179. export default XmFormchildtableRender
  180. // forDisplay导出:只读模式的组件
  181. export { XmFormchildtableRender as forDisplay }
  182. </script>
  183. <style module lang="scss">
  184. @use '@/common/design' as *;
  185. .btnselect {
  186. position: relative;
  187. top: 2px;
  188. float: right;
  189. .batchselect {
  190. z-index: 100;
  191. margin-left: 10px;
  192. }
  193. }
  194. </style>