问题描述:
在删除表单最后一条数据时,自动返回上一页,但实际情况是还是删除前的页数,数据为空
解决办法:
rowDel(row) {
sensitiveWordDelete(row.id).then((res) => {
if (res.data.code == 200) {
this.$message({
type: "success",
message: "删除成功!",
});
// totalSensitive : 总数据条数
// shieldForm.size: 每页多少条数据
// shieldForm.current: 当前页码(当前是哪一页)
const totalPage = Math.ceil((this.totalSensitive -1) / this.shieldForm.size)
this.shieldForm.current = this.shieldForm.current > totalPage ? totalPage : this.shieldForm.current
this.shieldForm.current = this.shieldForm.current < 1 ? 1 : this.shieldForm.current
this.getSensitiveWordList();
}
});
},
.
当删除数据出现页码错误时,加上这三句代码就可以完美解决
const totalPage = Math.ceil((this.totalSensitive -1) / this.shieldForm.size)
this.shieldForm.current = this.shieldForm.current > totalPage ? totalPage : this.shieldForm.current
this.shieldForm.current = this.shieldForm.current < 1 ? 1 : this.shieldForm.current
.
.
解读三句代码
const totalPage = Math.ceil((this.totalSensitive -1) / this.shieldForm.size)
3 = (31 - 1) / 10
Math.ceil() // 向上取整方法
定义一个 当前总页数totalPage : 当前总页数等于 = 总条数减去删除一条数据 除以 每页的条数
假设数据为31条 每页10条数据
this.shieldForm.current = this.shieldForm.current > totalPage ? > totalPage : this.shieldForm.current
假设没删除之前当前页码在最后一页, 也就是第4页
删除后总页数就是上面定义的 totalPage 为 3
this.shieldForm.current = 4 > 3 ? 3 : 4
所以目前的页码就是 3
this.shieldForm.current = this.shieldForm.current < 1 ? 1 : this.shieldForm.current
还有一种情况就是只有一条数据时,删除后,当前页码设置为 1