table.vue 4.37 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
<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>

18 19 20 21 22 23
        <my-card>
            <a-space :class="{ 'tw-mb-2.5': !!$scopedSlots.search }">
                <a-button type="primary" v-if="addBtn" @click="addBtnClick">
                    {{ typeof addBtn === 'object' ? addBtn.text : '新增' }}
                </a-button>
                <slot name="operation" />
水落(YangLei)'s avatar
水落(YangLei) committed
24
            </a-space>
25

水落(YangLei)'s avatar
水落(YangLei) committed
26 27 28 29 30 31 32 33
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
                :pagination="pagination"
                @change="pageChange"
            >
                <slot />
34 35 36 37 38
                <a-table-column title="操作" v-if="buttons">
                    <template #default="row">
                        <my-ac-btn :row="row" :buttons="buttons" />
                    </template>
                </a-table-column>
水落(YangLei)'s avatar
水落(YangLei) committed
39 40
            </a-table>
        </my-card>
41 42 43 44 45 46 47 48 49 50 51 52 53

        <a-drawer
            placement="right"
            :visible="visible"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
            destroyOnClose
            :width="600"
            @close="hidden"
            :maskClosable="false"
        >
            <slot name="drawer" :hidden="hidden" />
        </a-drawer>
水落(YangLei)'s avatar
水落(YangLei) committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67
    </div>
</template>

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

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

export default {
    props: {
        url: String,
68 69 70 71
        addBtn: [Object, Boolean],
        buttons: Array,
        noPage: Boolean,
        formatData: Function,
水落(YangLei)'s avatar
水落(YangLei) committed
72 73 74 75 76 77 78 79 80 81 82
    },

    data() {
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
            queryForm: {},
            loading: false,
            total: 0,
83 84 85 86 87 88 89 90 91 92 93
            visible: false,
            title: '新增',
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
            },
水落(YangLei)'s avatar
水落(YangLei) committed
94 95 96 97 98 99 100 101 102
        };
    },

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

    computed: {
        pagination() {
103 104 105 106 107 108 109 110
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                      showQuickJumper: true,
                  };
水落(YangLei)'s avatar
水落(YangLei) committed
111 112 113 114 115 116 117
        },
    },

    methods: {
        async getData() {
            this.loading = true;
            try {
118
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
119 120 121 122 123 124 125 126 127 128 129 130 131
            } 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;
        },

132 133 134 135 136 137
        async getDataNoPage() {
            const res = await request(this.url, METHOD.GET, this.queryForm);
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

水落(YangLei)'s avatar
水落(YangLei) committed
138 139 140 141
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

        hidden() {
            this.visible = false;
        },
        show({ title } = {}) {
            this.visible = true;
            if (title) this.title = title;
        },
        reset() {
            this.queryForm = {};
            this.initQuery = { ...initQuery };
            this.getData();
        },
        addBtnClick() {
            const { click } = typeof this.addBtn === 'object' ? this.addBtn : {};
            click && click();
            this.visible = true;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
160 161 162
    },
};
</script>