mirror of https://github.com/ElemeFE/element
Table: let table compatible with vuex.
parent
c08acf713c
commit
0eacd76e22
|
@ -45,7 +45,7 @@
|
||||||
|
|
||||||
- Tabs 组件的 click 和 remove 事件回调参数从 name 改为事件对应的 tab 组件实例
|
- Tabs 组件的 click 和 remove 事件回调参数从 name 改为事件对应的 tab 组件实例
|
||||||
- 全屏 Loading 现在默认不再锁定屏幕滚动。如果需要的话,可添加 `lock` 修饰符
|
- 全屏 Loading 现在默认不再锁定屏幕滚动。如果需要的话,可添加 `lock` 修饰符
|
||||||
- Table 删除属性 fixedColumnCount, customCriteria, customBackgroundColors
|
- Table 删除属性 fixedColumnCount、customCriteria、customBackgroundColors、selectionMode
|
||||||
- Table 的 selectionchange、cellmouseenter、cellmouseleave、cellclick 事件更名为 selection-change、cell-mouse-enter、cell-mouse-leave、cell-click。
|
- Table 的 selectionchange、cellmouseenter、cellmouseleave、cellclick 事件更名为 selection-change、cell-mouse-enter、cell-mouse-leave、cell-click。
|
||||||
- Pagination 的 currentchange、sizechange 事件更名为 current-change、size-change
|
- Pagination 的 currentchange、sizechange 事件更名为 current-change、size-change
|
||||||
|
|
||||||
|
|
|
@ -917,6 +917,7 @@
|
||||||
| 方法名 | 说明 | 参数 |
|
| 方法名 | 说明 | 参数 |
|
||||||
| ---- | ---- | ---- |
|
| ---- | ---- | ---- |
|
||||||
| clearSelection | 清空用户的选择,当使用 reserve-selection 功能的时候,可能会需要使用此方法 | selection |
|
| clearSelection | 清空用户的选择,当使用 reserve-selection 功能的时候,可能会需要使用此方法 | selection |
|
||||||
|
| toggleRowSelection | 切换某一行的选中状态,如果使用了第二个参数,则是设置这一行选中与否(selected 为 true 则选中) | row, selected |
|
||||||
|
|
||||||
### Table-column Attributes
|
### Table-column Attributes
|
||||||
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
||||||
|
|
|
@ -27,14 +27,13 @@ const forced = {
|
||||||
headerTemplate: function(h) {
|
headerTemplate: function(h) {
|
||||||
return <el-checkbox
|
return <el-checkbox
|
||||||
nativeOn-click={ this.toggleAllSelection }
|
nativeOn-click={ this.toggleAllSelection }
|
||||||
domProps-value={ this.isAllSelected }
|
domProps-value={ this.isAllSelected } />;
|
||||||
on-input={ (value) => { this.$emit('allselectedchange', value); } } />;
|
|
||||||
},
|
},
|
||||||
template: function(h, { row, column, store, $index }) {
|
template: function(h, { row, column, store, $index }) {
|
||||||
return <el-checkbox
|
return <el-checkbox
|
||||||
domProps-value={ row.$selected }
|
domProps-value={ store.isSelected(row) }
|
||||||
disabled={ column.selectable ? !column.selectable.call(null, row, $index) : false }
|
disabled={ column.selectable ? !column.selectable.call(null, row, $index) : false }
|
||||||
on-input={ (value) => { row.$selected = value; store.commit('rowSelectedChanged', row); } } />;
|
on-input={ (value) => { store.commit('rowSelectedChanged', row); } } />;
|
||||||
},
|
},
|
||||||
sortable: false,
|
sortable: false,
|
||||||
resizable: false
|
resizable: false
|
||||||
|
|
|
@ -19,6 +19,39 @@ const sortData = (data, states) => {
|
||||||
return orderBy(data, states.sortProp, states.sortOrder, sortingColumn.sortMethod);
|
return orderBy(data, states.sortProp, states.sortOrder, sortingColumn.sortMethod);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getSelectedMap = function(states, rowKey) {
|
||||||
|
const selectionMap = {};
|
||||||
|
states.selection.forEach((row, index) => {
|
||||||
|
selectionMap[getRowIdentity(row, rowKey)] = { row, index };
|
||||||
|
});
|
||||||
|
return selectionMap;
|
||||||
|
};
|
||||||
|
|
||||||
|
const toggleRowSelection = function(states, row, selected) {
|
||||||
|
let changed = false;
|
||||||
|
const selection = states.selection;
|
||||||
|
const index = selection.indexOf(row);
|
||||||
|
if (typeof selected === 'undefined') {
|
||||||
|
if (index === -1) {
|
||||||
|
selection.push(row);
|
||||||
|
changed = true;
|
||||||
|
} else {
|
||||||
|
selection.splice(index, 1);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (selected && index === -1) {
|
||||||
|
selection.push(row);
|
||||||
|
changed = true;
|
||||||
|
} else if (!selected && index > -1) {
|
||||||
|
selection.splice(index, 1);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
};
|
||||||
|
|
||||||
const TableStore = function(table, initialState = {}) {
|
const TableStore = function(table, initialState = {}) {
|
||||||
if (!table) {
|
if (!table) {
|
||||||
throw new Error('Table is required.');
|
throw new Error('Table is required.');
|
||||||
|
@ -55,26 +88,21 @@ const TableStore = function(table, initialState = {}) {
|
||||||
TableStore.prototype.mutations = {
|
TableStore.prototype.mutations = {
|
||||||
setData(states, data) {
|
setData(states, data) {
|
||||||
states._data = data;
|
states._data = data;
|
||||||
if (data && data[0] && typeof data[0].$selected === 'undefined') {
|
|
||||||
data.forEach((item) => Vue.set(item, '$selected', false));
|
|
||||||
}
|
|
||||||
states.data = sortData((data || []), states);
|
states.data = sortData((data || []), states);
|
||||||
|
const selection = states.selection;
|
||||||
|
|
||||||
if (!states.reserveSelection) {
|
if (!states.reserveSelection) {
|
||||||
states.isAllSelected = false;
|
states.isAllSelected = false;
|
||||||
} else {
|
} else {
|
||||||
const rowKey = states.rowKey;
|
const rowKey = states.rowKey;
|
||||||
if (rowKey) {
|
if (rowKey) {
|
||||||
const selectionMap = {};
|
const selectedMap = getSelectedMap(states, rowKey);
|
||||||
states.selection.forEach((row) => {
|
|
||||||
selectionMap[getRowIdentity(row, rowKey)] = row;
|
|
||||||
});
|
|
||||||
|
|
||||||
states.data.forEach((row) => {
|
states.data.forEach((row) => {
|
||||||
const rowId = getRowIdentity(row, rowKey);
|
const rowId = getRowIdentity(row, rowKey);
|
||||||
if (selectionMap[rowId]) {
|
const rowInfo = selectedMap[rowId];
|
||||||
row.$selected = true;
|
if (rowInfo) {
|
||||||
selectionMap[rowId] = row;
|
selection[rowInfo.index] = row;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -159,19 +187,13 @@ TableStore.prototype.mutations = {
|
||||||
},
|
},
|
||||||
|
|
||||||
rowSelectedChanged(states, row) {
|
rowSelectedChanged(states, row) {
|
||||||
|
const changed = toggleRowSelection(states, row);
|
||||||
const selection = states.selection;
|
const selection = states.selection;
|
||||||
if (row.$selected) {
|
|
||||||
if (selection.indexOf(row) === -1) {
|
if (changed) {
|
||||||
selection.push(row);
|
this.table.$emit('selection-change', selection);
|
||||||
}
|
this.table.$emit('select', selection, row);
|
||||||
} else {
|
|
||||||
const index = selection.indexOf(row);
|
|
||||||
if (index > -1) {
|
|
||||||
selection.splice(index, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.table.$emit('selection-change', selection);
|
|
||||||
this.table.$emit('select', selection, row);
|
|
||||||
|
|
||||||
this.updateAllSelected();
|
this.updateAllSelected();
|
||||||
},
|
},
|
||||||
|
@ -182,30 +204,15 @@ TableStore.prototype.mutations = {
|
||||||
const selection = this.states.selection;
|
const selection = this.states.selection;
|
||||||
let selectionChanged = false;
|
let selectionChanged = false;
|
||||||
|
|
||||||
const setSelected = (item) => {
|
|
||||||
if (item.$selected !== value) {
|
|
||||||
selectionChanged = true;
|
|
||||||
if (value) {
|
|
||||||
if (selection.indexOf(item) === -1) {
|
|
||||||
selection.push(item);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const itemIndex = selection.indexOf(item);
|
|
||||||
if (itemIndex > -1) {
|
|
||||||
selection.splice(itemIndex, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
item.$selected = value;
|
|
||||||
};
|
|
||||||
|
|
||||||
data.forEach((item, index) => {
|
data.forEach((item, index) => {
|
||||||
if (states.selectable) {
|
if (states.selectable) {
|
||||||
if (states.selectable.call(null, item, index)) {
|
if (states.selectable.call(null, item, index) && toggleRowSelection(states, item, value)) {
|
||||||
setSelected(item);
|
selectionChanged = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setSelected(item);
|
if (toggleRowSelection(states, item, value)) {
|
||||||
|
selectionChanged = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -230,36 +237,57 @@ TableStore.prototype.updateColumns = function() {
|
||||||
states.columns = [].concat(states.fixedColumns).concat(_columns.filter((column) => !column.fixed)).concat(states.rightFixedColumns);
|
states.columns = [].concat(states.fixedColumns).concat(_columns.filter((column) => !column.fixed)).concat(states.rightFixedColumns);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TableStore.prototype.isSelected = function(row) {
|
||||||
|
return (this.states.selection || []).indexOf(row) > -1;
|
||||||
|
};
|
||||||
|
|
||||||
TableStore.prototype.clearSelection = function() {
|
TableStore.prototype.clearSelection = function() {
|
||||||
const states = this.states;
|
const states = this.states;
|
||||||
const oldSelection = states.selection;
|
|
||||||
oldSelection.forEach((row) => { row.$selected = false; });
|
|
||||||
if (this.states.reserveSelection) {
|
|
||||||
const data = states.data || [];
|
|
||||||
data.forEach((row) => { row.$selected = false; });
|
|
||||||
}
|
|
||||||
states.isAllSelected = false;
|
states.isAllSelected = false;
|
||||||
states.selection = [];
|
states.selection = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TableStore.prototype.toggleRowSelection = function(row, selected) {
|
||||||
|
toggleRowSelection(this.states, row, selected);
|
||||||
|
};
|
||||||
|
|
||||||
TableStore.prototype.updateAllSelected = function() {
|
TableStore.prototype.updateAllSelected = function() {
|
||||||
const states = this.states;
|
const states = this.states;
|
||||||
|
const { selection, rowKey, selectable, data } = states;
|
||||||
|
if (!data || data.length === 0) {
|
||||||
|
states.isAllSelected = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let selectedMap;
|
||||||
|
if (rowKey) {
|
||||||
|
selectedMap = getSelectedMap(states, rowKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSelected = function(row) {
|
||||||
|
if (selectedMap) {
|
||||||
|
return !!selectedMap[getRowIdentity(row, rowKey)];
|
||||||
|
} else {
|
||||||
|
return selection.indexOf(row) !== -1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let isAllSelected = true;
|
let isAllSelected = true;
|
||||||
const data = states.data || [];
|
|
||||||
for (let i = 0, j = data.length; i < j; i++) {
|
for (let i = 0, j = data.length; i < j; i++) {
|
||||||
const item = data[i];
|
const item = data[i];
|
||||||
if (states.selectable) {
|
if (selectable) {
|
||||||
if (states.selectable.call(null, item, i) && !item.$selected) {
|
if (selectable.call(null, item, i) && !isSelected(item)) {
|
||||||
isAllSelected = false;
|
isAllSelected = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!item.$selected) {
|
if (!isSelected(item)) {
|
||||||
isAllSelected = false;
|
isAllSelected = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
states.isAllSelected = isAllSelected;
|
states.isAllSelected = isAllSelected;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -158,6 +158,11 @@
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
toggleRowSelection(row, selected) {
|
||||||
|
this.store.toggleRowSelection(row, selected);
|
||||||
|
},
|
||||||
|
|
||||||
clearSelection() {
|
clearSelection() {
|
||||||
this.store.clearSelection();
|
this.store.clearSelection();
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue