|
1 | 1 | <template> |
2 | | - <template v-for="(actionItem, _index) in actionFilters" :key="`${_index}-${actionItem.label}`"> |
| 2 | + <template v-for="(actionItem, index) in actionFilters" :key="`${index}-${actionItem.label}`"> |
3 | 3 | <component |
4 | | - :is="actionItem.popConfirm ? Popconfirm : 'span'" |
| 4 | + :is="actionItem.popConfirm ? 'a-popconfirm' : 'span'" |
5 | 5 | :title="actionItem.title" |
6 | 6 | v-bind="actionItem.popConfirm" |
7 | 7 | > |
8 | | - <a-button type="link" v-bind="actionItem">{{ actionItem.label }}</a-button> |
| 8 | + <a-button |
| 9 | + type="link" |
| 10 | + :loading="loadingMap.get(getKey(actionItem, index))" |
| 11 | + v-bind="actionItem" |
| 12 | + > |
| 13 | + {{ actionItem.label }} |
| 14 | + </a-button> |
9 | 15 | </component> |
10 | 16 | </template> |
11 | 17 | </template> |
12 | 18 |
|
13 | | -<script lang="ts" setup> |
14 | | - import { computed } from 'vue'; |
| 19 | +<script lang="ts"> |
| 20 | + import { defineComponent, computed, ref } from 'vue'; |
15 | 21 | import { Popconfirm } from 'ant-design-vue'; |
16 | 22 | import type { PropType } from 'vue'; |
17 | 23 | import type { ActionItem } from '../types/tableAction'; |
| 24 | + import type { CustomRenderParams } from '../types/column'; |
18 | 25 | import { verifyAuth } from '@/core/permission/'; |
19 | | - import { isString, isObject } from '@/utils/is'; |
| 26 | + import { isString, isObject, isAsyncFunction } from '@/utils/is'; |
20 | 27 |
|
21 | | - const props = defineProps({ |
22 | | - actions: { |
23 | | - // 表格行动作 |
24 | | - type: Array as PropType<ActionItem[]>, |
25 | | - default: () => [], |
| 28 | + export default defineComponent({ |
| 29 | + components: { [Popconfirm.name]: Popconfirm }, |
| 30 | + props: { |
| 31 | + actions: { |
| 32 | + // 表格行动作 |
| 33 | + type: Array as PropType<ActionItem[]>, |
| 34 | + default: () => [], |
| 35 | + }, |
| 36 | + columnParams: { |
| 37 | + type: Object as PropType<CustomRenderParams>, |
| 38 | + default: () => ({}), |
| 39 | + }, |
| 40 | + rowKey: [String, Number] as PropType<Key>, |
26 | 41 | }, |
27 | | - }); |
| 42 | + setup(props) { |
| 43 | + const loadingMap = ref(new Map<string, boolean>()); |
| 44 | +
|
| 45 | + const actionFilters = computed(() => { |
| 46 | + return props.actions |
| 47 | + .filter((item) => { |
| 48 | + const auth = item.auth; |
| 49 | +
|
| 50 | + if (Object.is(auth, undefined)) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + if (isString(auth)) { |
| 54 | + const isValid = verifyAuth(auth); |
| 55 | + item.disabled ??= !isValid; |
| 56 | + if (item.disabled && !isValid) { |
| 57 | + item.title = '对不起,您没有该操作权限!'; |
| 58 | + } |
| 59 | + return isValid; |
| 60 | + } |
| 61 | + if (isObject(auth)) { |
| 62 | + const isValid = verifyAuth(auth.perm); |
| 63 | + const isDisable = auth.effect !== 'delete'; |
| 64 | + item.disabled ??= !isValid && isDisable; |
| 65 | + if (item.disabled && !isValid) { |
| 66 | + item.title = '对不起,您没有该操作权限!'; |
| 67 | + } |
| 68 | + return isValid || isDisable; |
| 69 | + } |
| 70 | + }) |
| 71 | + .map((item, index) => { |
| 72 | + const onClick = item.onClick; |
28 | 73 |
|
29 | | - const actionFilters = computed(() => { |
30 | | - return props.actions.filter((item) => { |
31 | | - const auth = item.auth; |
| 74 | + if (isAsyncFunction(onClick)) { |
| 75 | + item.onClick = async () => { |
| 76 | + const key = getKey(item, index); |
| 77 | + loadingMap.value.set(key, true); |
| 78 | + await onClick(props.columnParams).finally(() => { |
| 79 | + loadingMap.value.delete(key); |
| 80 | + }); |
| 81 | + }; |
| 82 | + } |
| 83 | + return item; |
| 84 | + }); |
| 85 | + }); |
32 | 86 |
|
33 | | - if (Object.is(auth, undefined)) { |
34 | | - return true; |
35 | | - } |
36 | | - if (isString(auth)) { |
37 | | - const isValid = verifyAuth(auth); |
38 | | - item.disabled ??= !isValid; |
39 | | - if (item.disabled && !isValid) { |
40 | | - item.title = '对不起,您没有该操作权限!'; |
41 | | - } |
42 | | - return isValid; |
43 | | - } |
44 | | - if (isObject(auth)) { |
45 | | - const isValid = verifyAuth(auth.perm); |
46 | | - const isDisable = auth.effect !== 'delete'; |
47 | | - item.disabled ??= !isValid && isDisable; |
48 | | - if (item.disabled && !isValid) { |
49 | | - item.title = '对不起,您没有该操作权限!'; |
50 | | - } |
51 | | - return isValid || isDisable; |
52 | | - } |
53 | | - }); |
| 87 | + const getKey = (actionItem: ActionItem, index: number) => { |
| 88 | + return `${props.rowKey}${index}${actionItem.label}`; |
| 89 | + }; |
| 90 | +
|
| 91 | + return { |
| 92 | + actionFilters, |
| 93 | + loadingMap, |
| 94 | + getKey, |
| 95 | + }; |
| 96 | + }, |
54 | 97 | }); |
55 | 98 | </script> |
0 commit comments