jinlin
2024-07-18 cccb1f0822ca3883c13cac0338638c2a7bd7094a
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<template>
  <div class="product-tree-container" v-if="showFlag" :style="'height:'+fullHeight+'px;'">
    <el-input
      placeholder="输入名称进行过滤"
      style="width: 80%"
      v-model="filterText"
      size="small"
      clearable
    ></el-input>
    <el-divider></el-divider>
    <el-tree
      style="overflow: auto"
      v-if="productTree.length > 0"
      node-key="id"
      :props="defaultProps"
      :default-expanded-keys="expandedIds"
      :data="productTree"
      :load="loadNode"
      :render-content="renderContent"
      lazy
      :expand-on-click-node="false"
      :highlight-current="true"
      @node-click="handleNodeClick"
      :filter-node-method="filterNode"
      ref="tree"
    ></el-tree>
  </div>
</template>
<script>
  export default {
    name: 'ZtBoatTree',
    props: {
      expandLevel: {// 展示层级
        type: String,
        default: 'model'// 默认只展开一层
      }
    },
    data() {
      return {
        isHasData:false,
        fullHeight: '',
        menuId: this.$store.state.sidebarMenuActiveName,
        filterText: '',
        productTree: [],
        expandedIds: [],
        showLevel: '', // 需要展示的level
        busiLevels: [], // 关联业务的
        showFlag: true,
        defaultProps: {
          children: 'children',
          label: 'name',
          isLeaf: 'leaf'
        }
      }
    },
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val)
      }
    },
    mounted() {
      this.getInfo()
      this.fullHeight = (document.documentElement.clientHeight  - 180)
      window.onresize = () => {
        return (() => {
          this.fullHeight = (document.documentElement.clientHeight - 180)
        })()
      }
    },
    methods: {
      async getInfo() {
        // console.log(this.menuId, 'product-tree this.menuId')
        // console.log(this.$store.state.sidebarMenuActiveName, 'product-tree this.$store.state.sidebarMenuActiveName')
        // let arr = []
        // if (this.$store.state.product) {
        //   arr = this.$store.state.product.menuConfigs.filter(config => config.menuId === this.menuId)
        // } else {
        //   let res = await this.$http.get(`/sys/menuConfig/${this.menuId}`)
        //   if (res.success && res.data.hasSystemTree) {
        //     arr.push(res.data)
        //   }
        // }
        this.showLevel = 'equipment' //menuConfig.showLevel
        this.getProductList()
      },
      async getProductList() {
        let that = this
          {
          let res = await that.$http.get('/ztProduct/getTreeNodes')
          if (res.success) {
            console.log(res.data,'res.data')
            that.productTree = res.data
          }
        }
        console.log(that.productTree, 'that.productTre')
      },
      renderContent(h, { node, data, store }) {
        // console.log(data,'data wenTe')
        if (data.hasNotData == 1) {
          return <span style="color:#d0c8c4">{node.label}</span>;
        } else {
          return <span>{node.label}</span>;
        }
      },
     async loadNode(node, resolve) {
        console.log(node.data,'node.data')
        let that = this
        if (node.level === 0) {
          return resolve(that.productTree)
        } else if (node.data) {
          if(node.data.leaf === false){
            let res = await that.$http.get(`/ztProduct/getTreeNodes?pid=${node.data.id}`)
            if(res.success){
              if(res.data.length>=0){
                return resolve((res.data || []))
              }
            }
          }
        } else {
          return resolve([])
        }
      },
      filterNode(value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      handleNodeClick(data) {
        // if (this.busiLevels.includes(data.level))
        {
          this.$emit('on-selected', data)
        }
      }
    }
  }
</script>
<style>
  /*.product-tree-container {*/
    /*max-height: 650px;*/
  /*}*/
  .hasData .el-tree-node__label{
    color:red;
  }
</style>