Skip to content

Commit 292b4e8

Browse files
ziyoungluckyCao
authored andcommitted
Table: fix current-row-key and select event bug (#15983)
1 parent b06bcd3 commit 292b4e8

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

packages/table/src/store/current.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,63 @@ export default {
55
data() {
66
return {
77
states: {
8-
current: null
8+
// 不可响应的,设置 currentRowKey 时,data 不一定存在,也许无法算出正确的 currentRow
9+
// 把该值缓存一下,当用户点击修改 currentRow 时,把该值重置为 null
10+
_currentRowKey: null,
11+
currentRow: null
912
}
1013
};
1114
},
1215

1316
methods: {
1417
setCurrentRowKey(key) {
1518
this.assertRowKey();
19+
this.states._currentRowKey = key;
20+
this.setCurrentRowByKey(key);
21+
},
22+
23+
restoreCurrentRowKey() {
24+
this.states._currentRowKey = null;
25+
},
1626

27+
setCurrentRowByKey(key) {
1728
const { states } = this;
1829
const { data = [], rowKey } = states;
19-
const currentRow = arrayFind(data, item => getRowIdentity(item, rowKey) === key);
20-
states.currentRow = currentRow ? currentRow : null;
30+
let currentRow = null;
31+
if (rowKey) {
32+
currentRow = arrayFind(data, item => getRowIdentity(item, rowKey) === key);
33+
}
34+
states.currentRow = currentRow;
2135
},
2236

23-
updateCurrentRow() {
37+
updateCurrentRow(currentRow) {
2438
const { states, table } = this;
25-
const { rowKey } = states;
39+
const { rowKey, _currentRowKey } = states;
2640
// data 为 null 时,结构时的默认值会被忽略
2741
const data = states.data || [];
2842
const oldCurrentRow = states.currentRow;
2943

30-
// 当 currentRow 不在 data 中时尝试更新数据
31-
if (data.indexOf(oldCurrentRow) === -1 && oldCurrentRow) {
32-
let newCurrentRow = null;
33-
if (rowKey) {
34-
newCurrentRow = arrayFind(data, item => {
35-
return getRowIdentity(item, rowKey) === getRowIdentity(oldCurrentRow, rowKey);
36-
});
44+
if (currentRow) {
45+
this.restoreCurrentRowKey();
46+
states.currentRow = currentRow;
47+
if (oldCurrentRow !== currentRow) {
48+
this.table.$emit('current-change', currentRow, oldCurrentRow);
3749
}
38-
states.currentRow = newCurrentRow;
39-
if (newCurrentRow !== oldCurrentRow) {
40-
table.$emit('current-change', null, oldCurrentRow);
50+
} else {
51+
// 当 currentRow 不在 data 中时尝试更新数据
52+
if (data.indexOf(oldCurrentRow) === -1 && oldCurrentRow) {
53+
this.restoreCurrentRowKey();
54+
if (rowKey) {
55+
const currentRowKey = getRowIdentity(oldCurrentRow, rowKey);
56+
this.setCurrentRowByKey(currentRowKey);
57+
} else {
58+
states.currentRow = null;
59+
}
60+
if (states.currentRow !== oldCurrentRow) {
61+
table.$emit('current-change', null, oldCurrentRow);
62+
}
63+
} else if (_currentRowKey) {
64+
this.setCurrentRowByKey(_currentRowKey);
4165
}
4266
}
4367
}

packages/table/src/store/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,7 @@ Watcher.prototype.mutations = {
130130
},
131131

132132
setCurrentRow(states, row) {
133-
const oldCurrentRow = states.currentRow;
134-
states.currentRow = row;
135-
136-
if (oldCurrentRow !== row) {
137-
this.table.$emit('current-change', row, oldCurrentRow);
138-
}
133+
this.updateCurrentRow(row);
139134
}
140135
};
141136

packages/table/src/store/watcher.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ export default Vue.extend({
6868
sortProp: null,
6969
sortOrder: null,
7070

71-
hoverRow: null,
72-
currentRow: null
71+
hoverRow: null
7372
}
7473
};
7574
},
@@ -165,11 +164,14 @@ export default Vue.extend({
165164
}
166165
},
167166

168-
toggleRowSelection(row, selected) {
167+
toggleRowSelection(row, selected, emitChange = true) {
169168
const changed = toggleRowStatus(this.states.selection, row, selected);
170169
if (changed) {
171170
const newSelection = this.states.selection ? this.states.selection.slice() : [];
172-
this.table.$emit('select', newSelection, row);
171+
// 调用 API 修改选中值,不触发 select 事件
172+
if (emitChange) {
173+
this.table.$emit('select', newSelection, row);
174+
}
173175
this.table.$emit('selection-change', newSelection);
174176
}
175177
},
@@ -296,7 +298,6 @@ export default Vue.extend({
296298
});
297299

298300
states.filteredData = data;
299-
// states.data = data;
300301
},
301302

302303
execSort() {

packages/table/src/table.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@
355355
},
356356
357357
toggleRowSelection(row, selected) {
358-
this.store.toggleRowSelection(row, selected);
358+
this.store.toggleRowSelection(row, selected, false);
359359
this.store.updateAllSelected();
360360
},
361361
@@ -585,8 +585,12 @@
585585
}
586586
},
587587
588-
currentRowKey(newVal) {
589-
this.store.setCurrentRowKey(newVal);
588+
currentRowKey: {
589+
immediate: true,
590+
handler(value) {
591+
if (!this.rowKey) return;
592+
this.store.setCurrentRowKey(value);
593+
}
590594
},
591595
592596
data: {

0 commit comments

Comments
 (0)