1 | <template>
|
2 | <select
|
3 | v-model="currentValue"
|
4 | :name="name"
|
5 | :multiple="!single"
|
6 | >
|
7 | <slot/>
|
8 | </select>
|
9 | </template>
|
10 |
|
11 | <script>
|
12 | const $ = window.jQuery
|
13 | const deepCopy = arg => {
|
14 | if (!arg) {
|
15 | return arg
|
16 | }
|
17 | return $.extend(true, Array.isArray(arg) ? [] : {}, arg)
|
18 | }
|
19 |
|
20 | export default {
|
21 | name: 'MultipleSelect',
|
22 |
|
23 | props: {
|
24 | value: {
|
25 | type: [String, Array],
|
26 | default: undefined
|
27 | },
|
28 | name: {
|
29 | type: String,
|
30 | default: undefined
|
31 | },
|
32 | single: {
|
33 | type: Boolean,
|
34 | default: false
|
35 | },
|
36 | width: {
|
37 | type: [Number, String],
|
38 | default: undefined
|
39 | },
|
40 | data: {
|
41 | type: Array,
|
42 | default () {
|
43 | return undefined
|
44 | }
|
45 | },
|
46 | options: {
|
47 | type: Object,
|
48 | default () {
|
49 | return {}
|
50 | }
|
51 | }
|
52 | },
|
53 |
|
54 | data () {
|
55 | return {
|
56 | currentValue: this.value
|
57 | }
|
58 | },
|
59 |
|
60 | watch: {
|
61 | options: {
|
62 | handler () {
|
63 | this._initSelect()
|
64 | },
|
65 | deep: true
|
66 | },
|
67 |
|
68 | data: {
|
69 | handler () {
|
70 | this.load(deepCopy(this.data))
|
71 | },
|
72 | deep: true
|
73 | }
|
74 | },
|
75 |
|
76 | mounted () {
|
77 | this.$select = $(this.$el).change(() => {
|
78 | const value = this.$select.val()
|
79 | this.$emit('input', value)
|
80 | this.$emit('change', value)
|
81 | })
|
82 |
|
83 | for (const event in $.fn.multipleSelect.defaults) {
|
84 | if (/^on[A-Z]/.test(event)) {
|
85 | $.fn.multipleSelect.defaults[event] = (...args) => {
|
86 | this.$emit(event, ...args)
|
87 | }
|
88 | }
|
89 | }
|
90 |
|
91 | this._initSelect()
|
92 | },
|
93 |
|
94 | methods: {
|
95 | _initSelect () {
|
96 | const options = {
|
97 | ...deepCopy(this.options),
|
98 | single: this.single,
|
99 | width: this.width,
|
100 | data: deepCopy(this.data)
|
101 | }
|
102 | if (!this._hasInit) {
|
103 | this.$select.multipleSelect(options)
|
104 | this._hasInit = true
|
105 | } else {
|
106 | this.refreshOptions(options)
|
107 | }
|
108 | },
|
109 |
|
110 | ...(() => {
|
111 | const res = {}
|
112 | for (const method of $.fn.multipleSelect.methods) {
|
113 | res[method] = function (...args) {
|
114 | return this.$select.multipleSelect(method, ...args)
|
115 | }
|
116 | }
|
117 | return res
|
118 | })()
|
119 | }
|
120 | }
|
121 | </script>
|