table.vue 7.44 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">
水落(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 16 17
                    <a-popover v-if="$scopedSlots.moreSearch" trigger="click">
                        <template slot="content">
                            <div :class="$style.moreSearch">
                                <slot name="moreSearch" :query="queryForm" />
                            </div>
                        </template>
                        <a-button>更多查询</a-button>
                    </a-popover>
水落(YangLei)'s avatar
水落(YangLei) committed
18 19 20 21 22 23
                    <a-button @click="reset">重置</a-button>
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
        </my-card>

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

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

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

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

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

81 82
const assign = Object.assign;

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

水落(YangLei)'s avatar
水落(YangLei) committed
88 89
const defaultTitle = '新增';

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

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

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

水落(YangLei)'s avatar
水落(YangLei) committed
153 154 155 156 157
    mounted() {
        this.getData();
    },

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

    methods: {
        async getData() {
            this.loading = true;
            try {
191
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
192 193 194 195 196 197 198
            } catch (error) {
                // todo
            }
            this.loading = false;
        },

        async getDataWithPage() {
陈浩玮's avatar
tijiao  
陈浩玮 committed
199 200 201 202 203
            const res = await request(this.url, METHOD.GET, {
                ...this.initQuery,
                ...this.queryForm,
                ...this.otherQuery,
            });
水落(YangLei)'s avatar
水落(YangLei) committed
204
            this.total = res.total;
陈浩玮's avatar
tijiao  
陈浩玮 committed
205
            if (this.formatData) this.data = this.formatData({ ...res });
水落(YangLei)'s avatar
水落(YangLei) committed
206 207 208
            else this.data = res.records;
        },

209
        async getDataNoPage() {
陈浩玮's avatar
tijiao  
陈浩玮 committed
210
            const res = await request(this.url, METHOD.GET, { ...this.queryForm, ...this.otherQuery });
211 212 213 214
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

水落(YangLei)'s avatar
水落(YangLei) committed
215 216 217 218
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
219 220 221

        hidden() {
            this.visible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
222 223 224 225 226 227
        },
        afterVisibleChange(visible) {
            if (!visible) {
                this.title = this.addBtn?.title ?? defaultTitle;
                this.type = null;
                this.row = null;
陈浩玮's avatar
tijiao  
陈浩玮 committed
228
                this.dWidth = this.drawerWidth;
水落(YangLei)'s avatar
水落(YangLei) committed
229
            }
230
        },
水落(YangLei)'s avatar
水落(YangLei) committed
231

232
        show(params) {
233
            this.visible = true;
234
            assign(this, params);
235
        },
水落(YangLei)'s avatar
水落(YangLei) committed
236

237
        reset() {
水落(YangLei)'s avatar
水落(YangLei) committed
238
            this.queryForm = { ...this.defaultQuery };
239 240 241
            this.initQuery = { ...initQuery };
            this.getData();
        },
水落(YangLei)'s avatar
水落(YangLei) committed
242

陈浩玮's avatar
tijiao  
陈浩玮 committed
243 244 245 246
        refreshView() {
            this.data = [...this.data];
        },

247 248 249 250 251
        addBtnClick() {
            const { click } = typeof this.addBtn === 'object' ? this.addBtn : {};
            click && click();
            this.visible = true;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
252 253 254
    },
};
</script>
水落(YangLei)'s avatar
水落(YangLei) committed
255 256 257 258 259 260 261 262 263 264 265 266 267

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

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