WXL
5 天以前 871522ed7e06fd9c62a87c178d7f5c88d7853a20
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
<template>
  <view class="u-tree">
    <tree-node
      v-for="node in treeData"
      :key="node[props.nodeKey]"
      :node="node"
      :props="props"
      :show-checkbox="showCheckbox"
      :check-strictly="checkStrictly"
      :expand-on-click-node="expandOnClickNode"
      @node-click="handleNodeClick"
      @check-change="$emit('check-change', $event)">
      <template #default="{ nodeData, level }">
        <slot :node="nodeData" :level="level"></slot>
      </template>
    </tree-node>
  </view>
</template>
 
<script>
import TreeNode from './tree-node.vue';
 
export default {
  name: 'u-tree',
  components: { TreeNode },
  props: {
    data: {
      type: Array,
      required: true
    },
    props: {
      type: Object,
      default: () => ({
        label: 'label',
        children: 'children',
        nodeKey: 'id'
      })
    },
    showCheckbox: {
      type: Boolean,
      default: false
    },
    defaultExpandAll: {
      type: Boolean,
      default: false
    },
    expandOnClickNode: {
      type: Boolean,
      default: true
    },
    checkStrictly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: []
    };
  },
  created() {
    this.initTree();
  },
  watch: {
    data: {
      handler(newVal) {
        this.treeData = JSON.parse(JSON.stringify(newVal));
        this.initExpandedState(this.treeData, this.defaultExpandAll);
      },
      deep: true,
      immediate: true
    }
  },
  emits: ['node-click', 'check-change'],
  methods: {
    initTree() {
      this.treeData = JSON.parse(JSON.stringify(this.data));
      this.initExpandedState(this.treeData, this.defaultExpandAll);
    },
    initExpandedState(nodes, expanded) {
      nodes.forEach(node => {
        node.expanded = expanded;
        if (node[this.props.children]) {
          this.initExpandedState(node[this.props.children], expanded);
        }
      });
    },
    handleNodeClick(node) {
      this.$emit('node-click', node);
    },
    /**
     * 直接递归 treeData 获取所有 checked 的节点
     */
    getCheckedNodes() {
        const traverse = (nodes) => {
            let result = [];
            nodes.forEach(node => {
            if (node.checked) {
                result.push(node);
            }
            if (node[this.props.children] && node[this.props.children].length > 0) {
                result = result.concat(traverse(node[this.props.children]));
            }
            });
            return result;
        };
        return traverse(this.treeData);
    }
  }
};
</script>
 
<style scoped>
.u-tree {
  font-size: 28rpx;
}
</style>