Table: fix reserve-selection not work (#16135)

* Table: fix reserve-selection not work

* fix
pull/16221/head
hetech 2019-06-25 16:57:30 +08:00 committed by Zhi Cun
parent ac00b28c74
commit 6dd3f632a0
2 changed files with 17 additions and 27 deletions

View File

@ -12,15 +12,15 @@ Watcher.prototype.mutations = {
// 没有使用 computed而是手动更新部分数据 https://github.com/vuejs/vue/issues/6660#issuecomment-331417140 // 没有使用 computed而是手动更新部分数据 https://github.com/vuejs/vue/issues/6660#issuecomment-331417140
this.updateCurrentRow(); this.updateCurrentRow();
this.updateExpandRows(); this.updateExpandRows();
if (!states.reserveSelection) { if (states.reserveSelection) {
this.assertRowKey();
this.updateSelectionByRowKey();
} else {
if (dataInstanceChanged) { if (dataInstanceChanged) {
this.clearSelection(); this.clearSelection();
} else { } else {
this.cleanSelection(); this.cleanSelection();
} }
} else {
this.assertRowKey();
this.updateSelectionByRowKey();
} }
this.updateAllSelected(); this.updateAllSelected();

View File

@ -127,18 +127,15 @@ export default Vue.extend({
const states = this.states; const states = this.states;
states.isAllSelected = false; states.isAllSelected = false;
const oldSelection = states.selection; const oldSelection = states.selection;
if (states.selection.length) { if (oldSelection.length) {
states.selection = []; states.selection = [];
} this.table.$emit('selection-change', []);
if (oldSelection.length > 0) {
this.table.$emit('selection-change', states.selection ? states.selection.slice() : []);
} }
}, },
cleanSelection() { cleanSelection() {
const selection = this.states.selection || []; const states = this.states;
const data = this.states.data; const { data, rowKey, selection } = states;
const rowKey = this.states.rowKey;
let deleted; let deleted;
if (rowKey) { if (rowKey) {
deleted = []; deleted = [];
@ -150,24 +147,19 @@ export default Vue.extend({
} }
} }
} else { } else {
deleted = selection.filter((item) => { deleted = selection.filter(item => data.indexOf(item) === -1);
return data.indexOf(item) === -1;
});
} }
deleted.forEach((deletedItem) => {
selection.splice(selection.indexOf(deletedItem), 1);
});
if (deleted.length) { if (deleted.length) {
this.table.$emit('selection-change', selection ? selection.slice() : []); const newSelection = selection.filter(item => deleted.indexOf(item) === -1);
states.selection = newSelection;
this.table.$emit('selection-change', newSelection.slice());
} }
}, },
toggleRowSelection(row, selected, emitChange = true) { toggleRowSelection(row, selected, emitChange = true) {
const changed = toggleRowStatus(this.states.selection, row, selected); const changed = toggleRowStatus(this.states.selection, row, selected);
if (changed) { if (changed) {
const newSelection = this.states.selection ? this.states.selection.slice() : []; const newSelection = (this.states.selection || []).slice();
// 调用 API 修改选中值,不触发 select 事件 // 调用 API 修改选中值,不触发 select 事件
if (emitChange) { if (emitChange) {
this.table.$emit('select', newSelection, row); this.table.$emit('select', newSelection, row);
@ -207,17 +199,15 @@ export default Vue.extend({
updateSelectionByRowKey() { updateSelectionByRowKey() {
const states = this.states; const states = this.states;
const { selection, rowKey, data = [] } = states; const { selection, rowKey, data } = states;
const selectedMap = getKeysMap(selection, rowKey); const selectedMap = getKeysMap(selection, rowKey);
// TODO这里的代码可以优化 data.forEach(row => {
states.selection = data.reduce((prev, row) => {
const rowId = getRowIdentity(row, rowKey); const rowId = getRowIdentity(row, rowKey);
const rowInfo = selectedMap[rowId]; const rowInfo = selectedMap[rowId];
if (rowInfo) { if (rowInfo) {
prev.push(row); selection[rowInfo.index] = row;
} }
return prev; });
}, []);
}, },
updateAllSelected() { updateAllSelected() {