element-ui 组件扩展 - 组合table 和 pagination组件

element-ui 中table 组件不带有分页,必须配合pagination组件来完成表格的分页功能,这对列表页面很多的系统来说,每次都需要定义分页组件相关的属性和事件,似乎做了很多重复的工作。下面通过写一个扩展组建来把table和pagination组件封装起来。

//table.js
import { Table, Pagination } from 'element-ui';
import Vue from 'vue';

export default {
    name: 'ExTable',
    mixins: [Table],
    props: {
        showPagination: {
            type: Boolean,
            default: true
        },
        pager_layout: {
            type: String,
            default: 'total,sizes,prev, pager, next, jumper'
        },
        pageSize: {
            type: Number,
            default: 10
        },
        pageSizes: {
            type: Array,
            default: () => [10, 25, 50, 100]
        },
        searchMethod: {
            type: Function,
            default: () => {}
        }
    },
    data() {
        return {
            pagination: null
        }
    },
    methods: {
        setCurrentPage(page) {
            this.pagination.currentPage = page;
            this.fetchData();
        },
        setPageSize(size) {
            this.pagination.pageSize = size;
            if (this.pagination.currentPage === 1) {
                this.fetchData();
            } else {
                this.pagination.currentPage = 1;
            }
        },
        fetchData() {
            this.searchMethod(this.pagination, {
                currentPage: this.pagination.currentPage,
                pageSize: this.pagination.pageSize
            });
        },
        mountPagination() {
            const container = document.createElement('div');
            const parent = this.$el.parentNode;
            if (parent.lastChild == this.$el) {
                parent.appentChild(container);
            } else {
                parent.insertBefore(container, this.$el.nextSibling);
            }
            const Pager = Vue.extend(Pagination);
            this.pagination = new Pager({
                components: { Pagination },
                propsData: {
                    pageSize: this.pageSize,
                    pageSizes: this.pageSizes,
                    layout: this.pager_layout,
                    total: 0,
                    currentPage: 1
                }
            });
            this.pagination.$on('current-change', this.setCurrentPage);
            this.pagination.$on('size-change', this.setPageSize);
            this.pagination.$mount(container);
        }
    },
    mounted() {
        if (this.showPagination) {
            this.mountPagination();
        }
    },
    beforeDestroy() {
        this.pagination.$off('current-change', this.setCurrentPage);
        this.pagination.$off('size-change', this.setPageSize);
    }
}

使用:

<template>
    <div>
        <ex-table :data="data" show-pagination :search-method="handleSearch" ref="table">
            <el-table-column label="column1" prop="prop1" />
        </ex-table>
        <el-button @click="$refs.table.setCurrentPage(1)">回到首页</el-button>
    </div>
</template>
<script>
import ExTable from './table.js';
export default {
    components: { ExTable },
    data() {
        return {
            data: []
        }
    },
    methods: {
        handleSearch(pagination, { currentPage, pageSize }) {
            this.fetchRemoteData(currentPage, pageSize)
        },
        fetchRemoteData(currentPage, pageSize) {
            //currentPage:当前页, pageSize: 每页最大条目数, 用于服务端分页
            //假设http请求数据,结果返回{data_list: [...], total: ..}
            //设置表格数据
            this.data = request.data_list;
            //设置分页总数
            const pagination = this.$refs.table.pagination;
            pagination .total = request.total;
        },
    },
    mounted() {
        this.$refs.table.fetchData();
    }
}
</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • 有一个姓蔡的老板,盘下了兴华街上几间店面,开了一家酒馆。几挂鞭炮一放,小酒馆就开了张。蔡老板带着伙计站在门口笑盈盈...
    咪辰阅读 2,656评论 0 0
  • 年年岁岁花相似 岁岁年年人不同 此时此刻 无论你是陪伴在家人身边 共享团圆宴 还是因事不能回家 眺望故乡的万家灯火...
    奇异果小姐阅读 1,495评论 0 2
  • 谭小二der阅读 968评论 0 0
  • 没画的时候一直在想要怎么抽象的画出来,实际画的时候并没有想象中的那么复杂,自己总是看把这些复杂化,嘿嘿,以后需要大...
    哗啦啦的小河流水阅读 1,118评论 1 1