Commit 601368921f075aa1870d1c3ce8f4a8330260206a

Authored by Vben
1 parent 500900ab

fix(table): get the selected rows of the table correctly

CHANGELOG.zh_CN.md
... ... @@ -13,6 +13,7 @@
13 13 ### 🐛 Bug Fixes
14 14  
15 15 - 修复验证码组件警告问题
  16 +- 修复表格不能正确的获取选中行
16 17  
17 18 ## 2.0.1 (2021-02-21)
18 19  
... ...
package.json
... ... @@ -94,7 +94,7 @@
94 94 "stylelint-config-standard": "^20.0.0",
95 95 "stylelint-order": "^4.1.0",
96 96 "ts-node": "^9.1.1",
97   - "typescript": "^4.2.2",
  97 + "typescript": "4.1.5",
98 98 "vite": "2.0.2",
99 99 "vite-plugin-compression": "^0.2.2",
100 100 "vite-plugin-html": "^2.0.2",
... ...
src/components/Table/src/BasicTable.vue
... ... @@ -92,6 +92,7 @@
92 92 ],
93 93 setup(props, { attrs, emit, slots }) {
94 94 const tableElRef = ref<ComponentRef>(null);
  95 + const tableData = ref<Recordable[]>([]);
95 96  
96 97 const wrapRef = ref<Nullable<HTMLDivElement>>(null);
97 98 const innerPropsRef = ref<Partial<BasicTableProps>>();
... ... @@ -120,7 +121,7 @@
120 121 getSelectRowKeys,
121 122 deleteSelectRowByKey,
122 123 setSelectedRowKeys,
123   - } = useRowSelection(getProps, emit);
  124 + } = useRowSelection(getProps, tableData, emit);
