dangsn
2024-06-06 8c8c59c1f2005fa3ca89ac2117ca1a3c0c618913
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<template>
  <avue-input-tree ref="referTree" class="w100" :clearable="true"
                   v-model="valueD"
                   :checked="checked"
                   :dic="treeData"
                   :disabled="disabled"
                   :lazy="lazy"
                   :leaf-only="referConfig.onlyLeaf"
                   :multiple="isMuti"
                   :node-click="nodeClick"
                   :placeholder="placeholder"
                   :props="props"
                   :default-expand-all="true"
                   :tree-load="treeLoad"></avue-input-tree>
</template>
 
<script>
import {getTree, getLazyTree} from "@/api/refer/tree";
import {verifyNull} from "@/util/validate";
 
export default {
  name: "vciWebReferTree",
  props: {
    referConfig: {
      type: Object,
    },
    value: {
      type: String,
      default: ''
    },
    text: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default:''
    }, width: {
      type: String,
      default:'70%'
    }, height: {
      type: String,
      default:'60%'
    },
    reloadFormKey: {
      type: String,
      default: '',
    }
  },
  data() {
    return {
      reloadData: this.referConfig.options.reloadData || false,
      visible: false,
      options: this.referConfig.options,
      textD: this.text || '',
      valueD: this.value || '',
      lazy: this.referConfig.options.loadType == 'node',
      isMuti: ("true" == this.referConfig.options.isMuti || this.referConfig.options.isMuti == true || this.referConfig.options.muti == true) ? true : false,
      props: {
        label: 'label',
        value: 'value',
        children: 'children'
      },
      config: {
        valueField: this.referConfig.valueField || this.referConfig.options.valueField || 'id',
        textField: this.referConfig.textField || this.referConfig.options.textField || "name",
        textSep: this.referConfig.textSep || ' '
      },
      treeUrl: this.referConfig.options.url || 'defaultReferTree',
      treeData: [],
      checkedData: [],
      currentNode: {},
      params: {},
      loadType: {'all': 'all', 'node': 'node'}
    };
  },
  created() {
    this.getParams();
  },
  mounted() {
    if(!this.lazy){
      if (this.options.data) {//如果是固定数据的情况下
        this.treeData = this.options.data;
      } else {
        this.getTree();
      }
    }
  },
  watch:{
    text:{
      handler(val) {
        this.textD=val;
      }
    },
    value:{
      handler(val) {
        this.valueD=val;
      }
    },
    reloadFormKey:{
      handler(val) {
        if(val != this.params[(this.options.paramForFormKey ? this.options.paramForFormKey : this.options.useFormKey)]){
          this.params[(this.options.paramForFormKey ? this.options.paramForFormKey : this.options.useFormKey)] = val;
          this.clearValue();
          this.getTree();
        }
      },
    }
  },
  computed: {
    placeholder: function () {
      return this.options.placeholder ? this.options.placeholder : this.title;
    }
  },
  methods: {
    getParams: function () {
      var queryParams = {};
      if (this.options.extraParams) {
        queryParams = this.options.extraParams;
      }
      queryParams['referBusCode'] = this.options['referBusCode'];
      if (this.options.useFormKey && this.options.formValues) {
        //使用表单上的字段来过滤
        queryParams[ (this.options.paramForFormKey ? this.options.paramForFormKey : this.options.useFormKey)] = this.options.formValues[this.options.useFormKey];
      }
      if (this.options.rootParams) {
        for (let key in this.options.rootParams) {
          queryParams[key] = this.options.rootParams[key];
        }
      }
      this.params = queryParams;
 
    },
    getTree() {
      //加载全部
      getTree(this.params, this.treeUrl).then(res => {
        this.treeData =  this.initTreeData(res.data);
      });
    },
    treeLoad: function (treeNode, resolve) {
      //逐级加载
      const parentId = (treeNode.level === 0) ? 0 : treeNode.data.attributes.id;
      this.params.parentId =  parentId;
      this.params.parentValue = parentId;
 
      if (this.options.rootParams && treeNode.level !== 0) {
        for (var key in this.options.rootParams) {
          delete this.params[key];
        }
      }
      getLazyTree(this.params, this.treeUrl).then(res => {
        resolve(this.initTreeData(res.data));
      });
    },
    initTreeData:function (nodes){
      let treeData=[];
      if(nodes && nodes.length>0){
        nodes.forEach(item => {
          let children=item.children || [];
          if(children && children.length>0){
            children=this.initTreeData(children);
          }
          treeData.push({
            label:item[this.config.textField],
            value:item[this.config.valueField],
            leaf: !item.children,
            children:children,
            attributes:item
          });
        });
      }
      return treeData;
    },
    nodeClick(data) {
      if (!this.isMuti) {
        this.setValue({checkedNodes: [data]});
      }
    },
    checked(checkedNode, checkedData) {
      this.setValue(checkedData);
    },
    clearValue(){
      this.valueD = '';
      this.textD = '';
      let mapFields =  this.referConfig.propMap || {};
      try {
        if (this.options.propMap) {
          mapFields = Object.assign(mapFields,this.options.propMap);
        }
        if (this.options.mapProps) {
          mapFields = Object.assign(mapFields,this.options.mapProps);
        }
      } catch (e) {
        ;
      }
      this.$emit("setReferValue", {
        prop: this.referConfig.prop,
        showProp: this.referConfig.showProp,
        value: this.valueD,
        text: this.textD,
        rawData: [],
        propMap: mapFields
      });
      this.visible = false;
    },
    setValue: function (checkedData) {
      this.checkedData = checkedData;
      let value = [];
      let text = [];
      let rawData=[];
      const textSep = this.config.textSep;
      for (var j = 0; j < checkedData.checkedNodes.length; j++) {
        const item = checkedData.checkedNodes[j].attributes || checkedData.checkedNodes[j];
        rawData.push(item);
        let v = item[this.config.valueField] || item.extendData[this.config.valueField];
        value.push(v);
        var tempRaw = [];
        var textFieldArray = this.config.textField.split(",");
        for (var i = 0; i < textFieldArray.length; i++) {//显示的字段可能有多个
          if (!verifyNull(textFieldArray[i])) {
            var t = item[textFieldArray[i]] || item.extendData[textFieldArray[i]];
            tempRaw.push(t);
          }
        }
        text.push(tempRaw.join(textSep));
      }
      let mapFields =  this.referConfig.propMap || {};
      try {
        if (this.options.propMap) {
          mapFields = Object.assign(mapFields,this.options.propMap);
        }
        if (this.options.mapProps) {
          mapFields = Object.assign(mapFields,this.options.mapProps);
        }
      } catch (e) {
        ;
      }
      this.valueD = value.join(',');
      this.textD = text.join(',');
      this.$emit("setReferValue", {
        prop: this.referConfig.prop,
        showProp: this.referConfig.showProp,
        value: this.valueD,
        text: this.textD || '',
        isTreeMuti: this.isMuti,
        rawData: rawData,
        propMap: mapFields
      });
    }
  }
};
</script>
 
<style scoped>
 
</style>