index.vue 7.06 KB
Newer Older
1 2 3
<template>
    <div>
        <div :class="$style.card" v-if="$scopedSlots.search">
陈浩玮's avatar
陈浩玮 committed
4 5 6 7
            <a-form-model :model="queryForm" layout="horizontal">
                <a-row :gutter="24">
                    <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 16 17 18 19 20 21 22 23 24 25 26
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
        </div>

        <div class="tw-mt-3" :class="$style.card">
            <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>
                <template v-for="btn of buttons">
                    <a-button :type="btn.primary" :key="btn.text">{{ btn.text }}</a-button>
                </template>
            </a-space>
陈浩玮's avatar
陈浩玮 committed
27 28 29 30 31 32 33 34 35
            <div class="tw-mb-2">
                <a-alert
                    v-if="selectedRowKeys.length"
                    :message="`已选择${selectedRowKeys.length}项`"
                    type="success"
                    closable
                    :after-close="handleClose"
                />
            </div>
36 37 38 39 40 41
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
                :pagination="pagination"
                @change="pageChange"
陈浩玮's avatar
陈浩玮 committed
42
                :row-selection="isRowSelection"
43
            >
44 45 46 47 48 49
                <slot />
            </a-table>
        </div>

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

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

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

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

140 141 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,
                  };
        },
陈浩玮's avatar
陈浩玮 committed
150 151 152 153 154 155 156 157 158
        isRowSelection() {
            return this.rowSelection
                ? {
                      ...this.rowSelection,
                      selectedRowKeys: this.selectedRowKeys,
                      onChange: this.onSelectChange,
                  }
                : undefined;
        },
159 160
    },

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

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

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

        onSelectChange(selectedRowKeys, selectedRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectedRows = selectedRows;
        },
        handleClose() {
            this.selectedRowKeys = [];
            this.selectedRows = [];
        },
        getSelectedRowKeys() {
            return this.selectedRowKeys;
        },
        getTableData() {
            return this.data;
        },
241 242 243 244 245 246
    },
};
</script>

<style module>
.card {
水落(YangLei)'s avatar
水落(YangLei) committed
247
    @apply tw-bg-white tw-rounded-md tw-p-4;
248 249
}
</style>