美文网首页
vue elementUI 分页组件封装Demo 可二次复用 分

vue elementUI 分页组件封装Demo 可二次复用 分

作者: 爱学习的小仙女早睡早起 | 来源:发表于2021-09-02 15:44 被阅读0次

最常用参数都做了处理
: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"

pagination.vue

<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/scroll-to'

export default {
  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: 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: 1, 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>

使用

<pagination 
:page="currentPage" 
:limit="pageSize" 
:total="total" 
@pagination="pagination" />




// 引用
import Pagination from '@/components/Pagination/index';







附加 scroll-to.js

Math.easeInOutQuad = function(t, b, c, d) {
  t /= d / 2
  if (t < 1) {
    return c / 2 * t * t + b
  }
  t--
  return -c / 2 * (t * (t - 2) - 1) + b
}

// requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
var requestAnimFrame = (function() {
  return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60) }
})()

/**
 * Because it's so fucking difficult to detect the scrolling element, just move them all
 * @param {number} amount
 */
function move(amount) {
  document.documentElement.scrollTop = amount
  document.body.parentNode.scrollTop = amount
  document.body.scrollTop = amount
}

function position() {
  return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop
}

/**
 * @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()
}

相关文章

  • vue elementUI 分页组件封装Demo 可二次复用 分

    最常用参数都做了处理:background="background":current-page.sync="cur...

  • vue elementUI table表格增删改查Demo

    涉及到elementUI,table组件,分页组件,dialog组件,form组件.... 完整Demo 分页组件...

  • 使用element-ui二次封装table组件(支持合并单元格,

    使用vue+elementui封装table。组件中提供了含有分页的表格,支持filter,支持render,支持...

  • 2018-09-18 vue第六章

    组件:组件可以扩展 HTML 元素,封装可重用的代码。组件是可复用的 Vue 实例,所以它们与 new Vue 接...

  • vue中对element的分页组件的封装

    在vue中使用分页,一般都做个封装来全局化,对组件进行复用。 1、封装。(直接copy就可以使用) 在compon...

  • elementUi之分页组件的简单封装

    因elementUi分页封装成单独组件当前页存在bug,没有在父组件设置当前页。 如有需要可在分页组件单独设置 在...

  • 2018-09-18

    组件是可复用的 Vue 实例一、组件:可以拓展HTML元素,封装可重用的代码组件分为全局组件和局部组件通过prop...

  • vue 组件基础

    祭出demo 基础示例 组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 data、...

  • Vue组件

    组件(Component)是Vue.js 最强大的功能之一。组件可以扩展出HTMl元素,封装可重用的代码。是可复用...

  • Vue组件

    Vue组件 明明vue已经那么完美了,为什么还要学习Vue呢? 这是为了实现高度封装和高度可复用。 一、注册 使用...

网友评论

      本文标题:vue elementUI 分页组件封装Demo 可二次复用 分

      本文链接:https://www.haomeiwen.com/subject/jkqcwltx.html