vue实现简单的分页功能

1、首先,在data中定义以下变量:

data() {
    return {
      list: null,
      listLoading: true,
      totalPage: 1, // 统共页数,默认为1
            currentPage: 1, //当前页数 ,默认为1
            pageSize: 5, // 每页显示数量
      currentPageData: [], //当前页显示内容
      headPage: 1
    }
  },

2、发送请求,获取后端数据(list集合)

axios.get('http://192.168.56.1:8081/sel/'+id).then((res) =>{
        console.log(res.data.data ) 
        that.list = res.data.data 
        that.listLoading = false

3、根据返回数据list的length来计算data中变量的值:

this.totalPage=Math.ceil(this.list.length / this.pageSize);
        this.totalPage = this.totalPage == 0 ? 1 : this.totalPage;
        this.getCurrentPageData();

4、调用getCurrentPageData()方法设置当前页面的数据

getCurrentPageData() {
                        let begin = (this.currentPage - 1) * this.pageSize;
                        let end = this.currentPage * this.pageSize;
                        this.currentPageData = this.list.slice(
                            begin,
                            end
                        );
                    },

5、添加按钮并实现首页、尾页、上一页、下一页功能:

<input type="button" value="首页" @click="firstPage">
<input type="button" value="上一页" @click="prevPage">
<input type="button" value="下一页" @click="nextPage">
<input type="button" value="尾页" @click="lastPage">

6.首页上一页下一页最后一页方法

// 上一页
    prevPage () {
      if (this.currentPage === 1) {
        alert('已经是第一页了')
        return false
      } else {
        this.currentPage--
        this.getCurrentPageData()
      }
    },
    // 下一页
    nextPage () {
      if (this.currentPage === this.totalPage) {
        alert('已经是最后一页了')
        return false
      } else {
        this.currentPage++
        this.getCurrentPageData()
      }
    },
    // 尾页
    lastPage () {
      if (this.currentPage === this.totalPage) {
        return false
      } else {
        this.currentPage = this.totalPage
        this.getCurrentPageData()
      }
    },
    // 首页
    firstPage () {
      // eslint-disable-next-line no-unused-expressions
      this.currentPage = this.headPage
      this.getCurrentPageData()
    }

7.最后修改组件中的输出信息

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值