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
<!--
* @Author: luopeng
* @Description: el-select下拉滚动加载
* @Date: 2022-08-31 09:22:49
* @LastEditTime: 2022-08-31 15:53:35
-->
<template>
<div class="content">
<el-select
:value="value"
clearable
:size="size"
:multiple="multiple"
:filter-method="filterMethod"
v-bind="$attrs"
v-select-showmore="showMore(rangeNumber)"
@visible-change="visibleChange"
@input="handleInput"
@change="handleChange"
>
<el-option
v-for="item in options.slice(0, rangeNumber)"
:key="item.value"
:label="item.label"
:value="item[valueKey]"
></el-option>
</el-select>
</div>
</template>

<script>
import { debounce } from 'lodash';
import { deepClone } from '@/utils';

export default {
props: {
value: { type: [String, Number, Array] },
list: {
type: Array,
default: () => [],
},
valueKey: {
type: String,
default: 'value',
},
size: {
type: String,
default: 'mini',
},
multiple: {
type: Boolean,
default: false,
},
},
data() {
return {
options: [],
rangeNumber: 10,
};
},
computed: {
initValue() {
const newArr = deepClone(this.list);
if (this.value) {
if (this.multiple) {
const checks = this.list.filter((item) => this.value.includes(item[this.valueKey]));
checks.forEach((item) => {
const index = this.list.indexOf(item);
newArr.unshift(newArr.splice(index, 1)[0]);
});
} else {
const checks = this.list.find((item) => item[this.valueKey] === this.value);
const index = this.list.indexOf(checks);
newArr.unshift(newArr.splice(index, 1)[0]);
}
}
return newArr;
},
},
methods: {
// elementui下拉超过7条会出滚动条,如果初始不出滚动条无法触发loadMore方法
showMore() {
/* eslint-disable no-return-assign */
return () => (this.rangeNumber += 10); // 每次滚动到底部可以新增条数
},
// 筛选方法
filterMethod: debounce(function filterFun(filterVal) {
const query = filterVal?.trim() || '';
if (query) {
/* eslint-disable max-len */
const filterArr = this.list.filter((item) => item.label?.toLowerCase().includes(query.toLowerCase()));
this.options = filterArr;
} else {
// this.options = this.list;
this.options = this.initValue;
}
}, 300),
// 下拉框出现时,调用过滤方法
visibleChange(flag) {
if (flag) {
this.filterMethod();
}
},

handleChange(value) {
if (this.multiple) {
const selectList = this.options.filter((item) => value.includes(item[this.valueKey]));
this.$emit('change', selectList);
return;
}
const selectObj = this.options.find((item) => item[this.valueKey] === value);
this.$emit('change', selectObj);
},
handleInput(val) {
this.$emit('input', val);
},
},
mounted() {
this.options = this.initValue;
},
directives: {
'select-showmore': (el, binding) => {
const selectWrapDom = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
if (selectWrapDom) {
const scrollFun = () => {
/* 如果元素滚动到底, 下面等式返回true, 没有则返回false */
const condition = selectWrapDom.scrollHeight - selectWrapDom.scrollTop <= selectWrapDom.clientHeight;
if (condition) binding.value();
};
selectWrapDom.addEventListener('scroll', scrollFun);
}
},
},
};
</script>