element Pagination分页 删除最后一条数据返回上一页问题

问题描述:

在删除表单最后一条数据时,自动返回上一页,但实际情况是还是删除前的页数,数据为空

解决办法:

   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 
  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值