Skip to content

Instantly share code, notes, and snippets.

@matinfo
Forked from jasonlfunk/vue2-select2.js
Created June 1, 2018 11:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save matinfo/eeee0ff7d667293d4a317cf759142bcf to your computer and use it in GitHub Desktop.
Vue.component('select2', {
props: ['options', 'value'],
template: `
<div>
<select multiple ref='select'>
<slot></slot>
</select>
</div>
`,
mounted: function () {
var vm = this;
$(this.$refs.select)
.select2({ data: this.options })
.on('change', function (ev, args) {
if (!(args && "ignore" in args)) {
vm.$emit('input', $(this).val())
}
});
Vue.nextTick(() => {
$(this.$refs.select)
.val(this.value)
.trigger('change', { ignore: true })
});
},
watch: {
value: function (value, oldValue) {
// update value
$(this.$refs.select)
.val(this.value)
.trigger('change', { ignore: true });
},
options: function (options) {
// update options
$(this.$refs.select).select2({ data: options })
}
},
destroyed: function () {
$(this.$refs.select).off().select2('destroy')
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment