Blame view

src/components/machine/machineTypeTree.vue 2.09 KB
21e37d17   wuxw   开发设备类型功能
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
  <template>
    <el-card class="tree-container">
      <el-tree ref="machineTypeTree" :data="treeData" node-key="id" :props="defaultProps" :highlight-current="true"
        :expand-on-click-node="false" @node-click="handleNodeClick"></el-tree>
    </el-card>
  </template>
  
  <script>
  import { listMachineType } from '@/api/machine/machineTypeTreeManageApi'
  import { getCommunityId } from '@/api/community/communityApi'
  
  export default {
    name: 'MachineTypeTree',
    props: {
      state: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'text'
        },
        communityId: ''
      }
    },
    created() {
      this.communityId = getCommunityId()
      this.loadMachineTypesTree()
    },
    methods: {
      async loadMachineTypesTree() {
        try {
          const params = {
            page: 1,
            row: 100,
            communityId: this.communityId,
            state: this.state
          }
          const { data } = await listMachineType(params)
          this.treeData = this.buildTree(data)
        } catch (error) {
          console.error('加载设备类型树失败:', error)
        }
      },
      buildTree(data) {
        const result = []
        const map = {}
  
        if (!Array.isArray(data)) {
          return result
        }
  
        data.forEach(item => {
          map[item.typeId] = {
            id: item.typeId,
            typeId: item.typeId,
            parentTypeId: item.parentTypeId,
            text: item.machineTypeName,
            children: []
          }
        })
  
        data.forEach(item => {
          const parent = map[item.parentTypeId]
          if (parent) {
            parent.children.push(map[item.typeId])
          } else {
            result.push(map[item.typeId])
          }
        })
  
        return result
      },
      handleNodeClick(data) {
        this.$emit('switchType', {
          typeId: data.typeId,
          typeName: data.text
        })
      },
      refreshTree() {
        this.loadMachineTypesTree()
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .tree-container {
    height: 100%;
  
    .el-tree {
      height: 100%;
      overflow: auto;
    }
  }
  </style>