index.vue 1.41 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) committed
1 2 3 4 5 6 7 8
<template>
    <a-tree
        :checkedKeys="value"
        :selectable="false"
        checkable
        :replaceFields="replaceFields"
        :tree-data="treeData"
        class="tw-overflow-y-auto"
9
        :expandedKeys.sync="expandedKeys"
水落(YangLei)'s avatar
水落(YangLei) committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
        v-bind="$attrs"
        v-on="$listeners"
    />
</template>

<script>
import { getMenuDataApi } from '@/api';
import { convertListToTree } from '@/utils';

export default {
    model: {
        prop: 'value',
        event: 'check',
    },
    props: {
        value: [Object, Array],
        showMenu: Boolean,
27
        defaultCheckedKeys: Array,
水落(YangLei)'s avatar
水落(YangLei) committed
28 29 30 31 32 33 34 35
    },
    data() {
        return {
            treeData: [],
            replaceFields: {
                title: 'menuName',
                key: 'menuId',
            },
36
            expandedKeys: [],
水落(YangLei)'s avatar
水落(YangLei) committed
37 38 39
        };
    },
    async mounted() {
水落(YangLei)'s avatar
水落(YangLei) committed
40 41
        // 使用 缓存
        this.rawData = await getMenuDataApi(true);
水落(YangLei)'s avatar
水落(YangLei) committed
42
        this.treeData = convertListToTree(this.rawData, !this.showMenu);
43 44 45 46 47 48 49 50 51
        this.$emit('check', this.defaultCheckedKeys);
        this.expandedKeys = this.defaultCheckedKeys;
    },

    watch: {
        defaultCheckedKeys(val) {
            this.$emit('check', val);
            this.expandedKeys = val;
        },
水落(YangLei)'s avatar
水落(YangLei) committed
52 53 54 55 56 57 58 59 60 61
    },

    methods: {
        get() {
            if (Array.isArray(this.value)) return this.rawData.filter(m => this.value.includes(m.menuId));
            return [];
        },
    },
};
</script>