table.vue 6.7 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"
水落(YangLei)'s avatar
水落(YangLei) committed
40
                :row-selection="selected ? rowSelection : undefined"
水落(YangLei)'s avatar
水落(YangLei) committed
41 42
            >
                <slot />
水落(YangLei)'s avatar
水落(YangLei) committed
43 44 45 46 47
                <a-table-column
                    :title="newButtons.title"
                    v-if="this.newButtons"
                    v-bind="this.newButtons.options"
                >
48
                    <template #default="row">
水落(YangLei)'s avatar
水落(YangLei) committed
49
                        <my-ac-btn :row="row" :buttons="newButtons.data" />
50 51
                    </template>
                </a-table-column>
水落(YangLei)'s avatar
水落(YangLei) committed
52 53
            </a-table>
        </my-card>
54 55

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

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

76 77
const assign = Object.assign;

水落(YangLei)'s avatar
水落(YangLei) committed
78 79 80 81 82
const initQuery = {
    pageSize: 10,
    pageNum: 1,
};

水落(YangLei)'s avatar
水落(YangLei) committed
83 84
const defaultTitle = '新增';

水落(YangLei)'s avatar
水落(YangLei) committed
85 86 87
export default {
    props: {
        url: String,
88
        addBtn: [Object, Boolean],
水落(YangLei)'s avatar
水落(YangLei) committed
89
        buttons: [Array, Object],
90 91
        noPage: Boolean,
        formatData: Function,
水落(YangLei)'s avatar
水落(YangLei) committed
92
        rowKey: [String, Function],
水落(YangLei)'s avatar
水落(YangLei) committed
93
        selected: Array,
94 95 96 97 98
        drawerWidth: {
            type: Number,
            default: 600,
        },
        noPadding: Boolean,
水落(YangLei)'s avatar
水落(YangLei) committed
99
        scroll: Object,
水落(YangLei)'s avatar
水落(YangLei) committed
100
        width: { type: Number, default: 900 },
水落(YangLei)'s avatar
水落(YangLei) committed
101 102 103
    },

    data() {
水落(YangLei)'s avatar
水落(YangLei) committed
104
        const newBtn = this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
水落(YangLei)'s avatar
水落(YangLei) committed
105 106 107 108 109 110 111 112
        return {
            initQuery: {
                ...initQuery,
            },
            data: [],
            queryForm: {},
            loading: false,
            total: 0,
113
            visible: false,
水落(YangLei)'s avatar
水落(YangLei) committed
114
            title: newBtn.title ?? defaultTitle,
115 116 117 118 119 120 121 122 123
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
            },
水落(YangLei)'s avatar
水落(YangLei) committed
124
            type: null,
125
            row: null,
水落(YangLei)'s avatar
水落(YangLei) committed
126 127 128 129 130 131 132 133
        };
    },

    mounted() {
        this.getData();
    },

    computed: {
水落(YangLei)'s avatar
水落(YangLei) committed
134 135 136
        newBtn() {
            return this.addBtn ? (typeof this.addBtn === 'object' ? this.addBtn : {}) : this.addBtn;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
137 138 139 140 141 142 143
        newButtons() {
            return this.buttons
                ? Array.isArray(this.buttons)
                    ? { data: this.buttons, title: '操作' }
                    : this.buttons
                : null;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
144
        pagination() {
145 146 147 148 149 150 151 152
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                      showQuickJumper: true,
                  };
水落(YangLei)'s avatar
水落(YangLei) committed
153
        },
水落(YangLei)'s avatar
水落(YangLei) committed
154 155 156 157 158 159 160
        rowSelection() {
            return {
                onChange: (selectedRowKeys, selectedRows) => {
                    this.$emit('update:selected', [selectedRowKeys, selectedRows]);
                },
            };
        },
水落(YangLei)'s avatar
水落(YangLei) committed
161 162 163 164 165
    },

    methods: {
        async getData() {
            this.loading = true;
166
            console.log(this.queryForm);
水落(YangLei)'s avatar
水落(YangLei) committed
167
            try {
168
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
169 170 171 172 173 174 175 176 177 178 179 180 181
            } catch (error) {
                // todo
            }
            this.loading = false;
        },

        async getDataWithPage() {
            const res = await request(this.url, METHOD.GET, { ...this.initQuery, ...this.queryForm });
            this.total = res.total;
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res.records;
        },

182 183 184 185 186 187
        async getDataNoPage() {
            const res = await request(this.url, METHOD.GET, this.queryForm);
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

水落(YangLei)'s avatar
水落(YangLei) committed
188 189 190 191
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
192 193 194

        hidden() {
            this.visible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
195 196 197 198 199 200 201
        },
        afterVisibleChange(visible) {
            if (!visible) {
                this.title = this.addBtn?.title ?? defaultTitle;
                this.type = null;
                this.row = null;
            }
202
        },
水落(YangLei)'s avatar
水落(YangLei) committed
203

204
        show(params) {
205
            this.visible = true;
206
            assign(this, params);
207
        },
水落(YangLei)'s avatar
水落(YangLei) committed
208

209 210 211 212 213
        reset() {
            this.queryForm = {};
            this.initQuery = { ...initQuery };
            this.getData();
        },
水落(YangLei)'s avatar
水落(YangLei) committed
214

215 216 217
        addBtnClick() {
            const { click } = typeof this.addBtn === 'object' ? this.addBtn : {};
            click && click();
水落(YangLei)'s avatar
水落(YangLei) committed
218
            this.type = 'add';
219 220
            this.visible = true;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
221 222 223
    },
};
</script>
水落(YangLei)'s avatar
水落(YangLei) committed
224 225 226 227 228 229 230 231 232 233 234 235 236

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

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