table.vue 5.45 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 :class="noPadding ? 'tw-p-0' : ''">
水落(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

        <a-drawer
45
            v-if="$scopedSlots.drawer"
46 47 48 49 50
            placement="right"
            :visible="visible"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
            destroyOnClose
51
            :width="drawerWidth"
52 53
            @close="hidden"
            :maskClosable="false"
水落(YangLei)'s avatar
水落(YangLei) committed
54
            :title="title"
55
        >
56
            <slot name="drawer" :hidden="hidden" :refresh="getData" :type="type" :row="row" />
57
        </a-drawer>
水落(YangLei)'s avatar
水落(YangLei) committed
58 59 60 61 62 63
    </div>
</template>

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

64 65
const assign = Object.assign;

水落(YangLei)'s avatar
水落(YangLei) committed
66 67 68 69 70
const initQuery = {
    pageSize: 10,
    pageNum: 1,
};

水落(YangLei)'s avatar
水落(YangLei) committed
71 72
const defaultTitle = '新增';

水落(YangLei)'s avatar
水落(YangLei) committed
73 74 75
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
        selected: Array,
82 83 84 85 86
        drawerWidth: {
            type: Number,
            default: 600,
        },
        noPadding: Boolean,
水落(YangLei)'s avatar
水落(YangLei) committed
87 88 89
    },

    data() {
水落(YangLei)'s avatar
水落(YangLei) committed
90
        const newBtn = this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
水落(YangLei)'s avatar
水落(YangLei) committed
91 92 93 94 95 96 97 98
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
            queryForm: {},
            loading: false,
            total: 0,
99
            visible: false,
水落(YangLei)'s avatar
水落(YangLei) committed
100
            title: newBtn.title ?? defaultTitle,
101 102 103 104 105 106 107 108 109
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
            },
110 111
            type: 'add',
            row: null,
水落(YangLei)'s avatar
水落(YangLei) committed
112 113 114 115 116 117 118 119
        };
    },

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

    computed: {
水落(YangLei)'s avatar
水落(YangLei) committed
120 121 122
        newBtn() {
            return this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
123
        pagination() {
124 125 126 127 128 129 130 131
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                      showQuickJumper: true,
                  };
水落(YangLei)'s avatar
水落(YangLei) committed
132
        },
水落(YangLei)'s avatar
水落(YangLei) committed
133 134 135 136 137 138 139
        rowSelection() {
            return {
                onChange: (selectedRowKeys, selectedRows) => {
                    this.$emit('update:selected', [selectedRowKeys, selectedRows]);
                },
            };
        },
水落(YangLei)'s avatar
水落(YangLei) committed
140 141 142 143 144 145
    },

    methods: {
        async getData() {
            this.loading = true;
            try {
146
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
147 148 149 150 151 152 153 154 155 156 157 158 159
            } 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;
        },

160 161 162 163 164 165
        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
166 167 168 169
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
170 171 172

        hidden() {
            this.visible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
173
            this.title = this.addBtn?.title ?? defaultTitle;
174 175
            this.type = 'add';
            this.row = null;
176
        },
水落(YangLei)'s avatar
水落(YangLei) committed
177

178
        show(params) {
179
            this.visible = true;
180
            assign(this, params);
181
        },
水落(YangLei)'s avatar
水落(YangLei) committed
182

183 184 185 186 187
        reset() {
            this.queryForm = {};
            this.initQuery = { ...initQuery };
            this.getData();
        },
水落(YangLei)'s avatar
水落(YangLei) committed
188

189 190 191 192 193
        addBtnClick() {
            const { click } = typeof this.addBtn === 'object' ? this.addBtn : {};
            click && click();
            this.visible = true;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
194 195 196
    },
};
</script>