kpi-indicator-group-child-table.vue 11 KB

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