WXL
3 天以前 ead85633109bcb3cc8d8b3c6804c280dd6ee1e5d
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<template>
  <div class="selectclass">
    <el-select
      v-model="myValue"
      :loading="isLoading"
      :filterable="filterable"
      :filter-method="filterMethod"
      clearable
      :multiple="multiple"
      :multiple-limit="multipleLimit"
      :disabled="disabled"
      :default-first-option="defaultFirstOption"
      :size="size"
      @focus="focusEvents.func"
      @change="change"
      @blur="changeBlur"
      @visible-change="handleVisibleChange"
      value-key="organizationid"
      allow-create
      reserve-keyword
      :placeholder="placeholder ? placeholder : '请选择'"
      class="full-block"
      ref="selecter"
    >
      <el-option
        v-for="(spec, index) in displayList"
        :key="spec.organizationid"
        :label="spec.organizationname"
        :value="spec.organizationid"
      >
      </el-option>
    </el-select>
  </div>
</template>
 
<script>
import { listOrganization, listReportname } from "@/api/project/organization";
 
export default {
  name: "OrgSelecter",
  components: {},
  props: {
    //Select基础属性
    value: {
      type: [String, Array]
    },
    //获取列表
    dataList: {
      type: Array,
      default: function() {
        return [];
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    clearable: {
      type: Boolean,
      default: false
    },
    filterable: {
      type: Boolean,
      default: true
    },
    multiple: {
      type: Boolean,
      default: false
    },
    multipleLimit: {
      type: Number,
      default: 0
    },
    defaultFirstOption: {
      type: Boolean,
      default: true
    },
    size: {
      type: String,
      default: "small"
    },
    placeholder: {
      type: String,
      default: ""
    },
    //业务属性
    lazyLoad: {
      type: Boolean,
      default: false
    },
    isAll: {
      type: Boolean,
      default: false
    },
    //机构类型
    orgType: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      pageData: { pageNum: 1, pageSize: 100 },
      isLoading: false,
      // 三个独立的数据源,避免相互污染
      originalList: [], // 完整的原始数据源
      tempList: [],     // 中间缓存数据
      displayList: [],  // 用于渲染显示的列表
      myValue: this.multiple ? [] : "",
      focusEvents: {
        func: () => {},
        loaded: false
      }
    };
  },
  mounted() {
    if (this.lazyLoad && !this.filterable) {
      console.error("'lazy-load'必须和'filterable'同时使用!");
      return false;
    }
 
    if (this.lazyLoad) {
      this.focusEvents.func = () => {
        if (!this.focusEvents.loaded) {
          this.renderSelecter();
        }
      };
    } else {
      this.renderSelecter();
    }
  },
  methods: {
    renderSelecter() {
      this.pageData.PageSize = 100;
      this.myValue = this.value;
      this.getdataList();
    },
 
    //获取数据
    getdataList() {
      this.isLoading = true;
      let searchData = {
        organizationtype: this.orgType, //传入的类型
        pageNum: 1,
        pageSize: 100000
      }; //搜索条件
 
      let userType = { userType: "1" };
      if (this.orgType == 4) {
        let arr = this.$store.state.user.organization;
        this.originalList.push(...arr);
        if (this.isAll) {
          let all = {
            organizationid: "",
            organizationname: "全部"
          };
          this.originalList.unshift(all);
        }
        // 初始化三个列表为相同数据
        this.tempList = [...this.originalList];
        this.displayList = [...this.originalList];
        this.focusEvents.loaded = true;
        this.isLoading = false;
        return;
      }
 
      listOrganization(searchData)
        .then(response => {
          this.originalList.push(...response.rows);
          if (this.isAll) {
            let all = {
              organizationid: "",
              organizationname: "全部"
            };
            this.originalList.unshift(all);
          }
          // 初始化三个列表为相同数据
          this.tempList = [...this.originalList];
          this.displayList = [...this.originalList];
          this.focusEvents.loaded = true;
        })
        .catch(error => {
          console.error("获取机构列表失败:", error);
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
 
    /**
     * 自定义筛选方法 - 修复搜索无效问题
     * @param {string} val - 搜索关键词
     */
    filterMethod(val) {
      if (val) {
        // 对临时列表进行筛选,不修改原始数据
        this.displayList = this.tempList.filter(item => {
          return (
            (item.PYM && item.PYM.toUpperCase().includes(val.toUpperCase())) ||
            (item.WBM && item.WBM.toUpperCase().includes(val.toUpperCase())) ||
            (item.organizationname &&
             item.organizationname.toUpperCase().includes(val.toUpperCase()))
          );
        });
      } else {
        // 搜索词为空时显示完整列表
        this.displayList = [...this.tempList];
      }
    },
 
    /**
     * 处理下拉框显示/隐藏事件 - 修复数据恢复问题
     * @param {boolean} visible - 是否可见
     */
    handleVisibleChange(visible) {
      if (!visible) {
        // 下拉框关闭时重置显示列表为完整数据
        this.displayList = [...this.originalList];
        this.tempList = [...this.originalList];
      }
    },
 
    getOptionByValue(key) {
      return this.originalList.find(item => item.organizationid == key) || null;
    },
 
    focus() {
      this.$refs.selecter.focus();
    },
 
    blur() {
      this.$refs.selecter.blur();
    },
 
    changeBlur() {},
 
    change(data) {
      this.$nextTick(() => {
        this.$emit("change", data);
      });
    },
 
    defaultInit(defaultItem, defaultItemName) {
      if (defaultItem && defaultItemName) {
        let list = this.originalList.filter(
          r =>
            r.organizationid == defaultItem &&
            r.organizationname == defaultItemName
        );
        if (list.length == 0) {
          let data = {
            organizationname: defaultItemName,
            organizationid: defaultItem
          };
          this.originalList.push(data);
          this.tempList = [...this.originalList];
          this.displayList = [...this.originalList];
        }
      }
    }
  },
  computed: {},
  watch: {
    // v-model 对外暴露
    value(newVal) {
      this.myValue = newVal;
    },
    myValue(newVal) {
      this.$emit("input", newVal);
      this.$emit("change", newVal);
    },
    // 监听props中的dataList变化
    dataList: {
      handler(newList) {
        if (newList && newList.length > 0) {
          this.originalList = [...newList];
          this.tempList = [...newList];
          this.displayList = [...newList];
        }
      },
      immediate: true,
      deep: true
    }
  }
};
</script>
 
<style lang="scss" scoped>
::v-deep.selectclass .el-select {
  display: inline-block;
  position: relative;
  width: 90%;
}
 
// 优化搜索框样式
::v-deep.selectclass .el-select__input {
  min-width: 80px;
}
 
// 确保选项列表正常显示
::v-deep.selectclass .el-select-dropdown {
  z-index: 9999 !important;
}
</style>