table.vue 7.72 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
1
<template>
水落(YangLei)'s avatar
水落(YangLei) committed
2
    <div :style="`min-width: ${width}px;`" class="tw-overflow-x-auto">
水落(YangLei)'s avatar
水落(YangLei) committed
3
        <my-card v-if="$scopedSlots.search" :class="{ 'tw-mb-2.5': !noMargin }">
水落(YangLei)'s avatar
水落(YangLei) committed
4
            <div class="tw-flex">
水落(YangLei)'s avatar
水落(YangLei) committed
5
                <slot name="search" :query="queryForm" />
水落(YangLei)'s avatar
水落(YangLei) committed
6
            </div>
水落(YangLei)'s avatar
水落(YangLei) committed
7 8 9

            <div class="tw-text-right tw-mt-2">
                <a-space>
水落(YangLei)'s avatar
水落(YangLei) committed
10 11 12 13 14 15
                    <a-popover v-if="$scopedSlots.moreSearch" trigger="click">
                        <template slot="content">
                            <div :class="$style.moreSearch">
                                <slot name="moreSearch" :query="queryForm" />
                            </div>
                        </template>
16
                        <a-button>{{ $t('table.moreSearch') }}</a-button>
水落(YangLei)'s avatar
水落(YangLei) committed
17
                    </a-popover>
18

19
                    <a-button @click="reset">{{ $t('table.reset') }}</a-button>
20
                    <a-button type="primary" @click="resetGetData">{{ $t('table.search') }}</a-button>
水落(YangLei)'s avatar
水落(YangLei) committed
21 22 23 24
                </a-space>
            </div>
        </my-card>

水落(YangLei)'s avatar
水落(YangLei) committed
25
        <my-card :class="noPadding ? 'tw-p-0' : ''" :style="tableCardStyle">
水落(YangLei)'s avatar
水落(YangLei) committed
26 27
            <a-space class="tw-mb-2">
                <a-button type="primary" v-if="newBtn" @click="addBtnClick">
28
                    {{ newBtn.text || $t('table.add') }}
29 30
                </a-button>
                <slot name="operation" />
水落(YangLei)'s avatar
水落(YangLei) committed
31
            </a-space>
32

水落(YangLei)'s avatar
水落(YangLei) committed
33 34 35 36
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
水落(YangLei)'s avatar
水落(YangLei) committed
37
                :scroll="scroll"
水落(YangLei)'s avatar
水落(YangLei) committed
38
                :rowKey="rowKey"
水落(YangLei)'s avatar
水落(YangLei) committed
39 40
                :pagination="pagination"
                @change="pageChange"
陈浩玮's avatar
tijiao  
陈浩玮 committed
41
                v-on="$listeners"
水落(YangLei)'s avatar
水落(YangLei) committed
42
                :row-selection="selected ? rowSelection : undefined"
水落(YangLei)'s avatar
水落(YangLei) committed
43 44
            >
                <slot />
水落(YangLei)'s avatar
水落(YangLei) committed
45
                <a-table-column
46
                    :title="newButtons.title || $t('table.operation')"
水落(YangLei)'s avatar
水落(YangLei) committed
47 48 49
                    v-if="this.newButtons"
                    v-bind="this.newButtons.options"
                >
50
                    <template #default="row">
水落(YangLei)'s avatar
水落(YangLei) committed
51
                        <my-ac-btn :row="row" :buttons="newButtons.data" />
52 53
                    </template>
                </a-table-column>
水落(YangLei)'s avatar
水落(YangLei) committed
54 55
            </a-table>
        </my-card>
56

陈浩玮's avatar
tijiao  
陈浩玮 committed
57 58
        <slot name="other" />

59
        <a-drawer
60
            v-if="$scopedSlots.drawer"
61 62 63 64 65
            placement="right"
            :visible="visible"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
            destroyOnClose
陈浩玮's avatar
tijiao  
陈浩玮 committed
66
            :width="dWidth"
67 68
            @close="hidden"
            :maskClosable="false"
69
            :title="drawerTitle"
水落(YangLei)'s avatar
水落(YangLei) committed
70
            :afterVisibleChange="afterVisibleChange"
71
        >
72
            <slot name="drawer" :hidden="hidden" :refresh="getData" :type="type" :row="row" />
73
        </a-drawer>
水落(YangLei)'s avatar
水落(YangLei) committed
74 75 76 77 78 79
    </div>
</template>

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

80 81
const assign = Object.assign;

水落(YangLei)'s avatar
水落(YangLei) committed
82 83 84 85 86 87 88 89
const initQuery = {
    pageSize: 10,
    pageNum: 1,
};

export default {
    props: {
        url: String,
90
        addBtn: [Object, Boolean],
水落(YangLei)'s avatar
水落(YangLei) committed
91
        buttons: [Array, Object],
92 93
        noPage: Boolean,
        formatData: Function,
水落(YangLei)'s avatar
水落(YangLei) committed
94
        rowKey: [String, Function],
陈浩玮's avatar
地点  
陈浩玮 committed
95
        selected: Object,
96 97 98 99 100
        drawerWidth: {
            type: Number,
            default: 600,
        },
        noPadding: Boolean,
水落(YangLei)'s avatar
水落(YangLei) committed
101
        scroll: Object,
水落(YangLei)'s avatar
水落(YangLei) committed
102
        width: { type: Number, default: 900 },
水落(YangLei)'s avatar
水落(YangLei) committed
103 104 105 106
        defaultQuery: {
            type: Object,
            default: () => ({}),
        },
水落(YangLei)'s avatar
水落(YangLei) committed
107
        noMargin: Boolean,
陈浩玮's avatar
tijiao  
陈浩玮 committed
108 109 110 111
        otherQuery: {
            type: Object,
            default: () => ({}),
        },
水落(YangLei)'s avatar
水落(YangLei) committed
112 113 114 115 116 117 118 119
    },

    data() {
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
水落(YangLei)'s avatar
水落(YangLei) committed
120
            queryForm: { ...this.defaultQuery },
水落(YangLei)'s avatar
水落(YangLei) committed
121 122
            loading: false,
            total: 0,
123
            visible: false,
124
            title: null,
125 126 127 128 129 130 131 132 133
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
            },
水落(YangLei)'s avatar
水落(YangLei) committed
134
            type: null,
135
            row: null,
陈浩玮's avatar
tijiao  
陈浩玮 committed
136
            dWidth: this.drawerWidth,
水落(YangLei)'s avatar
水落(YangLei) committed
137 138 139
        };
    },

