Commit 7b1d0e2a authored by riho's avatar riho Committed by Zhi Cun

Table: make toggleAllSelection method an instance method(#14075)

parent 4a0e9262
...@@ -111,6 +111,35 @@ const TableStore = function(table, initialState = {}) { ...@@ -111,6 +111,35 @@ const TableStore = function(table, initialState = {}) {
selectOnIndeterminate: false selectOnIndeterminate: false
}; };
this._toggleAllSelection = debounce(10, function(states) {
const data = states.data || [];
if (data.length === 0) return;
const selection = this.states.selection;
// when only some rows are selected (but not all), select or deselect all of them
// depending on the value of selectOnIndeterminate
const value = states.selectOnIndeterminate
? !states.isAllSelected
: !(states.isAllSelected || selection.length);
let selectionChanged = false;
data.forEach((item, index) => {
if (states.selectable) {
if (states.selectable.call(null, item, index) && toggleRowSelection(states, item, value)) {
selectionChanged = true;
}
} else {
if (toggleRowSelection(states, item, value)) {
selectionChanged = true;
}
}
});
const table = this.table;
if (selectionChanged) {
table.$emit('selection-change', selection ? selection.slice() : []);
}
table.$emit('select-all', selection);
states.isAllSelected = value;
});
for (let prop in initialState) { for (let prop in initialState) {
if (initialState.hasOwnProperty(prop) && this.states.hasOwnProperty(prop)) { if (initialState.hasOwnProperty(prop) && this.states.hasOwnProperty(prop)) {
this.states[prop] = initialState[prop]; this.states[prop] = initialState[prop];
...@@ -335,36 +364,9 @@ TableStore.prototype.mutations = { ...@@ -335,36 +364,9 @@ TableStore.prototype.mutations = {
this.updateAllSelected(); this.updateAllSelected();
}, },
toggleAllSelection: debounce(10, function(states) { toggleAllSelection(state) {
const data = states.data || []; this._toggleAllSelection(state);
if (data.length === 0) return;
const selection = this.states.selection;
// when only some rows are selected (but not all), select or deselect all of them
// depending on the value of selectOnIndeterminate
const value = states.selectOnIndeterminate
? !states.isAllSelected
: !(states.isAllSelected || selection.length);
let selectionChanged = false;
data.forEach((item, index) => {
if (states.selectable) {
if (states.selectable.call(null, item, index) && toggleRowSelection(states, item, value)) {
selectionChanged = true;
} }
} else {
if (toggleRowSelection(states, item, value)) {
selectionChanged = true;
}
}
});
const table = this.table;
if (selectionChanged) {
table.$emit('selection-change', selection ? selection.slice() : []);
}
table.$emit('select-all', selection);
states.isAllSelected = value;
})
}; };
const doFlattenColumns = (columns) => { const doFlattenColumns = (columns) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment