在项目中,后端可能不支持分页,请求回来数据很多,放到页面中不利于浏览查找,因此前端可以做一个伪分页效果
代码
<el-pagination
background
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:page-sizes="[10, 20, 30, 50]"
:page-size="10"
layout="sizes, prev, pager, next, jumper"
:total="tableDataList.length">
</el-pagination>
js 逻辑
data() {
return {
tableData: [],
tableDataList: [],
pagesize: 10,
page: 1
}
},
created: {
this.tableData = this.tableDataList.slice(0, 10)
},
watch: {
page(newVal,oldVal) {
console.log(newVal,oldVal);
if(newVal * 1 > oldVal * 1) {
this.tableData = this.tableDataList.slice((newVal-1) * this.pagesize, newVal * this.pagesize)
}else{
this.tableData = this.tableDataList.slice((newVal-1) * this.pagesize, newVal * this.pagesize)
}
},
pagesize(newVal, oldVal) {
if(newVal * 1 > oldVal * 1) {
this.tableData = this.tableDataList.slice((this.page-1) * newVal, this.page * newVal)
}else{
this.tableData = this.tableDataList.slice((this.page-1) * newVal, this.page * newVal)
}
}
},
methods: {
handleSizeChange(val) {
console.log(`${val} 条每页`);
this.pagesize = val
},
handleCurrentChange(val) {
console.log(`当前页码: ${val}`);
this.page = val
},
}
如果有删除按钮,在删除事件里添加
this.tableData = this.tableDataList.slice((this.page - 1) * this.pagesize, this.page * this.pagesize)
|