diff --git a/src/components/Table/src/hooks/useRowSelection.ts b/src/components/Table/src/hooks/useRowSelection.ts index dfd4b6e714b634ed27ee3e51f4c1d8636c2b615b..7f5e4272190c684bf2d6ed3ddf3bdef79710a3fa 100644 --- a/src/components/Table/src/hooks/useRowSelection.ts +++ b/src/components/Table/src/hooks/useRowSelection.ts @@ -1,4 +1,4 @@ -import { isFunction } from '/@/utils/is'; +import { isFunction, isString } from '/@/utils/is'; import type { BasicTableProps, TableRowSelection } from '../types/table'; import { computed, ComputedRef, nextTick, Ref, ref, toRaw, unref, watch } from 'vue'; import { ROW_KEY } from '../const'; @@ -10,7 +10,7 @@ export function useRowSelection( tableData: Ref, emit: EmitType, ) { - const selectedRowKeysRef = ref([]); + const selectedRowKeysRef = ref([]); const selectedRowRef = ref([]); const getRowSelectionRef = computed((): TableRowSelection | null => { @@ -21,12 +21,10 @@ export function useRowSelection( return { selectedRowKeys: unref(selectedRowKeysRef), - preserveSelectedRowKeys: true, // 由 clearSelectedOnReload 选项控制是否保留选择项 - onChange: (selectedRowKeys: string[] | number[], selectedRows: any[]) => { + onChange: (selectedRowKeys: string[]) => { setSelectedRowKeys(selectedRowKeys); - if (rowSelection && rowSelection.onChange) { - rowSelection.onChange(selectedRowKeys, selectedRows); - } + // selectedRowKeysRef.value = selectedRowKeys; + // selectedRowRef.value = selectedRows; }, ...omit(rowSelection, ['onChange']), }; @@ -46,11 +44,9 @@ export function useRowSelection( const { rowSelection } = unref(propsRef); if (rowSelection) { const { onChange } = rowSelection; - if (onChange && isFunction(onChange)) { - onChange(getSelectRowKeys(), getSelectRows()); - } + if (onChange && isFunction(onChange)) onChange(getSelectRowKeys(), getSelectRows()); } - // 有数据时,再调用选择变更事件 + // ������ʱ���ٵ���ѡ�����¼� if (unref(tableData).length > 0) { emit('selection-change', { keys: getSelectRowKeys(), @@ -71,18 +67,32 @@ export function useRowSelection( return unref(getAutoCreateKey) ? ROW_KEY : rowKey; }); - function setSelectedRowKeys(rowKeys: string[] | number[]) { + function getKey(record: Recordable) { + const rowKey = unref(getRowKey); + + if (isString(rowKey)) { + return record[rowKey]; + } + + if (isFunction(rowKey)) { + return rowKey(record, null); + } + return null; + } + + function setSelectedRowKeys(rowKeys: string[]) { selectedRowKeysRef.value = rowKeys; + const allSelectedRows = findNodeAll( toRaw(unref(tableData)).concat(toRaw(unref(selectedRowRef))), - (item) => rowKeys.includes(item[unref(getRowKey) as string] as never), + (item) => rowKeys.includes(getKey(item)), { children: propsRef.value.childrenColumnName ?? 'children', }, ); const trueSelectedRows: any[] = []; - rowKeys.forEach((key: string | number) => { - const found = allSelectedRows.find((item) => item[unref(getRowKey) as string] === key); + rowKeys.forEach((key: string) => { + const found = allSelectedRows.find((item) => getKey(item) === key); found && trueSelectedRows.push(found); }); selectedRowRef.value = trueSelectedRows;