index.vue 2.61 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
1
<template>
2 3 4 5
    <div>
        <span v-for="(btn, index) of basicBtns" :key="btn.label">
            <PopconfirmDelete
                :url="getDelUrl(btn.url, row)"
6
                v-if="btn.type === 'confirm'"
7 8
                :cb="btn.after"
                :title="btn.title"
陈浩玮's avatar
陈浩玮 committed
9
                :onOk="() => btn.onOk && btn.onOk(row)"
10
                :label="btn.label"
11
            />
12
            <a v-else @click="() => btn.click(row)" v-bind="btn.option">{{ btn.label }}</a>
13 14 15 16 17 18 19
            <a-divider type="vertical" v-if="moreBtns.length || index !== basicBtns.length - 1" />
        </span>

        <template v-if="moreBtns.length">
            <a-popover>
                <template slot="content">
                    <a-space direction="vertical">
20 21 22 23 24 25
                        <span v-for="btn of moreBtns" :key="btn.label">
                            <PopconfirmDelete
                                :url="getDelUrl(btn.url, row)"
                                v-if="btn.type === 'confirm'"
                                :cb="btn.after"
                                :title="btn.title"
陈浩玮's avatar
陈浩玮 committed
26
                                :onOk="() => btn.onOk && btn.onOk(row)"
27 28
                                :label="btn.label"
                            />
29
                            <a v-else @click="() => btn.click(row)" v-bind="btn.option">{{ btn.label }}</a>
30
                        </span>
31 32 33 34
                    </a-space>
                </template>
                <a>...</a>
            </a-popover>
水落(YangLei)'s avatar
水落(YangLei) committed
35
        </template>
36
    </div>
水落(YangLei)'s avatar
水落(YangLei) committed
37 38 39
</template>

<script>
40 41 42
import { isFunction } from '@/utils';
import PopconfirmDelete from '@/components/popconfirm_delete/index.vue';

水落(YangLei)'s avatar
水落(YangLei) committed
43
export default {
44 45
    components: { PopconfirmDelete },

水落(YangLei)'s avatar
水落(YangLei) committed
46 47 48
    props: {
        buttons: {
            type: Array,
49
            default: () => [],
水落(YangLei)'s avatar
水落(YangLei) committed
50
        },
51
        row: Object,
水落(YangLei)'s avatar
水落(YangLei) committed
52
    },
陈浩玮's avatar
陈浩玮 committed
53 54 55 56 57 58 59 60 61 62 63 64 65
    watch: {
        row() {
            this.init();
        },
    },
    data() {
        return {
            buttonsArr: [],
        };
    },
    created() {
        this.init();
    },
水落(YangLei)'s avatar
水落(YangLei) committed
66 67
    computed: {
        basicBtns() {
陈浩玮's avatar
陈浩玮 committed
68
            return this.buttonsArr.length > 3 ? this.buttonsArr.slice(0, 3) : this.buttonsArr;
水落(YangLei)'s avatar
水落(YangLei) committed
69 70
        },
        moreBtns() {
陈浩玮's avatar
陈浩玮 committed
71
            return this.buttonsArr.length > 3 ? this.buttonsArr.slice(3) : [];
水落(YangLei)'s avatar
水落(YangLei) committed
72 73
        },
    },
74 75 76 77 78
    methods: {
        getDelUrl(url, row) {
            if (isFunction(url)) return url(row);
            return url;
        },
陈浩玮's avatar
陈浩玮 committed
79
        init() {
陈浩玮's avatar
陈浩玮 committed
80
            this.buttons.map((i) => {
陈浩玮's avatar
陈浩玮 committed
81 82
                i.__hidden__ = i.isHidden && i.isHidden(this.row);
            });
陈浩玮's avatar
陈浩玮 committed
83
            this.buttonsArr = this.buttons.filter((i) => !i.__hidden__);
陈浩玮's avatar
陈浩玮 committed
84
        },
85
    },
水落(YangLei)'s avatar
水落(YangLei) committed
86 87
};
</script>