陈浩玮's avatar
tijiao  
陈浩玮 committed
140 141 142 143 144 145 146 147 148 149
    watch: {
        otherQuery: {
            handler(val) {
                this.otherQuery = val;
            },
            deep: true,
            immediate: true,
        },
    },

水落(YangLei)'s avatar
水落(YangLei) committed
150 151 152 153 154
    mounted() {
        this.getData();
    },

    computed: {
水落(YangLei)'s avatar
水落(YangLei) committed
155 156 157
        newBtn() {
            return this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
158 159 160
        newButtons() {
            return this.buttons
                ? Array.isArray(this.buttons)
161
                    ? { data: this.buttons }
水落(YangLei)'s avatar
水落(YangLei) committed
162 163 164
                    : this.buttons
                : null;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
165
        pagination() {
166 167 168 169 170 171 172 173
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                      showQuickJumper: true,
                  };
水落(YangLei)'s avatar
水落(YangLei) committed
174
        },
水落(YangLei)'s avatar
水落(YangLei) committed
175 176 177
        rowSelection() {
            return {
                onChange: (selectedRowKeys, selectedRows) => {
陈浩玮's avatar
地点  
陈浩玮 committed
178
                    this.$emit('update:selected', { keys: selectedRowKeys, rows: selectedRows });
水落(YangLei)'s avatar
水落(YangLei) committed
179 180 181
                },
            };
        },
水落(YangLei)'s avatar
水落(YangLei) committed
182 183 184 185 186 187 188
        tableCardStyle() {
            return this.noMargin
                ? {
                      paddingTop: 0,
                  }
                : {};
        },
189 190 191
        drawerTitle() {
            return this.title ?? this.addBtn?.title ?? this.$t('table.add');
        },
水落(YangLei)'s avatar
水落(YangLei) committed
192 193 194 195 196 197
    },

    methods: {
        async getData() {
            this.loading = true;
            try {
198
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
199 200 201 202 203 204 205
            } catch (error) {
                // todo
            }
            this.loading = false;
        },

        async getDataWithPage() {
陈浩玮's avatar
tijiao  
陈浩玮 committed
206 207 208 209 210
            const res = await request(this.url, METHOD.GET, {
                ...this.initQuery,
                ...this.queryForm,
                ...this.otherQuery,
            });
水落(YangLei)'s avatar
水落(YangLei) committed
211
            this.total = res.total;
陈浩玮's avatar
tijiao  
陈浩玮 committed
212
            if (this.formatData) this.data = this.formatData({ ...res });
水落(YangLei)'s avatar
水落(YangLei) committed
213 214 215
            else this.data = res.records;
        },

216
        async getDataNoPage() {
陈浩玮's avatar
tijiao  
陈浩玮 committed
217
            const res = await request(this.url, METHOD.GET, { ...this.queryForm, ...this.otherQuery });
218 219 220 221
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

水落(YangLei)'s avatar
水落(YangLei) committed
222 223 224 225
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
226 227 228

        hidden() {
            this.visible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
229 230 231
        },
        afterVisibleChange(visible) {
            if (!visible) {
232
                this.title = null;
水落(YangLei)'s avatar
水落(YangLei) committed
233 234
                this.type = null;
                this.row = null;
陈浩玮's avatar
tijiao  
陈浩玮 committed
235
                this.dWidth = this.drawerWidth;
水落(YangLei)'s avatar
水落(YangLei) committed
236
            }
237
        },
水落(YangLei)'s avatar
水落(YangLei) committed
238

239
        show(params) {
240
            this.visible = true;
241
            assign(this, params);
242
        },
水落(YangLei)'s avatar
水落(YangLei) committed
243

244
        reset() {
水落(YangLei)'s avatar
水落(YangLei) committed
245
            this.queryForm = { ...this.defaultQuery };
246 247 248
            this.initQuery = { ...initQuery };
            this.getData();
        },
水落(YangLei)'s avatar
水落(YangLei) committed
249

陈浩玮's avatar
陈浩玮 committed
250 251 252 253 254
        resetGetData() {
            this.initQuery = { ...initQuery };
            this.getData();
        },

陈浩玮's avatar
tijiao  
陈浩玮 committed
255 256 257 258
        refreshView() {
            this.data = [...this.data];
        },

259 260 261 262 263
        addBtnClick() {
            const { click } = typeof this.addBtn === 'object' ? this.addBtn : {};
            click && click();
            this.visible = true;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
264 265 266
    },
};
</script>
水落(YangLei)'s avatar
水落(YangLei) committed
267 268 269 270 271 272 273 274 275 276 277 278 279

<style module lang="less">
.moreSearch {
    display: flex;
    flex-direction: column;
    width: 400px;
    padding: 15px 0 0 0;

    > div {
        @apply tw-mb-3;
    }
}
</style>