table.vue 4.74 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
1 2 3 4 5 6 7 8 9 10 11
<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>
水落(YangLei)'s avatar
水落(YangLei) committed
12 13 14 15 16 17 18
                    <a-popover title="Title">
                        <template slot="content">
                            <p>Content</p>
                            <p>Content</p>
                        </template>
                        <a-button>高级查询</a-button>
                    </a-popover>
水落(YangLei)'s avatar
水落(YangLei) committed
19 20 21 22 23 24
                    <a-button @click="reset">重置</a-button>
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
        </my-card>

25 26 27 28 29 30
        <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
31
            </a-space>
32

水落(YangLei)'s avatar
水落(YangLei) committed
33 34 35 36
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
水落(YangLei)'s avatar
水落(YangLei) committed
37
                :rowKey="rowKey"
水落(YangLei)'s avatar
水落(YangLei) committed
38 39 40 41
                :pagination="pagination"
                @change="pageChange"
            >
                <slot />
42 43 44 45 46
                <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
47 48
            </a-table>
        </my-card>
49 50 51 52 53 54 55 56 57 58 59 60 61

        <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
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    </div>
</template>

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

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

export default {
    props: {
        url: String,
76 77 78 79
        addBtn: [Object, Boolean],
        buttons: Array,
        noPage: Boolean,
        formatData: Function,
水落(YangLei)'s avatar
水落(YangLei) committed
80
        rowKey: [String, Function],
水落(YangLei)'s avatar
水落(YangLei) committed
81 82 83 84 85 86 87 88 89 90 91
    },

    data() {
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
            queryForm: {},
            loading: false,
            total: 0,
92 93 94 95 96 97 98 99 100 101 102
            visible: false,
            title: '新增',
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
            },
水落(YangLei)'s avatar
水落(YangLei) committed
103 104 105 106 107 108 109 110 111
        };
    },

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

    computed: {
        pagination() {
112 113 114 115 116 117 118 119
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                      showQuickJumper: true,
                  };
水落(YangLei)'s avatar
水落(YangLei) committed
120 121 122 123 124 125 126
        },
    },

    methods: {
        async getData() {
            this.loading = true;
            try {
127
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
128 129 130 131 132 133 134 135 136 137 138 139 140
            } 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;
        },

141 142 143 144 145 146
        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
147 148 149 150
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

        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
169 170 171
    },
};
</script>