lockerTree.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div class="workstation-tree">
  3. <div class="head-container">
  4. <el-input
  5. v-model="searchValue"
  6. placeholder="请输入区域名称"
  7. clearable
  8. style="margin-bottom: 20px"
  9. @clear="handleClear"
  10. @input="handleSearch"
  11. >
  12. <template #prefix>
  13. <Icon icon="ep:search" />
  14. </template>
  15. </el-input>
  16. </div>
  17. <div class="head-container">
  18. <el-tree
  19. :data="workstationOptions"
  20. :props="{ label: 'workstationName', value: 'id', children: 'children' }"
  21. :expand-on-click-node="false"
  22. :filter-node-method="filterNode"
  23. ref="treeData"
  24. node-key="id"
  25. default-expand-all
  26. @node-click="handleNodeClick"
  27. highlight-current
  28. />
  29. </div>
  30. </div>
  31. </template>
  32. <script lang="ts" setup>
  33. import { handleTree } from '@/utils/tree'
  34. import * as MarsDeptApi from '@/api/system/marsdept'
  35. // 定义接口类型
  36. interface TreeNode {
  37. id: string
  38. workstationName: string
  39. children?: TreeNode[]
  40. [key: string]: any
  41. }
  42. // 定义事件
  43. const emit = defineEmits<{
  44. nodeClick: [data: TreeNode]
  45. }>()
  46. // 响应式数据
  47. const searchValue = ref<string>('')
  48. const workstationOptions = ref<TreeNode[]>([])
  49. const treeData = ref()
  50. // 获取岗位树数据
  51. const getWorkstationTree = async (): Promise<void> => {
  52. try {
  53. const data = {
  54. pageNo: 1,
  55. pageSize: -1
  56. }
  57. const response = await MarsDeptApi.listMarsDept(data)
  58. console.log('原始数据:', response.list)
  59. // 找到所有根节点的数据(parentId 为 0 的节点)
  60. const rootData = response.list.filter((item: any) => {
  61. return item.parentId === 0 || item.parentId === '0'
  62. })
  63. console.log('根节点数据:', rootData)
  64. // 构建树形结构
  65. workstationOptions.value = handleTree(rootData, 'id', 'parentId', 'children')
  66. } catch (error) {
  67. console.error('获取岗位树数据失败:', error)
  68. }
  69. }
  70. /** 基于名字过滤 */
  71. const filterNode = (value: string, data: TreeNode): boolean => {
  72. if (!value) return true
  73. return data.workstationName.includes(value)
  74. }
  75. // 岗位树点击事件
  76. const handleNodeClick = (data: TreeNode): void => {
  77. // searchValue.value = data.workstationName // 点击节点时赋值 暂不需要
  78. emit('nodeClick', data)
  79. }
  80. // 清除搜索
  81. const handleClear = (): void => {
  82. searchValue.value = ''
  83. emit('nodeClick', { id: '', workstationName: '' })
  84. }
  85. // 搜索处理
  86. const handleSearch = (value: string): void => {
  87. // 这里不需要额外处理,watch 会自动调用 filter
  88. }
  89. /** 监听searchValue */
  90. watch(searchValue, (val) => {
  91. if (treeData.value) {
  92. treeData.value.filter(val)
  93. }
  94. })
  95. // 初始化
  96. onMounted(async () => {
  97. await getWorkstationTree()
  98. })
  99. </script>
  100. <style lang="scss" scoped>
  101. .workstation-tree {
  102. height: 100%;
  103. .head-container {
  104. padding: 0 10px;
  105. }
  106. }
  107. </style>