Skip to content

Commit 6013689

Browse files
committed
fix(table): get the selected rows of the table correctly
1 parent 500900a commit 6013689

File tree

8 files changed

+61
-20
lines changed

8 files changed

+61
-20
lines changed

CHANGELOG.zh_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
### 🐛 Bug Fixes
1414

1515
- 修复验证码组件警告问题
16+
- 修复表格不能正确的获取选中行
1617

1718
## 2.0.1 (2021-02-21)
1819

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"stylelint-config-standard": "^20.0.0",
9595
"stylelint-order": "^4.1.0",
9696
"ts-node": "^9.1.1",
97-
"typescript": "^4.2.2",
97+
"typescript": "4.1.5",
9898
"vite": "2.0.2",
9999
"vite-plugin-compression": "^0.2.2",
100100
"vite-plugin-html": "^2.0.2",

src/components/Table/src/BasicTable.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
],
9393
setup(props, { attrs, emit, slots }) {
9494
const tableElRef = ref<ComponentRef>(null);
95+
const tableData = ref<Recordable[]>([]);
9596
9697
const wrapRef = ref<Nullable<HTMLDivElement>>(null);
9798
const innerPropsRef = ref<Partial<BasicTableProps>>();
@@ -120,7 +121,7 @@
120121
getSelectRowKeys,
121122
deleteSelectRowByKey,
122123
setSelectedRowKeys,
123-
} = useRowSelection(getProps, emit);
124+
} = useRowSelection(getProps, tableData, emit);
124125
125126
const {
126127
handleTableChange,
@@ -135,6 +136,7 @@
135136
} = useDataSource(
136137
getProps,
137138
{
139+
tableData,
138140
getPaginationInfo,
139141
setLoading,
140142
setPagination,

src/components/Table/src/hooks/useCustomRow.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export function useCustomRow(
4545
if (!key) return;
4646

4747
const isCheckbox = rowSelection.type === 'checkbox';
48-
4948
if (isCheckbox) {
5049
if (!keys.includes(key)) {
5150
setSelectedRowKeys([...keys, key]);

src/components/Table/src/hooks/useDataSource.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import type { BasicTableProps, FetchParams, SorterResult } from '../types/table';
22
import type { PaginationProps } from '../types/pagination';
33

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';
515

616
import { useTimeoutFn } from '/@/hooks/core/useTimeout';
717

@@ -17,6 +27,7 @@ interface ActionType {
1727
setLoading: (loading: boolean) => void;
1828
getFieldsValue: () => Recordable;
1929
clearSelectedRowKeys: () => void;
30+
tableData: Ref<Recordable[]>;
2031
}
2132

2233
interface SearchState {
@@ -31,6 +42,7 @@ export function useDataSource(
3142
setLoading,
3243
getFieldsValue,
3344
clearSelectedRowKeys,
45+
tableData,
3446
}: ActionType,
3547
emit: EmitType
3648
) {
@@ -45,6 +57,10 @@ export function useDataSource(
4557
// !api && dataSource && (dataSourceRef.value = dataSource);
4658
// });
4759

60+
watchEffect(() => {
61+
tableData.value = unref(dataSourceRef);
62+
});
63+
4864
watch(
4965
() => unref(propsRef).dataSource,
5066
() => {

src/components/Table/src/hooks/useRowSelection.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type { BasicTableProps, TableRowSelection } from '../types/table';
22

3-
import { computed, ref, unref, ComputedRef } from 'vue';
3+
import { computed, ref, unref, ComputedRef, Ref, toRaw } from 'vue';
4+
import { ROW_KEY } from '../const';
45

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+
) {
711
const selectedRowKeysRef = ref<string[]>([]);
812
const selectedRowRef = ref<Recordable[]>([]);
913

@@ -27,8 +31,26 @@ export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, emit: Em
2731
};
2832
});
2933

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+
3043
function setSelectedRowKeys(rowKeys: string[]) {
3144
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;
3254
}
3355

3456
function clearSelectedRowKeys() {
@@ -65,5 +87,6 @@ export function useRowSelection(propsRef: ComputedRef<BasicTableProps>, emit: Em
6587
setSelectedRowKeys,
6688
clearSelectedRowKeys,
6789
deleteSelectRowByKey,
90+
setSelectedRows,
6891
};
6992
}

src/components/Table/src/hooks/useTable.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { PaginationProps } from '../types/pagination';
33
import type { DynamicProps } from '/@/types/utils';
44
import { getDynamicProps } from '/@/utils';
55

6-
import { ref, onUnmounted, unref, watch } from 'vue';
6+
import { ref, onUnmounted, unref, watch, toRaw } from 'vue';
77
import { isProdMode } from '/@/utils/env';
88
import { isInSetup } from '/@/utils/helper/vueHelper';
99
import { error } from '/@/utils/log';
@@ -77,11 +77,11 @@ export function useTable(
7777
getTableInstance().setLoading(loading);
7878
},
7979
getDataSource: () => {
80-
return getTableInstance().getDataSource();
80+
return toRaw(getTableInstance().getDataSource());
8181
},
8282
getColumns: ({ ignoreIndex = false }: { ignoreIndex?: boolean } = {}) => {
8383
const columns = getTableInstance().getColumns({ ignoreIndex }) || [];
84-
return columns;
84+
return toRaw(columns);
8585
},
8686
setColumns: (columns: BasicColumn[]) => {
8787
getTableInstance().setColumns(columns);
@@ -96,10 +96,10 @@ export function useTable(
9696
getTableInstance().deleteSelectRowByKey(key);
9797
},
9898
getSelectRowKeys: () => {
99-
return getTableInstance().getSelectRowKeys();
99+
return toRaw(getTableInstance().getSelectRowKeys());
100100
},
101101
getSelectRows: () => {
102-
return getTableInstance().getSelectRows();
102+
return toRaw(getTableInstance().getSelectRows());
103103
},
104104
clearSelectedRowKeys: () => {
105105
getTableInstance().clearSelectedRowKeys();
@@ -111,16 +111,16 @@ export function useTable(
111111
return getTableInstance().getPaginationRef();
112112
},
113113
getSize: () => {
114-
return getTableInstance().getSize();
114+
return toRaw(getTableInstance().getSize());
115115
},
116116
updateTableData: (index: number, key: string, value: any) => {
117117
return getTableInstance().updateTableData(index, key, value);
118118
},
119119
getRowSelection: () => {
120-
return getTableInstance().getRowSelection();
120+
return toRaw(getTableInstance().getRowSelection());
121121
},
122122
getCacheColumns: () => {
123-
return getTableInstance().getCacheColumns();
123+
return toRaw(getTableInstance().getCacheColumns());
124124
},
125125
getForm: () => {
126126
return (unref(formRef) as unknown) as FormActionType;
@@ -129,7 +129,7 @@ export function useTable(
129129
getTableInstance().setShowPagination(show);
130130
},
131131
getShowPagination: () => {
132-
return getTableInstance().getShowPagination();
132+
return toRaw(getTableInstance().getShowPagination());
133133
},
134134
};
135135

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8610,10 +8610,10 @@ typedarray-to-buffer@^3.1.5:
86108610
dependencies:
86118611
is-typedarray "^1.0.0"
86128612

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==
86178617

86188618
uglify-js@^3.1.4:
86198619
version "3.12.5"

0 commit comments

Comments
 (0)