-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSelect2.vue
More file actions
58 lines (56 loc) · 1.62 KB
/
Select2.vue
File metadata and controls
58 lines (56 loc) · 1.62 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
<template>
<select>
<slot></slot>
</select>
</template>
<script>
// Based on https://vuejs.org/v2/examples/select2.html but adapted to handle list values
// with "multiple: true" set.
import $ from "jquery";
export default {
props: ["options", "value", "placeholder", "containerClass", "enabled"],
watch: {
value: function (value) {
// update value
$(this.$el).val(value);
},
options: function (options) {
// update options
$(this.$el).empty().select2({ data: options });
},
enabled: function (value) {
$(this.$el).select2("enable", value);
},
},
mounted: function () {
const vm = this;
// TODO: refactor property list to objects that allow defaults and types
let enabled = this.enabled;
if (enabled === undefined) {
enabled = true;
}
const select2Options = {
data: this.options,
placeholder: this.placeholder,
allowClear: !!this.placeholder,
enable: enabled,
dropdownAutoWidth: true,
};
if (this.containerClass) {
select2Options.containerCssClass = this.containerClass;
}
$(this.$el)
// init select2
.select2(select2Options)
.val(this.value)
.trigger("change")
// emit event on change.
.on("change", function (event) {
vm.$emit("input", event.val);
});
},
destroyed: function () {
$(this.$el).off().select2("destroy");
},
};
</script>