-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiselect.js
39 lines (37 loc) · 975 Bytes
/
multiselect.js
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
export const multiselect = () => {
return {
options: [],
selected: [],
show: false,
open() {
this.show = true
},
close() {
this.show = false
},
select(optionIndex) {
if (!this.options[optionIndex].selected) {
this.options[optionIndex].selected = true;
this.selected.push(optionIndex);
} else {
this.remove(optionIndex);
}
},
remove(optionIndex) {
this.selected.splice(this.selected.lastIndexOf(optionIndex), 1);
this.options[optionIndex].selected = false
},
loadOptions() {
const options = this.$refs.hiddenSelect.options;
for (let i = 0; i < options.length; i++) {
let selected = options[i].getAttribute("selected") != null;
this.options.push({
value: options[i].value,
text: options[i].innerText,
selected: selected
});
if (selected) { this.selected.push(i); }
}
}
}
}