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