From 7d26ed0e19bf952e7c037b21bfd687759b46e851 Mon Sep 17 00:00:00 2001 From: Ben Lin <maobin001@msn.com> Date: 星期二, 25 六月 2024 19:21:33 +0800 Subject: [PATCH] 菜单更新 --- src/components/Table/src/hooks/useTableExpand.ts | 127 ++++++++++++++++++++++++++++++++---------- 1 files changed, 97 insertions(+), 30 deletions(-) diff --git a/src/components/Table/src/hooks/useTableExpand.ts b/src/components/Table/src/hooks/useTableExpand.ts index 2628365..7d61444 100644 --- a/src/components/Table/src/hooks/useTableExpand.ts +++ b/src/components/Table/src/hooks/useTableExpand.ts @@ -1,14 +1,16 @@ import type { ComputedRef, Ref } from 'vue'; import type { BasicTableProps } from '../types/table'; -import { computed, unref, ref, toRaw } from 'vue'; +import { computed, unref, ref, toRaw, nextTick } from 'vue'; import { ROW_KEY } from '../const'; +import { parseRowKeyValue } from '../helper'; +import type { Key } from 'ant-design-vue/lib/table/interface'; export function useTableExpand( propsRef: ComputedRef<BasicTableProps>, tableData: Ref<Recordable[]>, emit: EmitType, ) { - const expandedRowKeys = ref<string[]>([]); + const expandedRowKeys = ref<Key[]>([]); const getAutoCreateKey = computed(() => { return unref(propsRef).autoCreateKey && !unref(propsRef).rowKey; @@ -20,46 +22,111 @@ }); const getExpandOption = computed(() => { - const { isTreeTable } = unref(propsRef); - if (!isTreeTable) return {}; + const { isTreeTable, expandRowByClick } = unref(propsRef); + if (!isTreeTable && !expandRowByClick) return {}; return { expandedRowKeys: unref(expandedRowKeys), - onExpandedRowsChange: (keys: string[]) => { - expandedRowKeys.value = keys; - emit('expanded-rows-change', keys); + onExpandedRowsChange: (keyValues: string[]) => { + expandedRowKeys.value = keyValues; + emit('expanded-rows-change', keyValues); }, }; }); function expandAll() { - const keys = getAllKeys(); - expandedRowKeys.value = keys; - } - - function expandRows(keys: string[]) { - // use row ID expands the specified table row - const { isTreeTable } = unref(propsRef); - if (!isTreeTable) return; - expandedRowKeys.value = [...expandedRowKeys.value, ...keys]; - } - - function getAllKeys(data?: Recordable[]) { - const keys: string[] = []; - const { childrenColumnName } = unref(propsRef); - toRaw(data || unref(tableData)).forEach((item) => { - keys.push(item[unref(getRowKey) as string]); - const children = item[childrenColumnName || 'children']; - if (children?.length) { - keys.push(...getAllKeys(children)); - } - }); - return keys; + const keyValues = getAllKeys(); + expandedRowKeys.value = keyValues; } function collapseAll() { expandedRowKeys.value = []; } - return { getExpandOption, expandAll, expandRows, collapseAll }; + function expandRows(keyValues: Key[]) { + // use row ID expands the specified table row + const { isTreeTable, expandRowByClick } = unref(propsRef); + if (!isTreeTable && !expandRowByClick) return; + expandedRowKeys.value = [...expandedRowKeys.value, ...keyValues]; + } + + function collapseRows(keyValues: Key[]) { + // use row ID collapses the specified table row + const { isTreeTable, expandRowByClick } = unref(propsRef); + if (!isTreeTable && !expandRowByClick) return; + expandedRowKeys.value = unref(expandedRowKeys).filter( + (keyValue) => !keyValues.includes(keyValue), + ); + } + + function getAllKeys(data?: Recordable[]) { + const keyValues: Array<number | string> = []; + const { childrenColumnName } = unref(propsRef); + toRaw(data || unref(tableData)).forEach((item) => { + keyValues.push(parseRowKeyValue(unref(getRowKey), item)); + const children = item[childrenColumnName || 'children']; + if (children?.length) { + keyValues.push(...getAllKeys(children)); + } + }); + return keyValues; + } + + // 鑾峰彇灞曞紑璺緞 keyValues + function getKeyPaths( + records: Recordable[], + childrenColumnName: string, + keyValue: Key, + paths: Array<Key>, + ): boolean { + if ( + records.findIndex((record) => parseRowKeyValue(unref(getRowKey), record) === keyValue) > -1 + ) { + paths.push(keyValue); + return true; + } else { + for (const record of records) { + const children = record[childrenColumnName]; + if (Array.isArray(children) && getKeyPaths(children, childrenColumnName, keyValue, paths)) { + paths.push(parseRowKeyValue(unref(getRowKey), record)); + return true; + } + } + } + return false; + } + + // 鎵嬮鐞村睍寮� + function expandRowAccordion(keyValue: Key) { + const { childrenColumnName } = unref(propsRef); + const paths: Array<Key> = []; + getKeyPaths(tableData.value, childrenColumnName || 'children', keyValue, paths); + expandedRowKeys.value = paths; + } + + // 鐩戝惉灞曞紑浜嬩欢锛岀敤浜庢敮鎸佹墜椋庣惔灞曞紑鏁堟灉 + function handleTableExpand(expanded: boolean, record: Recordable) { + // 鎵嬮鐞村紑鍏� + // isTreeTable 鎴� expandRowByClick 鏃舵敮鎸� + // 灞曞紑鎿嶄綔 + if ( + propsRef.value.accordion && + (propsRef.value.isTreeTable || propsRef.value.expandRowByClick) && + expanded + ) { + nextTick(() => { + expandRowAccordion(parseRowKeyValue(unref(getRowKey), record)); + }); + } + } + + return { + getExpandOption, + expandAll, + collapseAll, + expandRows, + collapseRows, + expandRowAccordion, + handleTableExpand, + }; } -- Gitblit v1.9.3