From 7cf58a4d2fff6b9cba9029d4d43ba9744dbef864 Mon Sep 17 00:00:00 2001 From: Ben Lin <maobin001@msn.com> Date: 星期四, 18 七月 2024 15:49:03 +0800 Subject: [PATCH] 工单更新 --- src/components/Table/src/hooks/useRowSelection.ts | 93 +++++++++++++++++++++++++++++++++++++++------- 1 files changed, 78 insertions(+), 15 deletions(-) diff --git a/src/components/Table/src/hooks/useRowSelection.ts b/src/components/Table/src/hooks/useRowSelection.ts index ed19b07..a58f267 100644 --- a/src/components/Table/src/hooks/useRowSelection.ts +++ b/src/components/Table/src/hooks/useRowSelection.ts @@ -1,16 +1,18 @@ -import { isFunction } from '/@/utils/is'; +import { isFunction } 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'; import { omit } from 'lodash-es'; -import { findNodeAll } from '/@/utils/helper/treeHelper'; +import { findNodeAll } from '@/utils/helper/treeHelper'; +import type { Key } from 'ant-design-vue/lib/table/interface'; +import { parseRowKey, parseRowKeyValue } from '../helper'; export function useRowSelection( propsRef: ComputedRef<BasicTableProps>, tableData: Ref<Recordable[]>, emit: EmitType, ) { - const selectedRowKeysRef = ref<string[]>([]); + const selectedRowKeysRef = ref<Key[]>([]); const selectedRowRef = ref<Recordable[]>([]); const getRowSelectionRef = computed((): TableRowSelection | null => { @@ -21,8 +23,55 @@ return { selectedRowKeys: unref(selectedRowKeysRef), - onChange: (selectedRowKeys: string[]) => { - setSelectedRowKeys(selectedRowKeys); + onChange: (selectedRowKeys: Key[], selectedRows: any[], isClickCustomRow?: boolean) => { + if (isClickCustomRow) { + // 鐐瑰嚮琛岃Е鍙� + + // 缁存寔澶栭儴瀹氫箟鐨� onChange 鍥炶皟 + rowSelection.onChange?.(selectedRowKeys, selectedRows); + } else { + // 鐐瑰嚮 checkbox/radiobox 瑙﹀彂 + + // 鍙栧嚭銆愬綋鍓嶉〉銆戞墍鏈� keyValues + const currentPageKeys = tableData.value.map((o) => parseRowKeyValue(unref(getRowKey), o)); + + // 浠庛�愭墍鏈夊垎椤点�戝凡閫夌殑 keyValues锛屼笖灞炰簬銆愬綋鍓嶉〉銆戠殑閮ㄥ垎 + for (const selectedKey of selectedRowKeysRef.value.filter((k) => + currentPageKeys.includes(k), + )) { + // 鍒ゆ柇鏄惁宸茬粡涓嶅瓨鍦ㄤ簬銆愬綋鍓嶉〉銆� + if (selectedRowKeys.findIndex((k) => k === selectedKey) < 0) { + // 涓嶅瓨鍦� = 鍙栨秷鍕鹃�� + const removeIndex = selectedRowKeysRef.value.findIndex((k) => k === selectedKey); + if (removeIndex > -1) { + // 鍙栨秷鍕鹃�� + selectedRowKeysRef.value.splice(removeIndex, 1); + selectedRowRef.value.splice(removeIndex, 1); + } + } + } + + // 瀛樺湪浜庛�愬綋鍓嶉〉銆戯紝浣嗕笉瀛樺湪浜庛�愭墍鏈夊垎椤点�戯紝鍒欒涓烘槸鏂板鐨� + for (const selectedKey of selectedRowKeys) { + const existIndex = selectedRowKeysRef.value.findIndex((k) => k === selectedKey); + if (existIndex < 0) { + // 鏂板鍕鹃�� + selectedRowKeysRef.value.push(selectedKey); + const record = selectedRows.find( + (o) => parseRowKeyValue(unref(getRowKey), o) === selectedKey, + ); + if (record) { + selectedRowRef.value.push(record); + } + } + } + + // 璧嬪�艰皟鏁磋繃鐨勫�� + setSelectedRowKeys(selectedRowKeysRef.value); + + // 缁存寔澶栭儴瀹氫箟鐨刼nChange鍥炶皟 + rowSelection.onChange?.(selectedRowKeysRef.value, selectedRowRef.value); + } }, ...omit(rowSelection, ['onChange']), }; @@ -30,7 +79,7 @@ watch( () => unref(propsRef).rowSelection?.selectedRowKeys, - (v: string[]) => { + (v?: Key[]) => { setSelectedRowKeys(v); }, ); @@ -42,7 +91,7 @@ const { rowSelection } = unref(propsRef); if (rowSelection) { const { onChange } = rowSelection; - if (onChange && isFunction(onChange)) onChange(getSelectRowKeys(), getSelectRows()); + if (onChange && isFunction(onChange)) onChange(getSelectRowKeys(), getSelectRows(), true); } emit('selection-change', { keys: getSelectRowKeys(), @@ -62,25 +111,39 @@ return unref(getAutoCreateKey) ? ROW_KEY : rowKey; }); - function setSelectedRowKeys(rowKeys: string[]) { - selectedRowKeysRef.value = rowKeys; + function setSelectedRowKeys(keyValues?: Key[]) { + selectedRowKeysRef.value = keyValues || []; + const rows = toRaw(unref(tableData)).concat(toRaw(unref(selectedRowRef))); const allSelectedRows = findNodeAll( - toRaw(unref(tableData)).concat(toRaw(unref(selectedRowRef))), - (item) => rowKeys?.includes(item[unref(getRowKey) as string]), + rows, + (item) => keyValues?.includes(parseRowKeyValue(unref(getRowKey), item)), { children: propsRef.value.childrenColumnName ?? 'children', }, ); const trueSelectedRows: any[] = []; - rowKeys?.forEach((key: string) => { - const found = allSelectedRows.find((item) => item[unref(getRowKey) as string] === key); - found && trueSelectedRows.push(found); + keyValues?.forEach((keyValue: Key) => { + const found = allSelectedRows.find( + (item) => parseRowKeyValue(unref(getRowKey), item) === keyValue, + ); + if (found) { + trueSelectedRows.push(found); + } else { + // 璺ㄩ〉鐨勬椂鍊欙紝闈炴湰椤垫暟鎹棤娉曞緱鍒帮紝鏆傚姝ゅ鐞� + // tableData or selectedRowRef 鎬绘湁鏁版嵁 + if (rows[0]) { + trueSelectedRows.push({ [parseRowKey(unref(getRowKey), rows[0])]: keyValue }); + } + } }); selectedRowRef.value = trueSelectedRows; } function setSelectedRows(rows: Recordable[]) { selectedRowRef.value = rows; + selectedRowKeysRef.value = selectedRowRef.value.map((o) => + parseRowKeyValue(unref(getRowKey), o), + ); } function clearSelectedRowKeys() { @@ -88,7 +151,7 @@ selectedRowKeysRef.value = []; } - function deleteSelectRowByKey(key: string) { + function deleteSelectRowByKey(key: Key) { const selectedRowKeys = unref(selectedRowKeysRef); const index = selectedRowKeys.findIndex((item) => item === key); if (index !== -1) { -- Gitblit v1.9.3