OrganizationIdTree.vue
3.58 KB
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
<template>
<div
ref="tree"
class="organization-tree flex relative items-center py-4"
:class="foldFlag ? '' : 'pl-4'"
>
<div
class="cursor-pointer flex py-4 fold-icon absolute rounded svg:fill-gray-400 hover:bg-gray-200"
:class="foldFlag ? '' : '-right-4'"
@click="handleFold"
>
<div>
<CaretRightOutlined
:class="[foldFlag ? '' : 'rotate-180']"
class="transform fill-gray-100"
/>
</div>
</div>
<div :style="{ width: foldFlag ? '0px' : '100%' }" class="bg-white mr-0 overflow-hidden h-full">
<BasicTree v-bind="getBindData" />
</div>
</div>
</template>
<script lang="ts" setup name="OrganizationIdTree">
import { computed, onMounted, ref, unref, useAttrs } from 'vue';
import { BasicTree, TreeItem } from '/@/components/Tree';
import { getOrganizationList } from '/@/api/system/system';
import { CaretRightOutlined } from '@ant-design/icons-vue';
import { getBoundingClientRect } from '/@/utils/domUtils';
import { BasicTreePropsType, OrganizationTreeActionType } from './types';
import { OrganizationTreePropsType } from './props';
const props = defineProps<OrganizationTreePropsType>();
const attrs = useAttrs();
const tree = ref<Nullable<HTMLDivElement>>();
const emit = defineEmits(['select', 'register']);
const treeData = ref<TreeItem[]>([]);
const selectedKeys = ref<string[]>();
const treeExpandData = ref<string[]>([]);
const innerProps = ref<OrganizationTreePropsType>({});
//获取所有父级id
function findForAllId(data: Recordable[] = [], arr: string[] = []) {
for (const item of data) {
arr.push(item.id);
}
return arr;
}
function handleSelect(keys: string[]) {
selectedKeys.value = keys;
emit('select', keys[0]);
}
function resetOrganization() {
selectedKeys.value = [];
}
const foldFlag = ref(true);
const handleFold = () => {
foldFlag.value = !unref(foldFlag);
};
const setTreeHeight = () => {
const rect = getBoundingClientRect(unref(tree)!);
if (rect) {
const { y } = rect as DOMRect;
const clientHight = document.documentElement.clientHeight;
const maxHeight = clientHight - y;
if (unref(tree)) unref(tree)!.style.height = `${maxHeight}px`;
}
};
onMounted(async () => {
treeData.value = (await getOrganizationList()) as unknown as TreeItem[];
const getAllIds = findForAllId(treeData.value as any, []);
//设置要展开的id
treeExpandData.value = getAllIds;
setTreeHeight();
});
defineExpose({
resetOrganization,
});
const getProps = computed<OrganizationTreePropsType>(() => ({ ...props, ...unref(innerProps) }));
const getBindData = computed(() => {
return {
title: '组织列表',
toolbar: true,
search: true,
clickRowToExpand: false,
treeData: unref(treeData),
expandedKeys: unref(treeExpandData),
replaceFields: { key: 'id', title: 'name' },
selectedKeys: unref(selectedKeys),
...attrs,
...unref(getProps),
onSelect: (keys: string[]) => {
handleSelect(keys);
},
} as BasicTreePropsType;
});
const setProps = (props: Partial<OrganizationTreePropsType>) => {
innerProps.value = { ...(props || {}) };
};
emit('register', {
clearSelected: resetOrganization,
getSelectKey: () => unref(selectedKeys)?.[0],
setProps,
} as OrganizationTreeActionType);
</script>
<style scoped lang="less">
.organization-tree {
max-height: 100vh;
.expand {
opacity: 0;
}
&:hover {
.expand {
opacity: 1;
}
}
}
</style>