总体配置其实和vue2 的 el-pagination差不多,不过将页码和页数的调用事件回调了回来,我们这里既可以单独使用这个分页组件,也可以和我前面写得 通用表格组件 一起使用,通用表格组件里内置了本分页组件。(注: 写时候有点急,所以没写啥注释,要用本代码但是不怎么理解的,可以下面评论,我基本上会看的哦~)

// 本页条数改变
emit('pagination', { page: currentPage, limit: val })
// 页数改变
emit('pagination', { page: val, limit: pageSize })
// 父组件调用方式
<template>
<Pagination v-show="pageInfo.totalCount > 0"
  :total="pageInfo.totalCount"
  :page="pageInfo.pageIndex"
  :limit="pageInfo.pageSize"
  :pageSizes="pageInfo.pageArr"
      @pagination="getList" />
</template>
<script lang="ts">
const getList = (pages: any) => {
    let { page, limit } = pages
    if (!isNumber(page)) {
      page = page.value
    }
    if (!isNumber(limit)) {
      limit = limit.value
    }
    const pageInfo = {
      page, // 页码
      limit // 页数
    }
    // 分页参数已拿到,剩下就是你自己调用接口的方法
  }
</script>



// 分页组件代码
<template>
  <div :class="{ hidden: hidden }" class="pagination-container scrollBar">
    <el-pagination
      :background="background"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts">
  import { nextTick, Prop } from 'vue'
  import { defineComponent, computed } from 'vue'
  interface Props {
    total: number
    page: number
    limit: number
    pageSizes: Array<any>
    layout: string
    background: boolean
    autoScroll: boolean
    hidden: boolean
  }
  export default defineComponent({
    name: 'Pagination',
    props: {
      total: {
        required: true,
        type: Number,
      },
      page: {
        type: Number,
        default: 1,
      },
      limit: {
        type: Number,
        default: 10,
      },
      pageSizes: {
        type: Array,
        default() {
          return [10, 20, 30, 50]
        },
      },
      layout: {
        type: String,
        default: 'total, sizes, prev, pager, next, jumper',
      },
      background: {
        type: Boolean,
        default: false,
      },
      autoScroll: {
        type: Boolean,
        default: true,
      },
      hidden: {
        type: Boolean,
        default: false,
      },
    },
    setup (props: Props, { emit }) {
      const currentPage: any = computed( {
        get() {
          return props.page
        },
        set(val) {
          emit('update:page', val)
        },
      })

      const pageSize: any = computed({
        get() {
          return props.limit
        },
        set(val) {
          emit('update:limit', val)
        },
      })

      const handleSizeChange = (val: number) => {
        emit('pagination', { page: currentPage, limit: val })
      }

      const handleCurrentChange = (val: number) => {
        nextTick(() => {
          emit('pagination', { page: val, limit: pageSize })
        })
      }
      return {
        currentPage,
        pageSize,
        handleSizeChange,
        handleCurrentChange
      }
    }
  })
</script>

<style scoped lang="scss">
@import '@/styles/index.scss';
  .pagination-container {
    width: 100%;
    overflow-x: auto;
    background: #fff;
    padding: 16px 16px 0;
    .el-pagination {
      text-align: right;
    }
  }
  .scrollBar {
    @include scrollBar;
  }
  .pagination-container.hidden {
    display: none;
  }
</style>

Logo

前往低代码交流专区

更多推荐