From cd86c19281f9c25b554b18c07c483914de2a7861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=9D=A4?= <1514100951@qq.com> Date: Tue, 20 Feb 2024 02:10:25 +0000 Subject: [PATCH] =?UTF-8?q?update=20src/components/Table/src/hooks/useRowS?= =?UTF-8?q?election.ts.=20=E8=A1=A8=E6=A0=BC=20rowKey=20=E4=B8=BA=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=97=B6,=E8=8E=B7=E5=8F=96getSelectRows?= =?UTF-8?q?=E4=B8=BA=E7=A9=BAbug,=20=E6=B7=BB=E5=8A=A0=E5=88=A4=E6=96=ADro?= =?UTF-8?q?wkey=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李坤 <1514100951@qq.com> --- .../Table/src/hooks/useRowSelection.ts | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/components/Table/src/hooks/useRowSelection.ts b/src/components/Table/src/hooks/useRowSelection.ts index dfd4b6e..7f5e427 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; -- Gitee