element-ui Pagination组件源码分析整理笔记(七)


element-ui源码的版本是2.4.9

pagination.js

import Pager from './pager.vue';
import ElSelect from 'element-ui/packages/select';
import ElOption from 'element-ui/packages/option';
import ElInput from 'element-ui/packages/input';
import Locale from 'element-ui/src/mixins/locale';
import { valueEquals } from 'element-ui/src/utils/util';

export default {
  name: 'ElPagination',

  props: {
    pageSize: {  //每页显示条目个数,支持.sync 修饰符
      type: Number,
      default: 10
    },
    small: Boolean, //是否使用小型分页样式
    total: Number, //总条目数
    pageCount: Number, //总页数,total 和 page-count 设置任意一个就可以达到显示页码的功能;如果要支持 page-sizes 的更改,则需要使用 total 属性
    pagerCount: {  //页码按钮的数量,当总页数超过该值时会折叠
      type: Number,
      validator(value) {
        return (value | 0) === value && value > 4 && value < 22 && (value % 2) === 1;
      },
      default: 7
    },
    currentPage: { //当前页数,支持 .sync 修饰符
      type: Number,
      default: 1
    },
    layout: { //组件布局,子组件名用逗号分隔
      default: 'prev, pager, next, jumper, ->, total'
    },
    pageSizes: { //每页显示个数选择器的选项设置
      type: Array,
      default() {
        return [10, 20, 30, 40, 50, 100];
      }
    },
    popperClass: String, //每页显示个数选择器的下拉框类名
    prevText: String, //替代图标显示的上一页文字
    nextText: String, //替代图标显示的下一页文字
    background: Boolean, //是否为分页按钮添加背景色
    disabled: Boolean //是否禁用
  },

  data() {
    return {
      internalCurrentPage: 1,  //当前的页码
      internalPageSize: 0,  //总页数
      lastEmittedPage: -1,
      userChangePageSize: false
    };
  },
  //render函数生成el-pagination
  render(h) {
    //最外层的div标签
    let template = 
; const layout = this.layout || ''; if (!layout) return; const TEMPLATE_MAP = { prev: , jumper: , pager: , next: , sizes: , slot: , total: }; const components = layout.split(',').map((item) => item.trim()); const rightWrapper =
; let haveRightWrapper = false; template.children = template.children || []; rightWrapper.children = rightWrapper.children || []; components.forEach(compo => { // ->这个符号主要是将其后面的组件放在rightWrapper中,然后右浮动;如果存在->符号,就将haveRightWrapper为true if (compo === '->') { haveRightWrapper = true; return; } // 当haveRightWrapper为true,即在->后面的放入rightWrapper中 if (!haveRightWrapper) { template.children.push(TEMPLATE_MAP[compo]); } else { rightWrapper.children.push(TEMPLATE_MAP[compo]); } }); if (haveRightWrapper) { //将rightWrapper加在template.children数组的开头,这样rightWrapper浮动之后就是在最右边 template.children.unshift(rightWrapper); } return template; }, components: { MySlot: { render(h) { return ( this.$parent.$slots.default ? this.$parent.$slots.default[0] : '' ); } }, // 上一页组件 Prev: { //上一页; prevText用户设置的替代上一页图标的文字,存在显示文字,不存在显示上一页图标 render(h) { return ( ); } }, //下一页组件 Next: { // this.$parent.internalCurrentPage === this.$parent.internalPageCount 当前页数等于总页数时 或者 总页数等于0时,下一页按钮被禁用 render(h) { return ( ); } }, // 每页显示条目个数组件 Sizes: { mixins: [Locale], props: { pageSizes: Array //每页显示个数选择器的选项设置 [10, 20, 30, 40, 50, 100] }, watch: { pageSizes: { // 确认是否以当前的初始值执行handler的函数 immediate: true, handler(newVal, oldVal) { if (valueEquals(newVal, oldVal)) return; if (Array.isArray(newVal)) { // 如果用户设置了每页显示的条目个数,并且pageSize在设置的pageSizes中存在的话,就显示pageSize,否则就显示this.pageSizes[0] // 最后将每页显示的条目个数赋值给this.$parent.internalPageSize this.$parent.internalPageSize = newVal.indexOf(this.$parent.pageSize) > -1 ? this.$parent.pageSize : this.pageSizes[0]; } } } }, render(h) { // this.t('el.pagination.pagesize') 返回'条/页' return ( { this.pageSizes.map(item => ) } ); }, components: { ElSelect, ElOption }, methods: { handleChange(val) { if (val !== this.$parent.internalPageSize) { this.$parent.internalPageSize = val = parseInt(val, 10); this.$parent.userChangePageSize = true; //如果父组件中pageSize用了.sync 修饰符,这里将会触发父组件的update,改变pageSize的值 this.$parent.$emit('update:pageSize', val); //触发父组件的size-change事件,将改变的每页显示的条目个数的值传递出去 this.$parent.$emit('size-change', val); } } } }, //前往多少页组件 Jumper: { mixins: [Locale], data() { return { oldValue: null }; }, components: { ElInput }, watch: { '$parent.internalPageSize'() { this.$nextTick(() => { this.$refs.input.$el.querySelector('input').value = this.$parent.internalCurrentPage; }); } }, methods: { handleFocus(event) { this.oldValue = event.target.value; }, handleBlur({ target }) { this.resetValueIfNeed(target.value); this.reassignMaxValue(target.value); }, // 按下回车,前往多少页 handleKeyup({ keyCode, target }) { if (keyCode === 13 && this.oldValue && target.value !== this.oldValue) { this.handleChange(target.value); } }, // 改变当前页 handleChange(value) { // 更新页码列表中当前页的值 this.$parent.internalCurrentPage = this.$parent.getValidCurrentPage(value); this.$parent.emitChange(); this.oldValue = null; this.resetValueIfNeed(value); }, resetValueIfNeed(value) { const num = parseInt(value, 10); if (!isNaN(num)) { if (num < 1) { // 调用input中的setCurrentValue方法,将input中的值设置为1 this.$refs.input.setCurrentValue(1); } else { // 如果input中输入的值,大于最大页码,则置为最大页码值 this.reassignMaxValue(value); } } }, reassignMaxValue(value) { const { internalPageCount } = this.$parent; if (+value > internalPageCount) { // 调用input中的setCurrentValue方法,将input中的值设置为internalPageCount或者为1 this.$refs.input.setCurrentValue(internalPageCount || 1); } } }, // 前往多少页 render(h) { return ( { this.t('el.pagination.goto') } { this.t('el.pagination.pageClassifier') } ); } }, //总共的页数,组件 Total: { mixins: [Locale], render(h) { return ( typeof this.$parent.total === 'number' ? { this.t('el.pagination.total', { total: this.$parent.total }) } : '' ); } }, Pager }, methods: { handleCurrentChange(val) { this.internalCurrentPage = this.getValidCurrentPage(val); this.userChangePageSize = true; //触发父组件current-change事件,并传递相应的值 this.emitChange(); }, prev() { if (this.disabled) return; const newVal = this.internalCurrentPage - 1; this.internalCurrentPage = this.getValidCurrentPage(newVal); //触发父组件的prev-click事件,并将CurrentPage传递出去 this.$emit('prev-click', this.internalCurrentPage); //触发父组件current-change事件,并传递相应的值 this.emitChange(); }, next() { if (this.disabled) return; const newVal = this.internalCurrentPage + 1; this.internalCurrentPage = this.getValidCurrentPage(newVal); //触发父组件的next-click事件,并将CurrentPage传递出去 this.$emit('next-click', this.internalCurrentPage); this.emitChange(); }, //校验需要前往的页码的值 getValidCurrentPage(value) { value = parseInt(value, 10); const havePageCount = typeof this.internalPageCount === 'number'; let resetValue; if (!havePageCount) { if (isNaN(value) || value < 1) resetValue = 1; } else { // 如果当前页码小于1,则取1;如果当前页码大于最大页码,则取最大页码 if (value < 1) { resetValue = 1; } else if (value > this.internalPageCount) { resetValue = this.internalPageCount; } } //如果当前页码是非数字,或者为0,则将当前页码置为1,并返回 if (resetValue === undefined && isNaN(value)) { resetValue = 1; } else if (resetValue === 0) { resetValue = 1; } return resetValue === undefined ? value : resetValue; }, emitChange() { this.$nextTick(() => { //用户改变当前PageSize时,触发父组件的current-change事件 if (this.internalCurrentPage !== this.lastEmittedPage || this.userChangePageSize) { this.$emit('current-change', this.internalCurrentPage); //lastEmittedPage记录最后传递的CurrentPage的值 this.lastEmittedPage = this.internalCurrentPage; this.userChangePageSize = false; } }); } }, computed: { internalPageCount() { if (typeof this.total === 'number') { //总页数 = 总条目数 / 每页的显示条数 return Math.ceil(this.total / this.internalPageSize); } else if (typeof this.pageCount === 'number') { //总页数 return this.pageCount; } return null; } }, watch: { currentPage: { immediate: true, handler(val) { this.internalCurrentPage = val; } }, pageSize: { immediate: true, handler(val) { this.internalPageSize = isNaN(val) ? 10 : val; } }, // internalCurrentPage改变时去触发父组件中currentPage更新 // 在v2.4.11这里已经改掉了 internalCurrentPage: { immediate: true, handler(newVal, oldVal) { newVal = parseInt(newVal, 10); /* istanbul ignore if */ if (isNaN(newVal)) { newVal = oldVal || 1; } else { newVal = this.getValidCurrentPage(newVal); } if (newVal !== undefined) { this.internalCurrentPage = newVal; if (oldVal !== newVal) { this.$emit('update:currentPage', newVal); } } else { this.$emit('update:currentPage', newVal); } this.lastEmittedPage = -1; } }, internalPageCount(newVal) { /* istanbul ignore if */ const oldPage = this.internalCurrentPage; if (newVal > 0 && oldPage === 0) { this.internalCurrentPage = 1; } else if (oldPage > newVal) { this.internalCurrentPage = newVal === 0 ? 1 : newVal; this.userChangePageSize && this.emitChange(); } this.userChangePageSize = false; } } };

pager.vue