SimpleTable.vue 2.59 KB
Newer Older
陈浩玮's avatar
陈浩玮 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<template>
    <a-row :gutter="[24, 24]">
        <a-col :span="24" v-if="$scopedSlots.search">
            <slot name="search" />
        </a-col>
        <a-col :span="24">
            <a-table
                :dataSource="dataSource"
                :loading="loading"
                :pagination="pagination"
                @change="pageChange"
                v-bind="$attrs"
                rowKey="key"
            >
                <slot />
            </a-table>
        </a-col>
    </a-row>
</template>

<script>
const initQuery = {
    pageNum: 1,
    pageSize: 10,
    total: 0,
};

export default {
    props: {
        request: { type: Function, default: undefined },
        otherParams: { type: Object, default: () => ({}) },
        rowKey: String,
    },
    data() {
        return {
            loading: true,
            dataSource: [],
            params: {
                ...initQuery,
            },
        };
    },

    computed: {
        pagination() {
            return {
                current: this.params.pageNum,
                pageSize: this.params.pageSize,
                total: this.params.total,
                showQuickJumper: true,
            };
        },
    },

    watch: {
        otherParams: {
            handler() {
                this.refresh();
            },
            deep: true,
            immediate: true,
        },
    },

    mounted() {
        this.getList({ ...this.params, ...this.otherParams });
    },

    methods: {
        async getList(params) {
            if (!this.request) return;
            this.loading = true;
            const data = await this.request(params);
            const { records, total, pageNum } = data;
            const newData = records.map((i) => ({
                ...i,
                key: i[this.rowKey],
                editable: false,
            }));
            this.dataSource = newData;
            this.params = {
                ...this.params,
                total,
                pageNum,
            };
            this.loading = false;
        },
        refresh() {
            const params = {
                ...initQuery,
                ...this.otherParams,
            };
            this.getList(params);
        },
        pageChange(pagination) {
            const params = {
                ...this.params,
                ...this.otherParams,
                pageNum: pagination.current,
            };
            this.getList(params);
        },
        getTableData() {
            return this.dataSource;
        },
        setTableData(data) {
            this.dataSource = data;
        },
    },
};
</script>