1. 程式人生 > 其它 >Vue + Element-UI管理系統中 Pagination 分頁 二次封裝

Vue + Element-UI管理系統中 Pagination 分頁 二次封裝

一.在components資料夾下,新建pagination檔案

<template>
  <div class="page-content">
    <el-pagination
      :background="background"
      :page-sizes="pageSizes"
      :layout="layout"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    background: {
      type: Boolean,
      default: true
    },
    pageSizes: {
      type: Array,
      default: () => {
        return [10, 20, 30, 50]
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    total: {
      required: true,
      type: Number,
      default: 0
    },
    pageNum: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 10
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.pageNum
      },
      set(val) {
        this.$emit('update:pageNum', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.currentPage = 1;
      this.$emit('pagination', { pageNum: this.currentPage, limit: val })
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { pageNum: val, limit: this.pageSize })
    }
  }
}
</script>

<style scoped>
.page-content{
  text-align: right;
  margin: 10px 10px 0 0;
}
</style>

  

二、使用元件

1、引入import Pagination from '@/components/pagination';

2、註冊元件components: { Pagination };

3、使用元件

<Pagination
      :total="total"
      :pageNum.sync="pages.pageNum"
      :limit.sync="pages.pageSize"
      @pagination="fetchData()" 
  />
mounted() {
    this.fetchData()
 },
methods: {
    fetchData() {
      this.loading = true
      getList(this.pages).then(res => {
        if(res.data.code === 20000) {
          this.dataList = res.data.data.items
          this.total = res.data.data.total
          this.loading = false
        }
      })
      setTimeout(() => {
        this.loading = false
      },3000)
    }
}

  

三、引數說明

total:資料總條數, pageNum: 頁數 pageSize:每頁展示條數 pagination:頁碼改變或每頁條數改變,所觸發的函式  

原始碼

如果你感興趣的話,請前往 GitHub 檢視原始碼和完整文件。

https://github.com/wangibook/my-table-component