Skip to content

Commit ef66be9

Browse files
ziyounglzq4047
authored andcommitted
Table: fix reserve-selection not work (ElemeFE#16135)
* Table: fix reserve-selection not work * fix
1 parent fe9ad0e commit ef66be9

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

packages/table/src/store/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ Watcher.prototype.mutations = {
1212
// 没有使用 computed,而是手动更新部分数据 https://github.com/vuejs/vue/issues/6660#issuecomment-331417140
1313
this.updateCurrentRow();
1414
this.updateExpandRows();
15-
if (!states.reserveSelection) {
15+
if (states.reserveSelection) {
16+
this.assertRowKey();
17+
this.updateSelectionByRowKey();
18+
} else {
1619
if (dataInstanceChanged) {
1720
this.clearSelection();
1821
} else {
1922
this.cleanSelection();
2023
}
21-
} else {
22-
this.assertRowKey();
23-
this.updateSelectionByRowKey();
2424
}
2525
this.updateAllSelected();
2626

packages/table/src/store/watcher.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,15 @@ export default Vue.extend({
127127
const states = this.states;
128128
states.isAllSelected = false;
129129
const oldSelection = states.selection;
130-
if (states.selection.length) {
130+
if (oldSelection.length) {
131131
states.selection = [];
132-
}
133-
if (oldSelection.length > 0) {
134-
this.table.$emit('selection-change', states.selection ? states.selection.slice() : []);
132+
this.table.$emit('selection-change', []);
135133
}
136134
},
137135

138136
cleanSelection() {
139-
const selection = this.states.selection || [];
140-
const data = this.states.data;
141-
const rowKey = this.states.rowKey;
137+
const states = this.states;
138+
const { data, rowKey, selection } = states;
142139
let deleted;
143140
if (rowKey) {
144141
deleted = [];
@@ -150,24 +147,19 @@ export default Vue.extend({
150147
}
151148
}
152149
} else {
153-
deleted = selection.filter((item) => {
154-
return data.indexOf(item) === -1;
155-
});
150+
deleted = selection.filter(item => data.indexOf(item) === -1);
156151
}
157-
158-
deleted.forEach((deletedItem) => {
159-
selection.splice(selection.indexOf(deletedItem), 1);
160-
});
161-
162152
if (deleted.length) {
163-
this.table.$emit('selection-change', selection ? selection.slice() : []);
153+
const newSelection = selection.filter(item => deleted.indexOf(item) === -1);
154+
states.selection = newSelection;
155+
this.table.$emit('selection-change', newSelection.slice());
164156
}
165157
},
166158

167159
toggleRowSelection(row, selected, emitChange = true) {
168160
const changed = toggleRowStatus(this.states.selection, row, selected);
169161
if (changed) {
170-
const newSelection = this.states.selection ? this.states.selection.slice() : [];
162+
const newSelection = (this.states.selection || []).slice();
171163
// 调用 API 修改选中值,不触发 select 事件
172164
if (emitChange) {
173165
this.table.$emit('select', newSelection, row);
@@ -207,17 +199,15 @@ export default Vue.extend({
207199

208200
updateSelectionByRowKey() {
209201
const states = this.states;
210-
const { selection, rowKey, data = [] } = states;
202+
const { selection, rowKey, data } = states;
211203
const selectedMap = getKeysMap(selection, rowKey);
212-
// TODO:这里的代码可以优化
213-
states.selection = data.reduce((prev, row) => {
204+
data.forEach(row => {
214205
const rowId = getRowIdentity(row, rowKey);
215206
const rowInfo = selectedMap[rowId];
216207
if (rowInfo) {
217-
prev.push(row);
208+
selection[rowInfo.index] = row;
218209
}
219-
return prev;
220-
}, []);
210+
});
221211
},
222212

223213
updateAllSelected() {

0 commit comments

Comments
 (0)