wujingjing
2024-12-23 6d82ad4fb10f7015059b1b2cbcf72e8a949e83fa
1
2
3
4
5
6
7
8
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
50
51
52
53
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<template>
    <el-card class="box-card" shadow="hover" style="height: 100%" body-style="height:100%; overflow:auto">
        <template #header>
            <div class="card-header">
                <div class="tree-h-flex">
                    <div class="tree-h-left">
                        <el-input :prefix-icon="Search" v-model="filterText" placeholder="机构名称" />
                    </div>
                    <div class="tree-h-right">
                        <el-dropdown @command="handleCommand">
                            <el-button style="margin-left: 8px; width: 34px">
                                <el-icon class="el-icon--center">
                                    <more-filled />
                                </el-icon>
                            </el-button>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item command="expandAll">全部展开</el-dropdown-item>
                                    <el-dropdown-item command="collapseAll">全部折叠</el-dropdown-item>
                                    <el-dropdown-item command="rootNode">根节点</el-dropdown-item>
                                    <el-dropdown-item command="refresh">刷新</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </div>
                </div>
            </div>
        </template>
        <div style="margin-bottom: 45px" v-loading="state.loading">
            <el-tree
                ref="treeRef"
                class="filter-tree"
                :data="state.orgData"
                node-key="id"
                :props="{ children: 'children', label: 'name' }"
                :filter-node-method="filterNode"
                @node-click="nodeClick"
                :show-checkbox="state.isShowCheckbox"
                :default-checked-keys="state.ownOrgData"
                highlight-current
                check-strictly
            >
                <template #default="{ node }">
                    <el-icon v-if="node.level == 1" size="16" style="margin-right: 3px; display: inline; vertical-align: middle"
                        ><ele-School
                    /></el-icon>
                    <el-icon v-else-if="node.level == 2" size="16" style="margin-right: 3px; display: inline; vertical-align: middle"
                        ><ele-PriceTag
                    /></el-icon>
                    <el-icon v-else size="16" style="margin-right: 3px; display: inline; vertical-align: middle"><ele-CollectionTag /></el-icon>
                    {{ node.label }}
                </template>
            </el-tree>
        </div>
    </el-card>
</template>
 
<script lang="ts" setup>
import { MoreFilled, Search } from '@element-plus/icons-vue';
import { ElTree } from 'element-plus';
import { onMounted, reactive, ref, watch } from 'vue';
import { treeData } from './testData';
 
// import { getAPI } from '/@/utils/axios-utils';
// import { SysOrgApi } from '/@/api-services/api';
// import { SysOrg } from '/@/api-services/models';
 
const filterText = ref('');
const treeRef = ref<InstanceType<typeof ElTree>>();
const state = reactive({
    loading: false,
    orgData: [] as Array<any>,
    isShowCheckbox: false,
    ownOrgData: [],
});
 
onMounted(() => {
    initTreeData();
});
 
watch(filterText, (val) => {
    treeRef.value!.filter(val);
});
 
const initTreeData = async () => {
    state.loading = true;
    // var res = await getAPI(SysOrgApi).apiSysOrgListGet(0);
    state.orgData = treeData;
    state.loading = false;
};
 
// 设置默认选择
const setCheckedKeys = (orgData: any) => {
    state.isShowCheckbox = true;
    treeRef.value!.setCheckedKeys([]);
    state.ownOrgData = orgData;
};
 
// 获取已经选择
const getCheckedKeys = () => {
    return treeRef.value!.getCheckedKeys();
};
 
const filterNode = (value: string, data: any) => {
    if (!value) return true;
    return data.name.includes(value);
};
 
const handleCommand = async (command: string | number | object) => {
    if ('expandAll' == command) {
        for (let i = 0; i < treeRef.value!.store._getAllNodes().length; i++) {
            treeRef.value!.store._getAllNodes()[i].expanded = true;
        }
    } else if ('collapseAll' == command) {
        for (let i = 0; i < treeRef.value!.store._getAllNodes().length; i++) {
            treeRef.value!.store._getAllNodes()[i].expanded = false;
        }
    } else if ('refresh' == command) {
        initTreeData();
    } else if ('rootNode' == command) {
        emits('node-click', { id: 0, name: '' });
    }
};
 
// 与父组件的交互逻辑
const emits = defineEmits(['node-click']);
const nodeClick = (node: any) => {
    emits('node-click', { id: node.id, name: node.name });
};
 
// 导出对象
defineExpose({ initTreeData, setCheckedKeys, getCheckedKeys });
</script>
 
<style lang="scss" scoped>
.tree-h-flex {
    display: flex;
}
 
.tree-h-left {
    flex: 1;
    width: 100%;
}
 
.tree-h-right {
    width: 42px;
    min-width: 42px;
}
</style>