在vue项目中,使用elementui组件库table表格,在列表页需要有无限滚动和复选框功能
问题:在滑动滚动条加载更多数据的时候,之前数据的选中状态发生变化
解决:
<!--表格-->
<el-table
ref="mytable"
:data="tableData"
v-el-table-infinite-scroll="load"
@select="selectnow"
@select-all="selectAll"
>
<el-table-column type="selection" width="30"></el-table-column>
.......
</el-table>
<!--加载更多动画-->
<div class="loadingtooltip" slot="append">
<p v-if="loading">
<i class="el-icon-loading"></i>
</p>
</div>
data(){
return {
tableData: [...],
selectData:[],
loading:false,
expandData:[....]
}
},
methods:{
selectlist(){
let that = this;
//解决toggleRowSelection无效的问题
that.$nextTick(()=>{
if (that.selectData) {
that.selectData.forEach((row) => {
that.$refs.mytable.toggleRowSelection(row);
});
} else {
that.$refs.mytable.clearSelection();
}
})
},
// 无限滚动
load() {
let that=this;
if (this.tableData.length >= 19) {
this.loading = false;
console.log("数据已经到底了");
return;
} else {
this.loading = true;
setTimeout(() => {
that.$nextTick(()=>{
that.selectlist();
})
this.tableData = this.tableData.concat(this.expandData);
this.loading = false;
}, 2000);
}
},
//获得当前选中的行
selectnow(sele, row) {
this.selectData = sele;
},
//获得选中的所有数据
selectAll(selection) {
this.selectData = selection;
},
}
实现效果: