index.vue 7.41 KB
Newer Older
1 2
<template>
    <div>
3
        <my-card v-if="$scopedSlots.search">
shuiluo's avatar
shuiluo committed
4 5 6
            <div class="tw-flex">
                <slot name="search" :query="queryForm" />
            </div>
7

水落(YangLei)'s avatar
水落(YangLei) committed
8
            <div class="tw-text-right tw-mt-2">
9
                <a-space>
shuiluo's avatar
shuiluo committed
10 11
                    <a-button @click="reset">{{ $t('table.reset') }}</a-button>
                    <a-button type="primary" @click="getData">{{ $t('table.search') }}</a-button>
12 13
                </a-space>
            </div>
14
        </my-card>
15

陈浩玮's avatar
陈浩玮 committed
16
        <my-card :class="$scopedSlots.search ? 'tw-mt-3' : ''">
17 18
            <a-space class="tw-mb-2.5">
                <a-button v-if="addBtn" type="primary" key="add" v-bind="addBtn.options" @click="add">
shuiluo's avatar
shuiluo committed
19
                    {{ addBtn.text || $t('table.add') }}
20
                </a-button>
陈浩玮's avatar
陈浩玮 committed
21
                <slot name="buttons" />
22
            </a-space>
陈浩玮's avatar
陈浩玮 committed
23 24 25
            <div class="tw-mb-2">
                <a-alert
                    v-if="selectedRowKeys.length"
shuiluo's avatar
shuiluo committed
26
                    :message="$t('table.hasSelectedSome', { num: selectedRowKeys.length })"
陈浩玮's avatar
陈浩玮 committed
27 28 29 30 31
                    type="success"
                    closable
                    :after-close="handleClose"
                />
            </div>
32 33 34 35 36 37
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
                :pagination="pagination"
                @change="pageChange"
陈浩玮's avatar
陈浩玮 committed
38
                :row-selection="isRowSelection"
39
            >
40 41
                <slot />
            </a-table>
42
        </my-card>
43 44 45

        <!-- 新增 -->
        <a-drawer
陈浩玮's avatar
修改  
陈浩玮 committed
46
            :title="title"
47 48 49
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
50 51
            v-if="$slots.drawer"
            :maskClosable="addBtn && !!addBtn.maskClosable"
52 53
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
54
            :width="(addBtn && addBtn.width) || 600"
55
            destroyOnClose
56
        >
水落(YangLei)'s avatar
水落(YangLei) committed
57
            <div class="tw-overflow-y-hidden tw-flex-1">
58
                <div class="tw-overflow-y-auto tw-h-full">
陈浩玮's avatar
修改  
陈浩玮 committed
59
                    <slot name="drawer" />
60 61
                </div>
            </div>
62 63
            <template v-if="!noFooter">
                <a-divider />
陈浩玮's avatar
陈浩玮 committed
64
                <a-space class="tw-justify-end">
shuiluo's avatar
shuiluo committed
65 66 67 68
                    <a-button @click="addVisible = false">{{ $t('table.cancel') }}</a-button>
                    <a-button type="primary" @click="submit" :loading="submitLoading">
                        {{ $t('table.confirm') }}
                    </a-button>
69 70
                </a-space>
            </template>
71
        </a-drawer>
水落(YangLei)'s avatar
水落(YangLei) committed
72
        <slot name="children" />
73 74 75 76 77 78
    </div>
</template>

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

79 80 81 82 83
const initQuery = {
    pageSize: 10,
    pageNum: 1,
};

84 85 86
export default {
    props: {
        url: String,
87 88
        buttons: { type: Array, default: () => [] },
        addBtn: { type: Object },
水落(YangLei)'s avatar
水落(YangLei) committed
89
        formatData: Function,
90
        noPage: Boolean,
陈浩玮's avatar
陈浩玮 committed
91
        rowSelection: Object, //  radio  OR  checkbox
陈浩玮's avatar
陈浩玮 committed
92 93 94 95
        tableParam: {
            type: Object,
            default: () => ({}),
        },
96 97 98
    },
    data() {
        return {
99 100
            initQuery: {
                ...initQuery,
陈浩玮's avatar
陈浩玮 committed
101
                ...this.tableParam,
102
            },
陈浩玮's avatar
修改  
陈浩玮 committed
103
            title: '新增',
104
            data: [],
105
            queryForm: {},
106 107
            loading: false,
            addVisible: false,
108 109 110 111 112 113 114 115 116 117 118 119
            submitLoading: false,
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
                display: 'flex',
                flexDirection: 'column',
            },
120
            noFooter: false,
陈浩玮's avatar
陈浩玮 committed
121 122 123 124
            layout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
            },
125
            total: 0,
陈浩玮's avatar
陈浩玮 committed
126 127
            selectedRowKeys: [],
            selectedRows: [],
128 129
        };
    },
