Blame view

ant-design-vue-jeecg/src/components/jeecg/JTreeDict.vue 4.23 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<template>
  <a-tree-select
    allowClear
    labelInValue
    style="width: 100%"
    :disabled="disabled"
    :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
    :placeholder="placeholder"
    :loadData="asyncLoadTreeData"
    :value="treeValue"
    :treeData="treeData"
    @change="onChange"
    @search="onSearch">
  </a-tree-select>
</template>

<script>
肖超群 authored
18
import {getAction} from '@/api/manage'
19
import { translateResultMessage } from '@/api/api'
肖超群 authored
20
肖超群 authored
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export default {
  name: 'JTreeDict',
  data() {
    return {
      treeData: [],
      treeValue: null,
      url_root: "/sys/category/loadTreeRoot",
      url_children: "/sys/category/loadTreeChildren",
      url_view: '/sys/category/loadOne',
    }
  },
  props: {
    value: {
      type: String,
      required: false
肖超群 authored
36
    },
肖超群 authored
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    placeholder: {
      type: String,
      default: '请选择',
      required: false
    },
    parentCode: {
      type: String,
      default: '',
      required: false
    },
    field: {
      type: String,
      default: 'id',
      required: false
    },
    root: {
      type: Object,
      required: false,
      default: () => {
        return {
          pid: '0'
肖超群 authored
58
59
60
        }
      }
    },
肖超群 authored
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    async: {
      type: Boolean,
      default: false,
      required: false
    },
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    root: {
      handler(val) {
        console.log("root-change", val)
肖超群 authored
76
      },
肖超群 authored
77
78
79
80
81
      deep: true
    },
    parentCode: {
      handler() {
        this.loadRoot()
肖超群 authored
82
83
      }
    },
肖超群 authored
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
    value: {
      handler() {
        this.loadViewInfo()
      }
    }
  },
  created() {
    this.loadRoot()
    this.loadViewInfo()
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  methods: {
    loadViewInfo() {
      if (!this.value || this.value == "0") {
        this.treeValue = null
      } else {
        let param = {
          field: this.field,
          val: this.value
        }
        getAction(this.url_view, param).then(res => {
          if (res.success) {
            this.treeValue = {
              value: this.value,
              label: res.result.name
            }
          }
        })
      }
肖超群 authored
116
    },
肖超群 authored
117
118
119
120
121
122
123
124
125
126
    loadRoot() {
      let param = {
        async: this.async,
        pcode: this.parentCode
      }
      getAction(this.url_root, param).then(res => {
        if (res.success) {
          this.handleTreeNodeValue(res.result)
          this.treeData = [...res.result]
        } else {
127
          this.$message.error(translateResultMessage(res, res.message))
肖超群 authored
128
129
        }
      })
肖超群 authored
130
    },
肖超群 authored
131
132
133
134
135
    asyncLoadTreeData(treeNode) {
      return new Promise((resolve) => {
        if (!this.async) {
          resolve()
          return
肖超群 authored
136
        }
肖超群 authored
137
138
139
140
141
        if (treeNode.$vnode.children) {
          resolve()
          return
        }
        let pid = treeNode.$vnode.key
肖超群 authored
142
        let param = {
肖超群 authored
143
          pid: pid
肖超群 authored
144
        }
肖超群 authored
145
146
        getAction(this.url_children, param).then(res => {
          if (res.success) {
肖超群 authored
147
            this.handleTreeNodeValue(res.result)
肖超群 authored
148
149
            this.addChildren(pid, res.result, this.treeData)
            this.treeData = [...this.treeData]
肖超群 authored
150
          }
肖超群 authored
151
          resolve()
肖超群 authored
152
        })
肖超群 authored
153
154
155
156
157
158
159
160
161
162
      })
    },
    addChildren(pid, children, treeArray) {
      if (treeArray && treeArray.length > 0) {
        for (let item of treeArray) {
          if (item.key == pid) {
            if (!children || children.length == 0) {
              item.leaf = true
            } else {
              item.children = children
肖超群 authored
163
            }
肖超群 authored
164
165
166
            break
          } else {
            this.addChildren(pid, children, item.children)
肖超群 authored
167
168
          }
        }
肖超群 authored
169
170
171
172
173
174
175
176
177
      }
    },
    handleTreeNodeValue(result) {
      let storeField = this.field == 'code' ? 'code' : 'key'
      for (let i of result) {
        i.value = i[storeField]
        i.isLeaf = (!i.leaf) ? false : true
        if (i.children && i.children.length > 0) {
          this.handleTreeNodeValue(i.children)
肖超群 authored
178
179
        }
      }
肖超群 authored
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    },
    onChange(value) {
      console.log(value)
      if (!value) {
        this.$emit('change', '');
      } else {
        this.$emit('change', value.value);
      }
      this.treeValue = value
    },
    onSearch(value) {
      console.log(value)
    },
    getCurrTreeData() {
      return this.treeData
肖超群 authored
195
196
    }
  }
肖超群 authored
197
198

}
肖超群 authored
199
</script>