简单实现element ui 分页组件底层原理

2,451 阅读1分钟

1.预览分页组件效果图

分页组件效果图.gif

2. 分析需求

常用 需求:

变量:

(1)总条数:total

(2)当前页:currentPage

(3)每页显示条数:pageSize

事件:

页码改变:currentPage

页码的按钮数量默认给予5个,也可以按照需求,进行传入修改

3. 基础实现翻页交互效果代码:

<template>
  <div class="xtx-pagination">
    <a v-if="myCurrentPage <= 1" href="javascript:;" class="disabled">上一页</a>
    <a @click="changePage(myCurrentPage - 1)" v-else href="javascript:;"
      >上一页</a
    >
    <span v-if="pager.start > 1">...</span>
    <a
      @click="changePage(i)"
      href="javascript:;"
      :class="{ active: i === myCurrentPage }"
      v-for="i in pager.btnArr"
      :key="i"
      >{{ i }}</a
    >
    <span v-if="pager.end < pager.pageCount">...</span>
    <a
      v-if="myCurrentPage >= pager.pageCount"
      href="javascript:;"
      class="disabled"
      >下一页</a
    >
    <a @click="changePage(myCurrentPage + 1)" v-else href="javascript:;"
      >下一页</a
    >
  </div>
</template>
<script>
import { ref, computed } from 'vue'
export default {
  name: 'Pagination',
  props: {
    total: {
      type: Number,
      default: 100
    },
    currentPage: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 10
    }
  },
  setup (props, { emit }) {
    // 总条数
    const myTotal = ref(100)
    // 每页条数
    const myPageSize = ref(10)
    // 当前第几页
    const myCurrentPage = ref(1)
    // 按钮个数
    const btnCount = 5

    // 重点:根据上述数据得到(总页数,起始页码,结束页码,按钮数组)
    const pager = computed(() => {
      // 计算总页数
      const pageCount = Math.ceil(myTotal.value / myPageSize.value)
      // 计算起始页码和结束页码
      // 1. 理想情况根据当前页码,和按钮个数可得到
      let start = myCurrentPage.value - Math.floor(btnCount / 2)
      let end = start + btnCount - 1
      // 2.1 如果起始页码小于1了,需要重新计算
      if (start < 1) {
        start = 1
        end = (start + btnCount - 1) > pageCount ? pageCount : (start + btnCount - 1)
      }
      // 2.2 如果结束页码大于总页数,需要重新计算
      if (end > pageCount) {
        end = pageCount
        start = (end - btnCount + 1) < 1 ? 1 : (end - btnCount + 1)
      }
      // 处理完毕start和end得到按钮数组
      const btnArr = []
      for (let i = start; i <= end; i++) {
        btnArr.push(i)
      }
      return { pageCount, start, end, btnArr }
    })
    return{ pager }
  }  
}
</script>
<style scoped lang="less">
.xtx-pagination {
  display: flex;
  justify-content: center;
  padding: 30px;
  > a {
    display: inline-block;
    padding: 5px 10px;
    border: 1px solid #e4e4e4;
    border-radius: 4px;
    margin-right: 10px;
    &:hover {
      color: @xtxColor;
    }
    &.active {
      background: @xtxColor;
      color: #fff;
      border-color: @xtxColor;
    }
    &.disabled {
      cursor: not-allowed;
      opacity: 0.4;
      &:hover {
        color: #333;
      }
    }
  }
  > span {
    margin-right: 10px;
  }
}
</style>

4. 接下来是基础功能实现的代码:

主要是借助子传父,在页码改变的时候抛出事件,以实现上述功能

import { ref, computed, watch } from 'vue'

 setup(){
        // 改变页码
    const changePage = (newPage) => {
      if (myCurrentPage.value !== newPage) {
        myCurrentPage.value = newPage
        // 通知父组件最新页码
        emit('current-change', newPage)
      }
    }
    // 监听传入的值改变
    watch(props, () => {
      myTotal.value = props.total
      myPageSize.value = props.pageSize
      myCurrentPage.value = props.currentPage
    }, { immediate: true })
    
    return { pager, myCurrentPage, changePage }
  }
    
}

5. 将这个组件注册为全局

// 分页组件
import Pagination from './pagination.vue'
const myPlugin = {
  install (app) {
    // app为vue的实例
    // app.component('组件名',组件对象)
    app.component(Pagination.name, Pagination)
  }
}

export default myPlugin

6. 使用组件并传值

 <XtxPagination
    @current-change="changePager"
    :total="total"
    :current-page="queryObj.page"
 />
 
 <script>
   export default {
     setup () {
       // 记录总条数
       const total = ref(0)
       
       const changePager = (page) => {
         //同步当前页码
         queryObj.page = page
         
       }
       
       return { total, changePager }
     }
   }
 </script>

根据当前项目的需求来对其做些更改,本文仅供参考