orgTree.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <el-card class="box-card" shadow="hover" style="height: 100%" body-style="height:100%; overflow:auto">
  3. <template #header>
  4. <div class="card-header">
  5. <div class="tree-h-flex">
  6. <div class="tree-h-left">
  7. <el-input :prefix-icon="Search" v-model="filterText" placeholder="机构名称" />
  8. </div>
  9. <div class="tree-h-right">
  10. <el-dropdown @command="handleCommand">
  11. <el-button style="margin-left: 8px; width: 34px">
  12. <el-icon class="el-icon--center">
  13. <more-filled />
  14. </el-icon>
  15. </el-button>
  16. <template #dropdown>
  17. <el-dropdown-menu>
  18. <el-dropdown-item command="expandAll">全部展开</el-dropdown-item>
  19. <el-dropdown-item command="collapseAll">全部折叠</el-dropdown-item>
  20. <el-dropdown-item command="rootNode">根节点</el-dropdown-item>
  21. <el-dropdown-item command="refresh">刷新</el-dropdown-item>
  22. </el-dropdown-menu>
  23. </template>
  24. </el-dropdown>
  25. </div>
  26. </div>
  27. </div>
  28. </template>
  29. <div style="margin-bottom: 45px" v-loading="state.loading">
  30. <el-tree
  31. ref="treeRef"
  32. class="filter-tree"
  33. :data="state.orgData"
  34. node-key="id"
  35. :props="{ children: 'children', label: 'name' }"
  36. :filter-node-method="filterNode"
  37. @node-click="nodeClick"
  38. :show-checkbox="state.isShowCheckbox"
  39. :default-checked-keys="state.ownOrgData"
  40. highlight-current
  41. check-strictly
  42. >
  43. <template #default="{ node }">
  44. <el-icon><ele-School /></el-icon>{{ node.label }}
  45. </template>
  46. </el-tree>
  47. </div>
  48. </el-card>
  49. </template>
  50. <script lang="ts" setup>
  51. import { onMounted, reactive, ref, watch } from 'vue';
  52. import type { ElTree } from 'element-plus';
  53. import { Search, MoreFilled } from '@element-plus/icons-vue';
  54. import { getAPI } from '/@/utils/axios-utils';
  55. import { SysOrgApi } from '/@/api-services/api';
  56. import { SysOrg } from '/@/api-services/models';
  57. const filterText = ref('');
  58. const treeRef = ref<InstanceType<typeof ElTree>>();
  59. const state = reactive({
  60. loading: false,
  61. orgData: [] as Array<SysOrg>,
  62. isShowCheckbox: false,
  63. ownOrgData: [],
  64. });
  65. onMounted(() => {
  66. initTreeData();
  67. });
  68. watch(filterText, (val) => {
  69. treeRef.value!.filter(val);
  70. });
  71. const initTreeData = async () => {
  72. state.loading = true;
  73. var res = await getAPI(SysOrgApi).apiSysOrgListGet(0);
  74. state.orgData = res.data.result ?? [];
  75. state.loading = false;
  76. };
  77. // 设置默认选择
  78. const setCheckedKeys = (orgData: any) => {
  79. state.isShowCheckbox = true;
  80. treeRef.value!.setCheckedKeys([]);
  81. state.ownOrgData = orgData;
  82. };
  83. // 获取已经选择
  84. const getCheckedKeys = () => {
  85. return treeRef.value!.getCheckedKeys();
  86. };
  87. const filterNode = (value: string, data: any) => {
  88. if (!value) return true;
  89. return data.name.includes(value);
  90. };
  91. const handleCommand = async (command: string | number | object) => {
  92. if ('expandAll' == command) {
  93. for (let i = 0; i < treeRef.value!.store._getAllNodes().length; i++) {
  94. treeRef.value!.store._getAllNodes()[i].expanded = true;
  95. }
  96. } else if ('collapseAll' == command) {
  97. for (let i = 0; i < treeRef.value!.store._getAllNodes().length; i++) {
  98. treeRef.value!.store._getAllNodes()[i].expanded = false;
  99. }
  100. } else if ('refresh' == command) {
  101. initTreeData();
  102. } else if ('rootNode' == command) {
  103. emits('node-click', { id: 0, name: '' });
  104. }
  105. };
  106. // 与父组件的交互逻辑
  107. const emits = defineEmits(['node-click']);
  108. const nodeClick = (node: any) => {
  109. emits('node-click', { id: node.id, name: node.name });
  110. };
  111. // 导出对象
  112. defineExpose({ initTreeData, setCheckedKeys, getCheckedKeys });
  113. </script>
  114. <style lang="scss" scoped>
  115. .tree-h-flex {
  116. display: flex;
  117. }
  118. .tree-h-left {
  119. flex: 1;
  120. width: 100%;
  121. }
  122. .tree-h-right {
  123. width: 42px;
  124. min-width: 42px;
  125. }
  126. </style>