分页及组件化(vue+element)分页功能简单流程操作示例

167 阅读1分钟

例图

image.png element参考链接

pagination.vue (在src下的components文件夹中)

<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-pagination :background="background" :current-page.sync="currentPage" :page-size.sync="pageSize" :layout="layout" :page-sizes="pageSizes" :total="total" v-bind="$attrs" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<script>
// import { scrollTo } from '@/utils' // 切换页数或每页条数发生变化是当前页面回到顶部 可加可不加

export default {
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    // 每页显示个数选择器的选项设置
    pageSizes: {
      type: Array,
      default () {
        return [10, 20, 30, 50, 100, 200]
      }
    },

    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    // 分页按钮添加背景色
    background: {
      type: Boolean,
      default: true
    },
    // 页面是否回到顶部
    autoScroll: {
      type: Boolean,
      default: true
    },
    // 是否显示分页组件
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    // 初始页
    currentPage: {
      get () {
        return this.page
      },
      set (val) {
        this.$emit('update:page', val)
      }
    },
    // 每页的数据
    pageSize: {
      get () {
        return this.limit
      },
      set (val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    // 每页条数改变时会触发
    handleSizeChange (val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    // 当前页数改变时触发
    handleCurrentChange (val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style scoped>
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
.pagination-container.hidden {
  display: none;
}
</style>

ceshi.vue (在ceshi文件中调用分页组件)

<template>
  <div id="app">
    <pagination v-if="page.totalPages>1" :total="page.total" :page.sync="page.pageNo" :limit.sync="page.pageSize" :auto-scroll="false" @pagination="handelAdminRecommendRecordQueryRecord" />
  </div>
</template>

<script>
import Pagination from '@/components/pagination'

export default {
  components: {
    Pagination
  },
  data () {
    return {
      page: {
        totalPages: 2, // 总条数
        total: 20, // 总条目数
        pageSize: 10 // 每页显示条目个数
      }
    }
  },
  methods: {
    // 分页函数
    handelAdminRecommendRecordQueryRecord (e) {
      console.log(e)
    }
  }

}
</script>

页面回到顶部

/**
 * @param {number} to
 * @param {number} duration
 * @param {Function} callback
 */
export function scrollTo(to, duration, callback) {
  const start = position()
  const change = to - start
  const increment = 20
  let currentTime = 0
  duration = typeof duration === 'undefined' ? 500 : duration
  var animateScroll = function () {
    // increment the time
    currentTime += increment
    // find the value with the quadratic in-out easing function
    var val = Math.easeInOutQuad(currentTime, start, change, duration)
    // move the document.body
    move(val)
    // do the animation unless its over
    if (currentTime < duration) {
      requestAnimFrame(animateScroll)
    } else {
      if (callback && typeof callback === 'function') {
        // the animation is done so lets callback
        callback()
      }
    }
  }
  animateScroll()
}