index.vue
7.31 KB
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
<template>
<el-select
class="zee__select"
:disabled="disabled"
:value-key="valueKey"
:filterable="filterable"
:remote="remote"
:reserve-keyword="reserveKeyword"
:clearable="clearable"
:placeholder="placeholder"
:remote-method="remoteMethod"
:loading="loading"
:size="size"
:multiple="multiple"
v-model="model"
v-on="bindEvents"
v-bind="selectProps"
>
<el-option v-for="item in optionsCurrent" :key="item.id" :label="labelFormat ? labelFormat(item) : item[labelKey]" :value="raw ? item : item[valueKey]" :disabled="item.disabled">
<slot :item="item" :value="model"></slot>
</el-option>
<slot name="empty" slot="empty"></slot>
<template slot="prefix">
<i v-if="initing" class="el-icon-loading"></i>
<slot v-else name="prefix"></slot>
</template>
</el-select>
</template>
<script>
export default {
name: 'Select',
props: {
value: [String, Number, Boolean, Object, Array],
// 占位符
placeholder: {
type: String,
default: '请选择',
},
// 选项数组
options: {
type: Array,
default: () => [],
},
// 选中Label格式化方法
labelFormat: Function,
labelKey: {
type: String,
default: 'name',
},
valueKey: {
type: String,
default: 'code',
},
searchKey: {
type: String,
default: 'query',
},
size: {
type: String,
default: 'mini',
},
multiple: Boolean,
disabled: Boolean,
clearable: {
type: Boolean,
default: true,
},
filterable: {
type: Boolean,
default: true,
},
reserveKeyword: {
type: Boolean,
default: true,
},
selectProps: Object,
// 保持原值,即绑定值为对象
raw: Boolean,
// 远程搜索URL
url: String,
// HTTP请求库
http: Function,
// 自定义接口
queryApi: Function,
// 触发远程搜索的字段长度
triggerSize: {
type: Number,
default: 0,
},
auto: Boolean,
update: Boolean,
},
data() {
return {
model: this.value,
optionsDataSource: this.fixOptions(this.options),
optionsCurrent: this.fixOptions(this.options),
loading: false,
initing: false,
};
},
created() {
if (this.remote && this.auto) {
this.initing = true;
this.remoteMethod();
}
},
watch: {
value(val) {
this.model = val;
},
},
computed: {
request() {
return this.http || this.zHttp;
},
remote() {
return Boolean(this.queryApi || (this.url && (this.http || this.zHttp)));
},
bindEvents() {
let _events = {};
Object.keys(this.$listeners || {}).forEach(key => {
// 非绑定对象的情况下,通过change事件向上emit出当前选中项
if (key === 'change' && !this.raw) {
_events[key] = value => {
this.$emit(
key,
value,
this.optionsCurrent.reduce((result, item) => {
if (value.includes(item[this.valueKey])) {
result.push(item);
}
return result;
}, []),
);
};
} else {
_events[key] = e => {
this.$emit(key, e);
};
}
});
return {
..._events,
'visible-change': show => {
if (this.remote && this.update && show) {
this.remoteMethod();
}
_events['visible-change'] && _events['visible-change'](show);
},
};
},
},
methods: {
// 修复当前数据源包含当前选中项
fixOptions(list) {
if (!this.value || (this.value instanceof Array && this.value.length === 0) || (this.value instanceof Object && Object.keys(this.value).length === 0)) {
return list;
}
if (this.raw) {
// 绑定为对象值时修复默认项
let fixOptions = [];
let defaultOptions = [];
if (this.multiple) {
fixOptions = [...this.value];
this.options.forEach(item => {
if (this.value.find(i => i[this.valueKey] === item[this.valueKey])) {
defaultOptions.push(item);
}
});
} else {
fixOptions = [this.value];
this.options.forEach(item => {
if (item[this.valueKey] === this.value[this.valueKey]) {
defaultOptions.push(item);
}
});
}
let hash = {};
const options = [...fixOptions, ...defaultOptions, ...list].reduce((result, item) => {
if (!hash[item[this.valueKey]]) {
// 如果当前元素的key值没有在hash对象里,则可放入最终结果数组
hash[item[this.valueKey]] = true; // 把当前元素key值添加到hash对象
result.push(item); // 把当前元素放入结果数组
}
return result; // 返回结果数组
}, []);
return options;
} else {
// 绑定为非对象值时修复默认项
let defaultOptions = [];
if (this.multiple) {
this.options.forEach(item => {
if (this.value.find(i => i === item[this.valueKey])) {
defaultOptions.push(item);
}
});
} else {
const targetOption = this.options.find(item => item[this.valueKey] === this.value);
if (targetOption) {
defaultOptions = [targetOption];
}
}
let hash = {};
const options = [...defaultOptions, ...list].reduce((result, item) => {
if (!hash[item[this.valueKey]]) {
// 如果当前元素的key值没有在hash对象里,则可放入最终结果数组
hash[item[this.valueKey]] = true; // 把当前元素key值添加到hash对象
result.push(item); // 把当前元素放入结果数组
}
return result; // 返回结果数组
}, []);
return options;
}
},
// 远程加载
remoteMethod(val = '') {
const searchText = val.trim();
if (searchText.length >= this.triggerSize) {
this.loading = true;
let requestPrimise;
if (this.queryApi) {
requestPrimise = this.queryApi(searchText);
} else {
requestPrimise = this.request({
url: this.url,
method: 'get',
params: searchText ? { [this.searchKey]: searchText } : {},
});
}
requestPrimise
.then(list => {
const options = this.fixOptions(list);
this.optionsDataSource = options;
this.optionsCurrent = options;
// 获取到选项数据源时刷新绑定显示值
this.model = undefined;
this.$nextTick(() => {
this.model = this.value;
});
})
.finally(() => {
this.loading = false;
this.initing = false;
});
} else {
this.optionsCurrent = this.optionsDataSource.filter(item => {
return item[this.labelKey].includes(val);
});
}
},
},
};
</script>
<style lang="scss">
.zee__select {
.el-input__prefix {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
.el-icon-loading {
font-size: 16px;
}
}
}
</style>