index.vue 4.44 KB
Newer Older
1 2 3 4 5 6 7
<template>
    <div>
        <div :class="$style.card" v-if="$scopedSlots.search">
            <a-form-model layout="inline" :model="queryForm">
                <slot name="search" :query="queryForm" />
            </a-form-model>

水落(YangLei)'s avatar
水落(YangLei) committed
8
            <div class="tw-text-right tw-mt-2">
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
                <a-space>
                    <a-button @click="queryForm = {}">重置</a-button>
                    <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>

            <a-table :data-source="data" :loading="loading" v-bind="$attrs">
                <slot />
            </a-table>
        </div>

        <!-- 新增 -->
        <a-drawer
            :title="addBtn.title || '新建'"
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
            v-if="addBtn"
            :maskClosable="addBtn.maskClosable"
        >
            <slot name="add" />
        </a-drawer>
    </div>
</template>

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

const data = [
    {
水落(YangLei)'s avatar
水落(YangLei) committed
50 51 52
        key: 1,
        name: 'John Brown sr.',
        age: 60,
53
        address: 'New York No. 1 Lake Park',
水落(YangLei)'s avatar
水落(YangLei) committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        children: [
            {
                key: 11,
                name: 'John Brown',
                age: 42,
                address: 'New York No. 2 Lake Park',
            },
            {
                key: 12,
                name: 'John Brown jr.',
                age: 30,
                address: 'New York No. 3 Lake Park',
                children: [
                    {
                        key: 121,
                        name: 'Jimmy Brown',
                        age: 16,
                        address: 'New York No. 3 Lake Park',
                    },
                ],
            },
            {
                key: 13,
                name: 'Jim Green sr.',
                age: 72,
                address: 'London No. 1 Lake Park',
                children: [
                    {
                        key: 131,
                        name: 'Jim Green',
                        age: 42,
                        address: 'London No. 2 Lake Park',
                        children: [
                            {
                                key: 1311,
                                name: 'Jim Green jr.',
                                age: 25,
                                address: 'London No. 3 Lake Park',
                            },
                            {
                                key: 1312,
                                name: 'Jimmy Green sr.',
                                age: 18,
                                address: 'London No. 4 Lake Park',
                            },
                        ],
                    },
                ],
            },
        ],
104 105
    },
    {
水落(YangLei)'s avatar
水落(YangLei) committed
106
        key: 2,
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        name: 'Joe Black',
        age: 32,
        address: 'Sidney No. 1 Lake Park',
    },
];

export default {
    props: {
        url: String,
        buttons: {
            type: Array,
            default: () => [],
        },
        addBtn: {
            type: Object,
        },
水落(YangLei)'s avatar
水落(YangLei) committed
123
        formatData: Function,
124 125 126 127 128 129 130 131 132 133
    },
    data() {
        return {
            data: [],
            queryForm: {},
            loading: false,
            addVisible: false,
        };
    },
    mounted() {
水落(YangLei)'s avatar
水落(YangLei) committed
134
        console.log(this.addBtn);
135 136 137 138 139 140
        this.getData();
    },

    methods: {
        async getData() {
            this.loading = true;
水落(YangLei)'s avatar
水落(YangLei) committed
141 142 143 144 145 146 147 148
            try {
                const res = await request(this.url, METHOD.GET);
                if (this.formatData) this.data = this.formatData(res);
                else this.data = res;
            } catch (error) {
                // todo
            }
            this.loading = false;
149 150 151 152 153 154 155 156 157 158 159 160 161
        },
        add() {
            this.addVisible = true;
        },
        addDrawerClose() {
            this.addVisible = false;
        },
    },
};
</script>

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