130 131
    watch: {
        addVisible(val) {
水落(YangLei)'s avatar
水落(YangLei) committed
132
            if (!val && this?.addBtn?.onCancel) this.addBtn.onCancel();
133
        },
陈浩玮's avatar
陈浩玮 committed
134 135 136
        tableParam(val) {
            this.reset();
        },
137
    },
138 139 140 141
    mounted() {
        this.getData();
    },

142 143 144 145 146 147 148 149
    computed: {
        pagination() {
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
150
                      showQuickJumper: true,
151 152
                  };
        },
陈浩玮's avatar
陈浩玮 committed
153 154 155 156 157 158 159 160 161
        isRowSelection() {
            return this.rowSelection
                ? {
                      ...this.rowSelection,
                      selectedRowKeys: this.selectedRowKeys,
                      onChange: this.onSelectChange,
                  }
                : undefined;
        },
162 163
    },

164 165 166
    methods: {
        async getData() {
            this.loading = true;
水落(YangLei)'s avatar
水落(YangLei) committed
167
            try {
水落(YangLei)'s avatar
水落(YangLei) committed
168
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
169 170 171 172
            } catch (error) {
                // todo
            }
            this.loading = false;
173
        },
水落(YangLei)'s avatar
水落(YangLei) committed
174
        async getDataNoPage() {
175
            const res = await request(this.url, METHOD.GET, this.queryForm);
陈浩玮's avatar
陈浩玮 committed
176
            if (this.formatData) this.data = await this.formatData(res);
水落(YangLei)'s avatar
水落(YangLei) committed
177 178 179 180
            else this.data = res;
        },

        async getDataWithPage() {
181
            const res = await request(this.url, METHOD.GET, { ...this.initQuery, ...this.queryForm });
182
            this.total = res.total;
陈浩玮's avatar
陈浩玮 committed
183
            if (this.formatData) this.data = await this.formatData(res);
水落(YangLei)'s avatar
水落(YangLei) committed
184 185 186
            else this.data = res.records;
        },

187 188
        add() {
            this.addVisible = true;
水落(YangLei)'s avatar
水落(YangLei) committed
189 190
            this.type = 0;
            this.noFooter = false;
shuiluo's avatar
shuiluo committed
191
            this.title = this.$t('table.add');
192 193 194 195
        },
        addDrawerClose() {
            this.addVisible = false;
        },
196 197 198 199
        async submit() {
            this.submitLoading = true;
            try {
                await this.addBtn?.onOk();
200
                this.addVisible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
201
                this.getData();
202 203 204 205 206
            } catch (error) {
                // todo
            }
            this.submitLoading = false;
        },
207
        show({ type, title, noFooter } = {}) {
陈浩玮's avatar
修改  
陈浩玮 committed
208
            if (type === 0) {
shuiluo's avatar
shuiluo committed
209
                this.title = this.$t('table.add');
陈浩玮's avatar
修改  
陈浩玮 committed
210 211
            }
            if (type === 1) {
shuiluo's avatar
shuiluo committed
212
                this.title = this.$t('table.edit');
陈浩玮's avatar
修改  
陈浩玮 committed
213 214
            }
            if (type === 2) {
shuiluo's avatar
shuiluo committed
215
                this.title = this.$t('table.view');
陈浩玮's avatar
修改  
陈浩玮 committed
216 217 218 219
            }
            if (title) {
                this.title = title;
            }
220
            this.noFooter = noFooter;
221 222
            this.addVisible = true;
        },
223 224
        reset() {
            this.queryForm = {};
陈浩玮's avatar
陈浩玮 committed
225
            this.initQuery = { ...initQuery, ...this.tableParam };
226 227 228 229 230 231
            this.getData();
        },
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
陈浩玮's avatar
陈浩玮 committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

        onSelectChange(selectedRowKeys, selectedRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectedRows = selectedRows;
        },
        handleClose() {
            this.selectedRowKeys = [];
            this.selectedRows = [];
        },
        getSelectedRowKeys() {
            return this.selectedRowKeys;
        },
        getTableData() {
            return this.data;
        },
陈浩玮's avatar
陈浩玮 committed
247 248 249
        setTableData(data) {
            this.data = data;
        },
250 251 252
    },
};
</script>