vue iview 表格分页选中数据
项目引入lodash
import _ from 'lodash';
<Table border ref="table"
:columns="resultTitle"
@on-selection-change="selectedChange"
@on-select="handleSelect"
@on-select-cancel="handleCancel"
@on-select-all-cancel="handleCancelSelectAll"
@on-select-all="handleSelectAll"
:data="statData"
style="margin-bottom: 20px">
</Table>
data(){
return{
selectedData: [],
selected: [],
}
}
methods:{
// 表格选中数据
selectedChange(selection) {
this.selectedData = selection;
},
// 取消选中
handleCancel(selection, row) {
//从已选项中去除取消项
_.remove(this.selected, n => {
return n.id === row.id;
});
},
// 选中
handleSelect(selection, row) {
//添加到已选项
this.selected.push(row);
},
// 全选
handleSelectAll(selection) {
//数组合并,有可能用户先选择了某几项,已经被我们push进去,因此数组合并需要去重一下
this.selected = _.uniqBy(this.selected.concat(selection), "id");
},
// 取消全选
handleCancelSelectAll(selection) {
//从已选项中移除当页数据
this.selected = _.differenceBy(this.selected, this.statData, "id");
},
//把源数据加上_checked字段,遍历已选项数据,更新选中状态
updateChecked() {
for (var i = 0; i < this.statData.length; i++) {
this.statData[i]._checked = false;
for (var j = 0; j < this.selected.length; j++) {
if (this.selected[j].id === this.statData[i].id) {
this.statData[i]._checked = true;
}
}
}
},
// 重复检测取值
getRepeatNum() {
if (this.selected.length != 0) {
console.log(this.selected)
},
}