提交 | 用户 | age
|
391da9
|
1 |
import type { ComputedRef, Ref } from 'vue'; |
V |
2 |
import type { BasicTableProps } from '../types/table'; |
|
3 |
import { computed, unref, ref, toRaw } from 'vue'; |
|
4 |
import { ROW_KEY } from '../const'; |
|
5 |
|
|
6 |
export function useTableExpand( |
|
7 |
propsRef: ComputedRef<BasicTableProps>, |
|
8 |
tableData: Ref<Recordable[]>, |
56a966
|
9 |
emit: EmitType, |
391da9
|
10 |
) { |
d3fd22
|
11 |
const expandedRowKeys = ref<(string | number)[]>([]); |
391da9
|
12 |
|
V |
13 |
const getAutoCreateKey = computed(() => { |
|
14 |
return unref(propsRef).autoCreateKey && !unref(propsRef).rowKey; |
|
15 |
}); |
|
16 |
|
|
17 |
const getRowKey = computed(() => { |
|
18 |
const { rowKey } = unref(propsRef); |
|
19 |
return unref(getAutoCreateKey) ? ROW_KEY : rowKey; |
|
20 |
}); |
|
21 |
|
|
22 |
const getExpandOption = computed(() => { |
|
23 |
const { isTreeTable } = unref(propsRef); |
|
24 |
if (!isTreeTable) return {}; |
|
25 |
|
|
26 |
return { |
|
27 |
expandedRowKeys: unref(expandedRowKeys), |
|
28 |
onExpandedRowsChange: (keys: string[]) => { |
|
29 |
expandedRowKeys.value = keys; |
|
30 |
emit('expanded-rows-change', keys); |
|
31 |
}, |
|
32 |
}; |
|
33 |
}); |
|
34 |
|
|
35 |
function expandAll() { |
|
36 |
const keys = getAllKeys(); |
|
37 |
expandedRowKeys.value = keys; |
|
38 |
} |
|
39 |
|
e656b5
|
40 |
function collapseAll() { |
Z |
41 |
expandedRowKeys.value = []; |
|
42 |
} |
|
43 |
|
d3fd22
|
44 |
function expandRows(keys: (string | number)[]) { |
1dc6fa
|
45 |
// use row ID expands the specified table row |
GT |
46 |
const { isTreeTable } = unref(propsRef); |
|
47 |
if (!isTreeTable) return; |
|
48 |
expandedRowKeys.value = [...expandedRowKeys.value, ...keys]; |
e656b5
|
49 |
} |
Z |
50 |
|
|
51 |
function collapseRows(keys: (string | number)[]) { |
|
52 |
// use row ID collapses the specified table row |
|
53 |
const { isTreeTable } = unref(propsRef); |
|
54 |
if (!isTreeTable) return; |
|
55 |
expandedRowKeys.value = unref(expandedRowKeys).filter((key) => !keys.includes(key)); |
1dc6fa
|
56 |
} |
GT |
57 |
|
391da9
|
58 |
function getAllKeys(data?: Recordable[]) { |
V |
59 |
const keys: string[] = []; |
|
60 |
const { childrenColumnName } = unref(propsRef); |
|
61 |
toRaw(data || unref(tableData)).forEach((item) => { |
|
62 |
keys.push(item[unref(getRowKey) as string]); |
|
63 |
const children = item[childrenColumnName || 'children']; |
|
64 |
if (children?.length) { |
|
65 |
keys.push(...getAllKeys(children)); |
|
66 |
} |
|
67 |
}); |
|
68 |
return keys; |
|
69 |
} |
|
70 |
|
e656b5
|
71 |
return { getExpandOption, expandAll, collapseAll, expandRows, collapseRows }; |
391da9
|
72 |
} |