124 125  
125 126 const {
126 127 handleTableChange,
... ... @@ -135,6 +136,7 @@
135 136 } = useDataSource(
136 137 getProps,
137 138 {
  139 + tableData,
138 140 getPaginationInfo,
139 141 setLoading,
140 142 setPagination,
... ...
src/components/Table/src/hooks/useCustomRow.ts
... ... @@ -45,7 +45,6 @@ export function useCustomRow(
45 45 if (!key) return;
46 46  
47 47 const isCheckbox = rowSelection.type === 'checkbox';
48   -
49 48 if (isCheckbox) {
50 49 if (!keys.includes(key)) {
51 50 setSelectedRowKeys([...keys, key]);
... ...
src/components/Table/src/hooks/useDataSource.ts
1 1 import type { BasicTableProps, FetchParams, SorterResult } from '../types/table';
2 2 import type { PaginationProps } from '../types/pagination';
3 3  
4   -import { ref, unref, ComputedRef, computed, onMounted, watch, reactive } from 'vue';
  4 +import {
  5 + ref,
  6 + unref,
  7 + ComputedRef,
  8 + computed,
  9 + onMounted,
  10 + watch,
  11 + reactive,
  12 + Ref,
  13 + watchEffect,
  14 +} from 'vue';
5 15  
6 16 import { useTimeoutFn } from '/@/hooks/core/useTimeout';
7 17  
... ... @@ -17,6 +27,7 @@ interface ActionType {
17 27 setLoading: (loading: boolean) => void;
18 28 getFieldsValue: () => Recordable;
19 29 clearSelectedRowKeys: () => void;
  30 + tableData: Ref<Recordable[]>;
20 31 }
21 32  
22 33 interface SearchState {
... ... @@ -31,6 +42,7 @@ export function useDataSource(
31 42 setLoading,
32 43 getFieldsValue,
33 44 clearSelectedRowKeys,
  45 + tableData,
34 46 }: ActionType,
35 47 emit: EmitType
36 48 ) {
... ... @@ -45,6 +57,10 @@ export function useDataSource(
45 57 // !api && dataSource && (dataSourceRef.value = dataSource);
46 58 // });
47 59  
  60 + watchEffect(() => {
  61 + tableData.value = unref(dataSourceRef);
  62 + });
  63 +
48 64 watch(
49 65 () => unref(propsRef).dataSource,
50 66 () => {
... ...
src/components/Table/src/hooks/useRowSelection.ts
1 1 import type { BasicTableProps, TableRowSelection } from '../types/table';
2 2  
3   -import { computed, ref, unref, ComputedRef } from 'vue';
  3 +import { computed, ref, unref, ComputedRef, Ref, toRaw } from 'vue';
  4 +import { ROW_KEY } from '../const';
4 5  
5   -/* eslint-disable */
6   -export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, emit: EmitType) {
  6 +export function useRowSelection(
  7 + propsRef: ComputedRef<BasicTableProps>,
  8 + tableData: Ref<Recordable[]>,
  9 + emit: EmitType
  10 +) {
7 11 const selectedRowKeysRef = ref<string[]>([]);
8 12 const selectedRowRef = ref<Recordable[]>([]);
9 13  
... ... @@ -27,8 +31,26 @@ export function useRowSelection(propsRef: ComputedRef&lt;BasicTableProps&gt;, emit: Em
27 31 };
28 32 });
29 33  
  34 + const getAutoCreateKey = computed(() => {
  35 + return unref(propsRef).autoCreateKey && !unref(propsRef).rowKey;
  36 + });
  37 +
  38 + const getRowKey = computed(() => {
  39 + const { rowKey } = unref(propsRef);
  40 + return unref(getAutoCreateKey) ? ROW_KEY : rowKey;
  41 + });
  42 +
30 43 function setSelectedRowKeys(rowKeys: string[]) {
31 44 selectedRowKeysRef.value = rowKeys;
  45 +
  46 + const rows = toRaw(unref(tableData)).filter((item) =>
  47 + rowKeys.includes(item[unref(getRowKey) as string])
  48 + );
  49 + selectedRowRef.value = rows;
  50 + }
  51 +
  52 + function setSelectedRows(rows: Recordable[]) {
  53 + selectedRowRef.value = rows;
32 54 }
33 55  
34 56 function clearSelectedRowKeys() {
... ... @@ -65,5 +87,6 @@ export function useRowSelection(propsRef: ComputedRef&lt;BasicTableProps&gt;, emit: Em
65 87 setSelectedRowKeys,
66 88 clearSelectedRowKeys,
67 89 deleteSelectRowByKey,
  90 + setSelectedRows,
68 91 };
69 92 }
... ...
src/components/Table/src/hooks/useTable.ts
... ... @@ -3,7 +3,7 @@ import type { PaginationProps } from &#39;../types/pagination&#39;;
3 3 import type { DynamicProps } from '/@/types/utils';
4 4 import { getDynamicProps } from '/@/utils';
5 5  
6   -import { ref, onUnmounted, unref, watch } from 'vue';
  6 +import { ref, onUnmounted, unref, watch, toRaw } from 'vue';
7 7 import { isProdMode } from '/@/utils/env';
8 8 import { isInSetup } from '/@/utils/helper/vueHelper';
9 9 import { error } from '/@/utils/log';
... ... @@ -77,11 +77,11 @@ export function useTable(
77 77 getTableInstance().setLoading(loading);
78 78 },
79 79 getDataSource: () => {
80   - return getTableInstance().getDataSource();
  80 + return toRaw(getTableInstance().getDataSource());
81 81 },
82 82 getColumns: ({ ignoreIndex = false }: { ignoreIndex?: boolean } = {}) => {
83 83 const columns = getTableInstance().getColumns({ ignoreIndex }) || [];
84   - return columns;
  84 + return toRaw(columns);
85 85 },
86 86 setColumns: (columns: BasicColumn[]) => {
87 87 getTableInstance().setColumns(columns);
... ... @@ -96,10 +96,10 @@ export function useTable(
96 96 getTableInstance().deleteSelectRowByKey(key);
97 97 },
98 98 getSelectRowKeys: () => {
99   - return getTableInstance().getSelectRowKeys();
  99 + return toRaw(getTableInstance().getSelectRowKeys());
100 100 },
101 101 getSelectRows: () => {
102   - return getTableInstance().getSelectRows();
  102 + return toRaw(getTableInstance().getSelectRows());
103 103 },
104 104 clearSelectedRowKeys: () => {
105 105 getTableInstance().clearSelectedRowKeys();
... ... @@ -111,16 +111,16 @@ export function useTable(
111 111 return getTableInstance().getPaginationRef();
112 112 },
113 113 getSize: () => {
114   - return getTableInstance().getSize();
  114 + return toRaw(getTableInstance().getSize());
115 115 },
116 116 updateTableData: (index: number, key: string, value: any) => {
117 117 return getTableInstance().updateTableData(index, key, value);
118 118 },
119 119 getRowSelection: () => {
120   - return getTableInstance().getRowSelection();
  120 + return toRaw(getTableInstance().getRowSelection());
121 121 },
122 122 getCacheColumns: () => {
123   - return getTableInstance().getCacheColumns();
  123 + return toRaw(getTableInstance().getCacheColumns());
124 124 },
125 125 getForm: () => {
126 126 return (unref(formRef) as unknown) as FormActionType;
... ... @@ -129,7 +129,7 @@ export function useTable(
129 129 getTableInstance().setShowPagination(show);
130 130 },
131 131 getShowPagination: () => {
132   - return getTableInstance().getShowPagination();
  132 + return toRaw(getTableInstance().getShowPagination());
133 133 },
134 134 };
135 135  
... ...
yarn.lock
... ... @@ -8610,10 +8610,10 @@ typedarray-to-buffer@^3.1.5:
8610 8610 dependencies:
8611 8611 is-typedarray "^1.0.0"
8612 8612  
8613   -typescript@^4.2.2:
8614   - version "4.2.2"
8615   - resolved "https://registry.npmjs.org/typescript/-/typescript-4.2.2.tgz#1450f020618f872db0ea17317d16d8da8ddb8c4c"
8616   - integrity sha512-tbb+NVrLfnsJy3M59lsDgrzWIflR4d4TIUjz+heUnHZwdF7YsrMTKoRERiIvI2lvBG95dfpLxB21WZhys1bgaQ==
  8613 +typescript@4.1.5:
  8614 + version "4.1.5"
  8615 + resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
  8616 + integrity sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==
8617 8617  
8618 8618 uglify-js@^3.1.4:
8619 8619 version "3.12.5"
... ...