he wei
2025-06-03 a10f3b82e33756ed0cd62a0cbe83bab8674df16f
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
<script setup>
import { ref, reactive, watch, onMounted, nextTick } from "vue";
const props = defineProps({
    data: {
        type: Array,
        default() {
            return [];
        }
    },
    defaultExpandKeys: {
        type: Array,
        default() {
            return []
        }
    },
    currentNodeKey: {
        type: [String, Number],
        default: ""
    },
});
 
const tree = ref(null);
const current = ref('');
const defaultProps = reactive({
  children: 'children',
  label: 'label',
  isLeaf: 'isLeaf',
});
 
watch(
  () => props.currentNodeKey,
  (val) => {
    nextTick(() => {
      current.value = val;
      tree.value.setCurrentKey(val);
    });
  }
);
 
const emit = defineEmits(['node-click', 'leaf-click']);
 
function nodeClick(data, node) {
  node.isCurrent = node.isLeaf;
  if (node.isLeaf && current.value != node.key) {
    current.value = node.key;
    emit('leaf-click', data, node);
  } else {
    emit('node-click', data, node);
  }
  tree.value.setCurrentKey(current.value);
}
 
function nodeExpand(data, node) {
  node.isCurrent = node.isLeaf;
  if (!node.isLeaf && current.value != node.key) {
    emit('node-click', data, node);
  }
}
 
function nodeCollapse(data, node) {
  loopClose(node.childrenNodes);
}
 
function loopClose(childNodes) {
  if(!childNodes || childNodes.length === 0) {
    return;
  }
  childNodes.map(item=>{
    item.expanded = false;
    loopClose(item.childNodes);
  });
}
 
 
onMounted(() => {
  setTimeout(() => {
    if(tree.value) {
      tree.value.setCurrentKey(props.currentNodeKey);
      current.value = props.currentNodeKey;
    }
  }, 500);
});
 
 
</script>
 
<template>
  <el-tree
    class="filter-tree"
    :props="defaultProps"
    :auto-expand-parent="true"
    node-key="key"
    ref="tree"
    :data="data"
    :default-expanded-keys="defaultExpandKeys"
    :current-node-key="currentNodeKey"
    @node-collapse="nodeCollapse"
    @node-click="nodeClick"
    @node-expand="nodeExpand"
  >
    <template #default="{ node }">
      <span class="span-ellipsis">
        <span :title="node.label">{{ node.label }}</span>
      </span>
    </template>
  </el-tree>
</template>
 
<style scoped lang="less">
.filter-tree {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
</style>