06_Vue实现分页功能(保姆级教程)

分页功能

1、前后端共同实现

<template>
  <div>
    <!-- 上面是table表 -->
    <!-- 分页器 -->
    <el-pagination 
      background 
      @current-change="handleCurrentChange" 
      :page-size="pageSize" 
      :current-page="currentPage"
      layout="total,prev, pager, next" 
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [], //table 数据
      total: 0,//数据的条数                       将后端的count赋给 total
      pageIndex: 1,//当前分页的页数,默认显示第一页   向后端传
      pageSize: 20,//每页展示的数据条数             向后端传
      currentPage: '',//当前页

    }
  },
  methods: {
    select() {
      let params = {
        strKey: this.strkey,
        page: this.currentPage,
        pageSize: this.pageSize
      }
      axios({
        method: '请求类型',
        url: '请求地址',
        data: params
      }).then(res => {
        this.total = res.data.count
      })
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
      this.select();
    }
  }
}
</script>

2、前端自己实现:

<template>
  <div>
    <!-- 上面是table表 -->
    <!-- 分页器 -->
    <el-pagination 
      background 
      @current-change="handleCurrentChange" 
      :page-size="pageSize" 
      :current-page="currentPage"
      layout="total,prev, pager, next" 
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import { getcustomqueryresults } from '@/utils/https'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      PageSize: 20,
      total: 0,

    }
  },
  methods: {
    select() {
      ().then(res => {
     	//将得到的数据 进行分页处理
        this.tableData = res.data.slice(
          (this.currentPage - 1) * this.PageSize,
          this.currentPage * this.PageSize
        )
        this.total = Number(res.count)
      })
    },
    handleCurrentChange(val) {
      console.log(val, 'val当前页')
      this.currentPage = val
      this.select()
    },
  }
}
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只嘉祺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值