table.vue 5.18 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
1 2
<template>
    <div>
水落(YangLei)'s avatar
水落(YangLei) committed
3
        <my-card v-if="$scopedSlots.search" class="tw-mb-2.5">
水落(YangLei)'s avatar
水落(YangLei) committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
            <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
        <my-card>
水落(YangLei)'s avatar
水落(YangLei) committed
19 20 21
            <a-space class="tw-mb-2">
                <a-button type="primary" v-if="newBtn" @click="addBtnClick">
                    {{ newBtn.text || '新增' }}
22 23
                </a-button>
                <slot name="operation" />
水落(YangLei)'s avatar
水落(YangLei) committed
24
            </a-space>
25

水落(YangLei)'s avatar
水落(YangLei) committed
26 27 28 29
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
水落(YangLei)'s avatar
水落(YangLei) committed
30
                :rowKey="rowKey"
水落(YangLei)'s avatar
水落(YangLei) committed
31 32
                :pagination="pagination"
                @change="pageChange"
水落(YangLei)'s avatar
水落(YangLei) committed
33
                :row-selection="selected ? rowSelection : undefined"
水落(YangLei)'s avatar
水落(YangLei) committed
34 35
            >
                <slot />
36 37 38 39 40
                <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
41 42
            </a-table>
        </my-card>
43 44 45 46 47 48 49 50 51 52

        <a-drawer
            placement="right"
            :visible="visible"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
            destroyOnClose
            :width="600"
            @close="hidden"
            :maskClosable="false"
水落(YangLei)'s avatar
水落(YangLei) committed
53
            :title="title"
54
        >
水落(YangLei)'s avatar
水落(YangLei) committed
55
            <slot name="drawer" :hidden="hidden" :refresh="getData" />
56
        </a-drawer>
水落(YangLei)'s avatar
水落(YangLei) committed
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,
};

水落(YangLei)'s avatar
水落(YangLei) committed
68 69
const defaultTitle = '新增';

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

    data() {
水落(YangLei)'s avatar
水落(YangLei) committed
82
        const newBtn = this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
水落(YangLei)'s avatar
水落(YangLei) committed
83 84 85 86 87 88 89 90
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
            queryForm: {},
            loading: false,
            total: 0,
91
            visible: false,
水落(YangLei)'s avatar
水落(YangLei) committed
92
            title: newBtn.title ?? defaultTitle,
93 94 95 96 97 98 99 100 101
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
            },
水落(YangLei)'s avatar
水落(YangLei) committed
102 103 104 105 106 107 108 109
        };
    },

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

    computed: {
水落(YangLei)'s avatar
水落(YangLei) committed
110 111 112
        newBtn() {
            return this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
113
        pagination() {
114 115 116 117 118 119 120 121
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                      showQuickJumper: true,
                  };
水落(YangLei)'s avatar
水落(YangLei) committed
122
        },
水落(YangLei)'s avatar
水落(YangLei) committed
123 124 125 126 127 128 129
        rowSelection() {
            return {
                onChange: (selectedRowKeys, selectedRows) => {
                    this.$emit('update:selected', [selectedRowKeys, selectedRows]);
                },
            };
        },
水落(YangLei)'s avatar
水落(YangLei) committed
130 131 132 133 134 135
    },

    methods: {
        async getData() {
            this.loading = true;
            try {
136
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
137 138 139 140 141 142 143 144 145 146 147 148 149
            } 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;
        },

150 151 152 153 154 155
        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
156 157 158 159
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
160 161 162

        hidden() {
            this.visible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
163
            this.title = this.addBtn?.title ?? defaultTitle;
164
        },
水落(YangLei)'s avatar
水落(YangLei) committed
165

166 167 168 169
        show({ title } = {}) {
            this.visible = true;
            if (title) this.title = title;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
170

171 172 173 174 175
        reset() {
            this.queryForm = {};
            this.initQuery = { ...initQuery };
            this.getData();
        },
水落(YangLei)'s avatar
水落(YangLei) committed
176

177 178 179 180 181
        addBtnClick() {
            const { click } = typeof this.addBtn === 'object' ? this.addBtn : {};
            click && click();
            this.visible = true;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
182 183 184 185

        getFormRef(ref) {
            console.log(ref);
        },
水落(YangLei)'s avatar
水落(YangLei) committed
186 187 188
    },
};
</script>