Skip to content

Instantly share code, notes, and snippets.

@tyler-shaw
Created June 2, 2019 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyler-shaw/4ad5903fda6fb47f201e28fca5171d7d to your computer and use it in GitHub Desktop.
Save tyler-shaw/4ad5903fda6fb47f201e28fca5171d7d to your computer and use it in GitHub Desktop.
Vue Select2 Wrapper
<template>
<div>
Select an option:
<select2 v-model="fruit">
<option value="">None</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
</select2>
</div>
</template>
<script>
import Select2 from './Select2.vue';
export default {
components: {
'select2': Select2,
},
data() {
return {
fruit: '',
};
},
};
</script>
<template>
<select>
<slot></slot>
</select>
</template>
<script>
export default {
props: ['options', 'value'],
mounted: function () {
let vm = this
$(this.$el)
.select2({ data: this.options })
.val(this.value)
.trigger('change')
.on('change', function (e) {
vm.$emit('input', this.value);
vm.$emit('change', e);
})
},
watch: {
value: function (value) {
$(this.$el).val(value).trigger('change');
},
options: function (options) {
$(this.$el).empty().select2({ data: options })
}
},
destroyed: function () {
$(this.$el).off().select2('destroy')
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment