UserDeptTree.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <template>
  2. <div class="head-container">
  3. <el-input v-model="deptName" placeholder="请输入部门名称" clearable style="margin-bottom: 20px">
  4. <template #prefix>
  5. <Icon icon="ep:search" />
  6. </template>
  7. </el-input>
  8. </div>
  9. <div class="head-container">
  10. <el-tree
  11. :data="deptOptions"
  12. :props="defaultProps"
  13. :expand-on-click-node="false"
  14. :filter-node-method="filterNode"
  15. ref="treeRef"
  16. node-key="id"
  17. default-expand-all
  18. highlight-current
  19. @node-click="handleDeptNodeClick"
  20. />
  21. </div>
  22. </template>
  23. <script setup lang="ts" name="UserDeptTree">
  24. import { ElTree } from 'element-plus'
  25. import * as DeptApi from '@/api/system/dept'
  26. import { defaultProps, handleTree } from '@/utils/tree'
  27. const emits = defineEmits(['node-click'])
  28. const deptName = ref('')
  29. const deptOptions = ref<Tree[]>([]) // 树形结构
  30. const treeRef = ref<InstanceType<typeof ElTree>>()
  31. const getTree = async () => {
  32. const res = await DeptApi.getSimpleDeptList()
  33. deptOptions.value = []
  34. deptOptions.value.push(...handleTree(res))
  35. }
  36. const filterNode = (value: string, data: Tree) => {
  37. if (!value) return true
  38. return data.name.includes(value)
  39. }
  40. const handleDeptNodeClick = async (row: { [key: string]: any }) => {
  41. emits('node-click', row)
  42. }
  43. onMounted(async () => {
  44. await getTree()
  45. })
  46. </script>