audit-rectchild-table.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <div :class="$style.wrapper">
  3. <div :class="[$style.caption]">
  4. <div v-if="!readOnly && deleteqx" :class="$style.header">
  5. <a-button
  6. type="link"
  7. :disabled="selectedRowKeys.length === 0"
  8. @click="remove(selectedRowKeys)"
  9. ><a-icon type="minus-circle" :theme="'filled'" />
  10. 删除
  11. </a-button>
  12. </div>
  13. </div>
  14. <sd-table
  15. size="middle"
  16. :columns="_columns"
  17. sortable
  18. :data-source="value"
  19. :row-key="(record, index) => index"
  20. :pagination="flagpage ? pagination : false"
  21. :row-selection="
  22. readOnly
  23. ? null
  24. : {
  25. getCheckboxProps(record) {
  26. return {
  27. props: { disabled: !showSelection(record) },
  28. }
  29. },
  30. selectedRowKeys: selectedRowKeys,
  31. onChange: onSelectChange,
  32. }
  33. "
  34. :custom-row="
  35. (record, index) => {
  36. return {
  37. on: {
  38. click: () => {
  39. edit(record, index)
  40. },
  41. },
  42. }
  43. }
  44. "
  45. @update:data-source="($event) => $emit('change', $event)"
  46. >
  47. <template v-for="field in fields" :slot="'sd_' + field.name" slot-scope="text, record, index">
  48. <slot v-bind="{ field, text, record, index }" :name="field.name">
  49. <sd-attachment
  50. v-if="field.dataType === 'attachment'"
  51. :key="attkeys[index]"
  52. :group-id="JSON.parse(text).value"
  53. read-only
  54. />
  55. <sd-quill-reader v-else-if="field.dataType === 'tinymce'" :value="text" />
  56. <span v-else :key="field.name">
  57. {{ getDisplayVaule(field, text) }}
  58. </span>
  59. </slot>
  60. </template>
  61. </sd-table>
  62. <a-modal
  63. v-model="visible"
  64. :title="label || '详情'"
  65. destroy-on-close
  66. v-bind="{ width: 1200 }"
  67. :mask-closable="false"
  68. :footer="readOnly || !findinfoeditable ? null : undefined"
  69. @ok="save"
  70. @cancel="attkeys[action] = Math.random()"
  71. >
  72. <sdForm ref="form" :init-values="editRecord" :read-only="readOnly">
  73. <template v-slot="scope">
  74. <!--
  75. @slot 详情表单内容,8.0.4及以后版本支持
  76. @binding {object} model 整个表单的数据,可用于 v-model 绑定
  77. @binding {object} fields 后台返回的字段定义
  78. -->
  79. <slot v-bind="scope" name="form">
  80. <sd-form-item
  81. v-for="field in fields"
  82. :key="field.name"
  83. :hidden="field.dataType === 'id'"
  84. :name="field.name"
  85. />
  86. </slot>
  87. </template>
  88. </sdForm>
  89. </a-modal>
  90. </div>
  91. </template>
  92. <script>
  93. import { Modal } from 'ant-design-vue'
  94. import { getComponentSpec } from '@/common/components/sd-form'
  95. import axios from '@/common/services/axios-instance'
  96. import components from './_import-components/audit-rectchild-table-import'
  97. /**
  98. * 主子表、动态表格字段
  99. * @displayName SdChildTable 主子表
  100. */
  101. export default {
  102. name: 'AuditRectChildTable',
  103. components: {
  104. ...components,
  105. sdForm: () => import('@/common/components/sd-form'),
  106. },
  107. model: {
  108. prop: 'value',
  109. event: 'change',
  110. },
  111. props: {
  112. /**
  113. * 字表的值,{key:value}的格式
  114. */
  115. value: {
  116. type: Array,
  117. default: () => [],
  118. },
  119. /**
  120. * 字表的字段属性
  121. */
  122. fields: {
  123. type: Array,
  124. default: () => [],
  125. },
  126. /**
  127. * 是否只读
  128. */
  129. readOnly: {
  130. type: Boolean,
  131. default: false,
  132. },
  133. /**
  134. * 标签名
  135. */
  136. label: {
  137. type: String,
  138. default: '',
  139. },
  140. /**
  141. * 自定义子表列属性,如[{dataIndex:'shuxingmingzi15',width:'120px'}],8.0.4及以后版本支持
  142. */
  143. columns: {
  144. type: Array,
  145. default: () => [],
  146. },
  147. /**
  148. * 某行是否可选
  149. * @since 8.0.11
  150. */
  151. showSelection: {
  152. type: Function,
  153. default: () => true,
  154. },
  155. // 子组件接收函数
  156. handleBeforeAdd: {
  157. type: Function,
  158. },
  159. flagpage: {
  160. type: Boolean,
  161. default: false,
  162. },
  163. pages: {
  164. type: Number,
  165. default: 10,
  166. },
  167. rowclick: {
  168. type: Boolean,
  169. default: true,
  170. },
  171. fnrowclick: {
  172. type: Function,
  173. default: null,
  174. },
  175. findinfoeditable: {
  176. type: Boolean,
  177. default: true,
  178. },
  179. /** 删除权限 */
  180. deleteqx: {
  181. type: Boolean,
  182. default: true,
  183. },
  184. },
  185. data() {
  186. return {
  187. selectedRowKeys: [],
  188. visible: false,
  189. editRecord: [], // 待修改或编辑的记录
  190. attkeys: {}, // 附件组件的key,行编辑了就修改对应的附件key,强制更新附件列表
  191. pageOption: {
  192. maxResults: 10,
  193. },
  194. cardIndex: null,
  195. pagination: {
  196. current: 1,
  197. total: 0,
  198. showTotal: (total) => {
  199. return `总共 ${total} 条`
  200. },
  201. onChange: (page) => {
  202. this.pagination.current = page
  203. this.pageOption.startPosition = this.pageOption.maxResults * (page - 1)
  204. this.cardIndex = null
  205. // this.updatedata(page)
  206. },
  207. pageSize: 10,
  208. }, // 分页属性
  209. }
  210. },
  211. computed: {
  212. _columns() {
  213. const fields = JSON.parse(JSON.stringify(this.fields))
  214. const obj = fields
  215. .filter((item) => item.dataType !== 'id')
  216. .map((item) => {
  217. const t = {
  218. dataIndex: item.name,
  219. key: item.name,
  220. title: item.caption,
  221. scopedSlots: { customRender: 'sd_' + item.name },
  222. }
  223. const column = this.columns.find((c) => c.dataIndex === item.name)
  224. if (column) Object.assign(t, column)
  225. return t
  226. })
  227. .filter((item) => item.sdHidden === false)
  228. const newobj = []
  229. this.columns.forEach((item) => {
  230. const lso = obj.find((o) => o.dataIndex === item.dataIndex)
  231. if (lso !== undefined) {
  232. newobj.push(lso)
  233. }
  234. })
  235. return newobj
  236. },
  237. },
  238. mounted() {
  239. this.pagination.pageSize = this.pages
  240. },
  241. methods: {
  242. getDisplayVaule(field, text) {
  243. if (getComponentSpec(field).getDisplayValue) {
  244. if (typeof text !== 'undefined' && text !== null) {
  245. const value = getComponentSpec(field).parseBackendValue?.(text) || text
  246. return getComponentSpec(field).getDisplayValue(value)
  247. } else {
  248. return ''
  249. }
  250. }
  251. return text
  252. },
  253. add() {
  254. if (this.handleBeforeAdd) {
  255. this.handleBeforeAdd(null, (res) => {
  256. if (!res) {
  257. this.addMethod()
  258. }
  259. })
  260. } else {
  261. this.addMethod()
  262. }
  263. },
  264. addMethod() {
  265. this.editRecord = this.fields
  266. this.action = 'add'
  267. const att = this.editRecord.find((item) => item.dataType === 'attachment')
  268. if (att) {
  269. axios('api/xcoa-mobile/v1/attachment-extend/attachments-create-group-code').then((res) => {
  270. att.value = JSON.stringify({
  271. value: res.data,
  272. })
  273. this.visible = true
  274. })
  275. } else {
  276. this.visible = true
  277. }
  278. },
  279. edit(record, index) {
  280. if (this.fnrowclick) {
  281. this.fnrowclick(record)
  282. }
  283. if (!this.rowclick) {
  284. this.$emit('rowclick', record)
  285. } else {
  286. const indexNum = (this.pagination.current - 1) * this.pagination.pageSize + index
  287. this.editRecord = JSON.parse(JSON.stringify(this.fields))
  288. this.editRecord.forEach((item) => {
  289. // 把值放到字段信息fields里
  290. item.value = this.value[indexNum][item.name]
  291. })
  292. this.visible = true
  293. this.action = indexNum
  294. }
  295. },
  296. editlsfk(index, editable) {
  297. const indexNum = (this.pagination.current - 1) * this.pagination.pageSize + index
  298. this.editRecord = JSON.parse(JSON.stringify(this.fields))
  299. this.editRecord.forEach((item) => {
  300. // 把值放到字段信息fields里
  301. item.value = this.value[indexNum][item.name]
  302. })
  303. this.visible = true
  304. this.action = indexNum
  305. },
  306. save() {
  307. if (this.readOnly || !this.visible) {
  308. // 判断visable为false时,modal没有是为了防止快速重复点击确定
  309. this.visible = false
  310. return
  311. }
  312. this.$refs.form.validateFields().then(() => {
  313. const value = {}
  314. this.$refs.form.getBackendValues().forEach((item) => {
  315. value[item.name] = item.value
  316. })
  317. const data = this.value || [] // 兼容初始值是null的情况
  318. if (this.action === 'add') {
  319. // 新增
  320. data.push(value)
  321. }
  322. if (!isNaN(this.action)) {
  323. // 修改
  324. this.attkeys[this.action] = Math.random()
  325. data[this.action] = value
  326. }
  327. /**
  328. * 动态表格值变化时触发
  329. * @property {Object} data 子表的值
  330. */
  331. this.$emit('change', [...data])
  332. this.visible = false
  333. })
  334. },
  335. onSelectChange(selectedRowKeys) {
  336. this.selectedRowKeys = selectedRowKeys
  337. },
  338. remove(keys) {
  339. Modal.confirm({
  340. title: '确认删除所选中的数据吗?',
  341. okType: 'danger',
  342. onOk: () => {
  343. // 用的序号作key,从大往小删,防止串了
  344. const removeKeys = keys.sort((a, b) => b - a)
  345. removeKeys.forEach((key) => {
  346. this.value.splice(key, 1)
  347. })
  348. this.selectedRowKeys = []
  349. this.$emit('change', this.value)
  350. },
  351. })
  352. },
  353. },
  354. }
  355. </script>
  356. <style module lang="scss">
  357. @use '@/common/design' as *;
  358. // @use "sd-web-print.scss" as print;
  359. .header {
  360. position: absolute;
  361. top: 0;
  362. right: 0;
  363. :global .ant-input-search {
  364. width: 200px;
  365. }
  366. :global .ant-btn {
  367. margin: 5px;
  368. }
  369. }
  370. .caption {
  371. position: absolute;
  372. width: 100%;
  373. min-height: 40px;
  374. margin: 3px 0;
  375. text-align: center;
  376. }
  377. .wrapper {
  378. :global(.ant-table-tbody) {
  379. .clickable-cell {
  380. color: $primary-color;
  381. cursor: pointer;
  382. &:hover {
  383. color: $primary-5;
  384. }
  385. &:active {
  386. color: $primary-7;
  387. }
  388. }
  389. }
  390. }
  391. // :global(.rectprojectchildFormhide) {
  392. // :global(.ant-modal-footer) {
  393. // display: none;
  394. // }
  395. // }
  396. // 打印状态下 子表上选择列的按钮
  397. // @include print.wrapper-for-printer {
  398. // .wrapper {
  399. // :global(.ant-table-thead .anticon) {
  400. // display: none;
  401. // }
  402. // }
  403. // }
  404. </style>