table.vue 2.35 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) 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
<template>
    <div>
        <my-card v-if="$scopedSlots.search">
            <a-form-model :model="queryForm" layout="horizontal">
                <a-row :gutter="16">
                    <slot name="search" :query="queryForm" />
                </a-row>
            </a-form-model>

            <div class="tw-text-right tw-mt-2">
                <a-space>
                    <a-button @click="reset">重置</a-button>
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
        </my-card>

        <my-card class="tw-mt-3">
            <a-space class="tw-mb-2.5">
                <slot name="action" />
            </a-space>
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
                :pagination="pagination"
                @change="pageChange"
            >
                <slot />
            </a-table>
        </my-card>
    </div>
</template>

<script>
import { request, METHOD } from '@/utils/requestUtil';

const initQuery = {
    pageSize: 10,
    pageNum: 1,
};

export default {
    props: {
        url: String,
    },

    data() {
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
            queryForm: {},
            loading: false,
            total: 0,
        };
    },

    mounted() {
        this.getData();
    },

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

    methods: {
        async getData() {
            this.loading = true;
            try {
                await this.getDataWithPage();
            } catch (error) {
                // todo
            }
            this.loading = false;
        },

        async getDataWithPage() {
            const res = await request(this.url, METHOD.GET, { ...this.initQuery, ...this.queryForm });
            this.total = res.total;
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res.records;
        },

        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
    },
};
</script>