Vue中实现分页

1.构造分页组件,并注册为全局组件

html 复制代码
<template>
    <div class="pagination">
      <button v-if="startNumAndEndNum.start>1" @click="$emit('getPageNo',pageNo-1)">上一页</button>
      <button v-if="startNumAndEndNum.start>1" @click="$emit('getPageNo',1)" :class="{active:pageNo==1}">1</button>
      <button v-if="startNumAndEndNum.start>2">···</button>
        <!-- 中间部分 -->
      <template  v-for="(page,index) in startNumAndEndNum.end">
        <button
        :key="index" 
        v-if="startNumAndEndNum.start<=page"
        @click="$emit('getPageNo',page)"
        :class="{active:pageNo==page}"
        > 
        {{page}}
        </button>
       </template>  

      
      <button v-if="startNumAndEndNum.end<allPage-1">···</button>
      <button v-if="startNumAndEndNum.end<allPage" @click="$emit('getPageNo',allPage)" :class="{active:pageNo==allPage}">{{allPage}}</button>
      <button v-if="startNumAndEndNum.end!=allPage" @click="$emit('getPageNo',pageNo+1)">下一页</button>
      
      <button style="margin-left: 30px">共 {{total}} 条</button>
    </div>
  </template>
  
  <script>
    export default {
      name: "Page",
      props:['pageNo','pageSize','total','continues'],
      computed:{
        //计算总页数
        allPage(){
            //向上取整
            return Math.ceil(this.total / this.pageSize)
        },
        //计算 连续页码 起始数据和结束数据[连续页码至少是5]
        startNumAndEndNum(){
            //先定义两个变量存储开始与结束的数字
            let start=0,end=0;
            //如果数据不够5页(不正常现象)
            if(this.allPage<this.continues){
                start=1,
                end=this.allPage
            }else{
                start=this.pageNo - parseInt(this.continues/2),
                end=this.pageNo + parseInt(this.continues/2)
                //当当前页为1时 start为-1 
                if(start<1){
                    start=1,
                    end=this.continues
                }
                //纠正end 
                if(end>this.allPage){
                    end=this.allPage,
                    start=end-this.continues + 1
                }
                
            }
            return {start,end}
        }

      }
    }
  </script>
  
  <style lang="less" scoped>
    .pagination {
      text-align: center;
      button {
        margin: 0 5px;
        background-color: #f4f4f5;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;

        &[disabled] {
          color: #c0c4cc;
          cursor: not-allowed;
        }
  
        &.active {
          cursor: not-allowed;
          background-color: #409eff;
          color: #fff;
        }
      }
    //   button:active {
    //     color: rgba(235, 217, 55, 0.935);
    //     border: 1px solid orange;
    // }
    .active{
      background-color: skyblue;
    }
    }
  </style>

2.使用分页组件

html 复制代码
<template>
  <div>
          <!-- 分页组件 -->
          <Page 
          :pageNo="params.pageNo" 
          :pageSize="params.pageSize" 
          :total="total" 
          :continues="5"
          ref="pg"
          ></Page>
  </div>
</template>

<script>
import {mapState} from "vuex";
export default {
  name: "Search",
  data() {
    return {
      params: {
        //分页器参数
        pageNo: 1,
        pageSize: 10,
      },
    };
  },
  mounted() {
    this.$refs.pg.$on('getPageNo',(pageNo)=>{
        this.params.pageNo=pageNo
        this.getSearch(this.params)
      })
  },
  computed: {
    //获取search模块展示的总数据条数
    ...mapState({
      total:state=>state.search.searchList.total
    })
  },
  methods: {
    //向服务器发请求获取search模块数据,根据参数不同返回不同的数据
    //把这次请求封装成函数,需要时候调用即可
    getSearch(params) {
      this.$store.dispatch("SearchInfo", params);
    },
};
</script>

<style lang="less" scoped>
</style>
相关推荐
yqcoder1 分钟前
Express + MongoDB 实现在筛选时间段中用户名的模糊查询
java·前端·javascript
十八朵郁金香23 分钟前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
计算机-秋大田1 小时前
基于Spring Boot的兴顺物流管理系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·spring·课程设计
GDAL1 小时前
HTML 中的 Canvas 样式设置全解
javascript
m0_528723811 小时前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer1 小时前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
GDAL2 小时前
HTML Canvas clip 深入全面讲解
前端·javascript·canvas
禾苗种树2 小时前
在 Vue 3 中使用 ECharts 制作多 Y 轴折线图时,若希望 **Y 轴颜色自动匹配折线颜色**且无需手动干预,可以通过以下步骤实现:
前端·vue.js·echarts
GISer_Jing2 小时前
Javascript排序算法(冒泡排序、快速排序、选择排序、堆排序、插入排序、希尔排序)详解
javascript·算法·排序算法
贵州数擎科技有限公司2 小时前
使用 Three.js 实现流光特效
前端·webgl