index.vue 6.94 KB
Newer Older
1 2
<template>
    <div>
3
        <my-card v-if="$scopedSlots.search">
陈浩玮's avatar
陈浩玮 committed
4
            <a-form-model :model="queryForm" layout="horizontal">
陈浩玮's avatar
陈浩玮 committed
5
                <a-row :gutter="16">
陈浩玮's avatar
陈浩玮 committed
6 7
                    <slot name="search" :query="queryForm" />
                </a-row>
8 9
            </a-form-model>

水落(YangLei)'s avatar
水落(YangLei) committed
10
            <div class="tw-text-right tw-mt-2">
11
                <a-space>
12
                    <a-button @click="reset">重置</a-button>
13 14 15
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
16
        </my-card>
17

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

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

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

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

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

133 134 135 136 137 138 139 140
    computed: {
        pagination() {
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
141
                      showQuickJumper: true,
142 143
                  };
        },
陈浩玮's avatar
陈浩玮 committed
144 145 146 147 148 149 150 151 152
        isRowSelection() {
            return this.rowSelection
                ? {
                      ...this.rowSelection,
                      selectedRowKeys: this.selectedRowKeys,
                      onChange: this.onSelectChange,
                  }
                : undefined;
        },
153 154
    },

155 156 157
    methods: {
        async getData() {
            this.loading = true;
水落(YangLei)'s avatar
水落(YangLei) committed
158
            try {
水落(YangLei)'s avatar
水落(YangLei) committed
159
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
160 161 162 163
            } catch (error) {
                // todo
            }
            this.loading = false;
164
        },
水落(YangLei)'s avatar
水落(YangLei) committed
165
        async getDataNoPage() {
166
            const res = await request(this.url, METHOD.GET, this.queryForm);
水落(YangLei)'s avatar
水落(YangLei) committed
167 168 169 170 171
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

        async getDataWithPage() {
172
            const res = await request(this.url, METHOD.GET, { ...this.initQuery, ...this.queryForm });
173
            this.total = res.total;
水落(YangLei)'s avatar
水落(YangLei) committed
174 175 176 177
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res.records;
        },

178 179
        add() {
            this.addVisible = true;
水落(YangLei)'s avatar
水落(YangLei) committed
180 181
            this.type = 0;
            this.noFooter = false;
182 183 184 185
        },
        addDrawerClose() {
            this.addVisible = false;
        },
186 187 188 189
        async submit() {
            this.submitLoading = true;
            try {
                await this.addBtn?.onOk();
190
                this.addVisible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
191
                this.getData();
192 193 194 195 196
            } catch (error) {
                // todo
            }
            this.submitLoading = false;
        },
197
        show({ type, title, noFooter } = {}) {
陈浩玮's avatar
修改  
陈浩玮 committed
198 199 200 201 202 203 204 205 206 207 208 209
            if (type === 0) {
                this.title = '新增';
            }
            if (type === 1) {
                this.title = '编辑';
            }
            if (type === 2) {
                this.title = '查看';
            }
            if (title) {
                this.title = title;
            }
210
            this.noFooter = noFooter;
211 212
            this.addVisible = true;
        },
213 214 215 216 217 218 219 220 221
        reset() {
            this.queryForm = {};
            this.initQuery = { ...initQuery };
            this.getData();
        },
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
陈浩玮's avatar
陈浩玮 committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

        onSelectChange(selectedRowKeys, selectedRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectedRows = selectedRows;
        },
        handleClose() {
            this.selectedRowKeys = [];
            this.selectedRows = [];
        },
        getSelectedRowKeys() {
            return this.selectedRowKeys;
        },
        getTableData() {
            return this.data;
        },
237 238 239
    },
};
</script>